-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSongApp.java
More file actions
278 lines (278 loc) · 6.48 KB
/
SongApp.java
File metadata and controls
278 lines (278 loc) · 6.48 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
public class SongApp {
private Song[] items; // keep Songs in an unsorted array
private int numberOfItems; // number of Songs in the array
/*
* Default constructor creates array with capacity for 5 Songs
*/
SongApp ()
{
items = new Song[5];
this.numberOfItems = 0;
}
/*
* One argument constructor creates array with user defines capacity
*
* @param capacity defines the capacity of the Song library
*/
SongApp (int capacity)
{
items = new Song[capacity];
this.numberOfItems = 0;
}
/*
* Add a Song into the library (unsorted array)
*
* If the library is full (there is not enough space to add another Song)
* - create a new array with double the size of the current array.
* - copy all current Songs into new array
* - add new Song
*
* @param m The Song to be added to the libary
*/
public void addSong (Song m) {
if(numberOfItems < items.length)
{
items[numberOfItems] = m;
this.numberOfItems++;
}
else{
Song[] newList = new Song[items.length * 2];
int i;
for ( i=0; i<this.numberOfItems; i++)
{
newList[i] = items[i];
}
newList[i] = m;
this.numberOfItems++;
items = newList;
}
}
/*
* Removes a Song from the library. Returns true if Song is removed, false
* otherwise.
* The array should not become sparse after a remove, that is, all Songs
* should be in consecutive indexes in the array.
*
* @param m The Song to be removed
*
*/
public boolean removeSong (Song m) {
int i;
for (i=0; i<this.numberOfItems; i++){
if (items[i].getName() == m.getName())
break;
}
numberOfItems--;
for (int j=i; j < numberOfItems; j++){
items[j] = items[j+1];
return true;
}
return false;
}
/*
* Returns the library of songs
*
* @return The array of Songs
*/
public Song[] getSongs ()
{
return items;
}
/*
* Returns the current number of Songs in the library
*
* @return The number of items in the array
*/
public int getNumberOfItems ()
{
return this.numberOfItems;
}
/*
* Update the rating of Song @m to @rating
*
* @param @m The Song to have its ratings updated
* @param @rating The new rating of @m
* @return true if update is successful, false otherwise
*/
public boolean updateRating (Song m, int rating) {
for(int i=0; i<this.numberOfItems; i++){
if( items[i].getName() == m.getName() ){
items[i].setRating(rating);
return true;
}
}
return false;
}
/*
* Prints all Songs
*/
public void print () {
// ADD YOUR CODE HERE
System.out.println(" Name - Year - Rating - Writers ");
for(int i=0; i < this.numberOfItems; i++){
System.out.println("Song " + (i+1) + " " + items[i].toString());
}
}
/*
* Return all the Songs by @songwriter. The array size should be the
* number of Songs by @songwriter.
*
* @param songwriter The songwriter's name
* @param An array of all the Songs by @songwriter
*
*/
public Song[] getSongsBySongwriter (String songwriter) {
Song[] songList;
int count = 0;
for(int i=0; i<this.numberOfItems; i++){
for (int j=0; j < items[i].getNumberOfWriters(); j++ ) {
if( items[i].getWriterAtIndex(j) == songwriter){
count++;
break;
}
}
}
songList = new Song[count];
for(int i=0, k=0 ; i<this.numberOfItems; i++){
for (int j=0 ; j < items[i].getNumberOfWriters(); j++ ) {
if( items[i].getWriterAtIndex(j) == songwriter){
songList[k] = items[i];
k++;
break;
}
}
}
return songList;
}
/*
* Return all the Songs released in @year. The array size should be the
* number of Songs made in @year.
*
* @param year The year the Songs were made
* @return An array of all the Songs made in @year
*
*/
public Song[] getSongsByYear (int year) {
Song[] songList;
int count = 0;
for(int i=0; i<this.numberOfItems; i++){
if( items[i].getYear() == year){
count++;
}
}
songList = new Song[count];
for(int i=0, j=0; i<this.numberOfItems; i++){
if( items[i].getYear() == year ){
songList[j] = items[i];
j++;
}
}
return songList;
}
/*
* Return all the Songs with ratings greater then @rating
*
* @param rating
* @return An array of all Songs with rating greater than @rating
*
*/
public Song[] getSongsWithRatingsGreaterThan (int rating) {
// ADD YOUR CODE HERE
Song[] songList;
int count = 0;
for(int i=0; i<this.numberOfItems; i++){
if( items[i].getRating() > rating){
count++;
}
}
songList = new Song[count];
for(int i=0, j=0; i<this.numberOfItems; i++){
if( items[i].getRating() > rating ){
songList[j] = items[i];
j++;
}
}
return songList;
}
/*
* Search for Song name @name using binary search algorithm.
* Assumes items are sorted
*/
public Song searchSongByName (String name) {
int l = 0;
int r = this.numberOfItems - 1;
while (l <= r)
{
int m = l + (r-l)/2;
// Check if name is present at mid
if (items[m].getName() == name)
return items[m];
// If name greater, ignore left half
if (items[m].getName().compareTo(name) < 0)
l = m + 1;
// If name is smaller, ignore right half
else
r = m - 1;
}
return null;
}
/*
* Sorts Songs by year using insertion sort
*/
public void sortByYear () {
// ADD YOUR CODE HERE
int n = items.length;
for (int i=1; i<n; ++i)
{
Song key = items[i];
int j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j>=0 && items[j].getYear() > key.getYear())
{
items[j+1] = items[j];
j = j-1;
}
items[j+1] = key;
}
}
/*
* Sorts array of Songs by name using selection sort
*/
public void sortByName () {
// ADD YOUR CODE HERE
int n = items.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_index = i;
for (int j = i+1; j < n; j++)
if (items[j].compareTo(items[min_index]) < 0) // s1 < s2
min_index = j;
// Swap the found minimum element with the first
// element
Song temp = items[min_index];
items[min_index] = items[i];
items[i] = temp;
}
}
/*
* Search for Song name using recursive linear search
* @param name The name of the song to search
* @param Songs The array containing all songs
* @param l The left index
* @param r The right index
* @return The song with name @name or null if song is not found
*/
public static Song searchSongByName (String name, Song[] Songs, int l, int r) {
if (r < l)
return null;
if (Songs[l].getName() == name)
return Songs[l];
if (Songs[r].getName() == name)
return Songs[r];
return searchSongByName(name,Songs, l+1, r-1);
}
}