-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4JavaPlaylist.java.txt
More file actions
169 lines (135 loc) · 3.16 KB
/
Copy path4JavaPlaylist.java.txt
File metadata and controls
169 lines (135 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Java code samples
// Created by Linh Ngo
// For the Java2 class
// This is a Java class called "Playlist" that represents a playlist of songs.
// The class contains an array of songs, and methods for adding and removing songs from the playlist.
// Connected to Song.java.txt
import java.util.Arrays;
public class Playlist {
private Song [] songs;
private int numSongs;
private static final int MIN_CAPACITY=3;
private static final int EXPAND_THRESHOLD=4;
private int expandBy =2;
private int expandFrequency= 0;
// Constructor method that creates a new Playlist object with a default capacity of 3 songs.
public Playlist()
{
songs= new Song[MIN_CAPACITY];
}
// Constructor method that creates a new Playlist object with the specified capacity.
public Playlist(int capacity)
{
if(capacity<MIN_CAPACITY)
{
songs= new Song[MIN_CAPACITY];
}
else
{
songs= new Song[capacity];
}
}
// Method that returns the current capacity of the playlist.
public int getCapacity()
{
return songs.length;
}
// Method that returns the number of songs currently in the playlist.
public int getNumSongs()
{
return numSongs;
}
// Method that returns the song at the specified index in the playlist.
public Song getSong(int index)
{
if(index<0||index>songs.length-1)
{
return null;
}
else
{
return songs[index];
}
}
// Method that returns a copy of the array of songs in the playlist.
public Song[] getSongs()
{
Song[] newsongs= Arrays.copyOf(songs,numSongs);
return newsongs;
}
// Method that expands the capacity of the playlist by a factor of 2 if the playlist has been expanded more than 4 times.
public void expand()
{
if(expandFrequency>=EXPAND_THRESHOLD)
{
expandBy=expandBy*2;
}
Song[] newsonggs=Arrays.copyOf(songs,songs.length+expandBy);
this.songs=newsonggs;
expandFrequency+=1;
}
// Method that adds a new song to the playlist at the specified index.
public boolean addSong(int index, Song song)
{
if(song==null||index<0||index>numSongs)
{
return false;
}
if(getCapacity()<getNumSongs()+1)
{
expand();
}
for(int i=numSongs;i>index;--i)
{
songs[i]=songs[i-1];
}
songs[index]=song;
++ numSongs;
return true;
}
// Method that adds a new song to the end of the playlist.
public boolean addSong(Song song)
{
return addSong(numSongs,song);
}
// Method that adds all the songs from the specified playlist to the end of this playlist.
public int addSongs(Playlist playlist)
{
if(playlist==null)
{
return 0;
}
int added= playlist.getNumSongs();
{
for(int i=0;i<added;++i)
{
if(playlist!=null)
{
addSong(playlist.getSong(i));
}
}
}
return added;
}
// Method that removes the song at the specified index from the playlist.
public Song removeSong(int index)
{
if(getSong(index)==null)
{
return null;
}
Song remove= getSong(index);
songs[index]=null;
for(int i=index;i<numSongs-1;++i)
{
songs[i]=songs[i+1];
}
--numSongs;
return remove;
}
// Method that removes the last song in the playlist.
public Song removeSong()
{
return removeSong(numSongs-1);
}
}