Question: Create Class to satisfy JUnit Test JAVA JUnit TEST: . Playlist Class Each Playlist object contains a reference to an array of Song objects that
Create Class to satisfy JUnit Test
JAVA

JUnit TEST:

.







Playlist Class Each Playlist object contains a reference to an array of Song objects that is stored in the songs field. When a Playlist object is constructed a new song array is also constructed. The array is initially empty, but Songs can be added and removed by calling the methods addSong, addSongs, and removeSong. In CS 1323/4, we used the adjective oversize" to describe arrays such as this, which have extra space for adding data. An oversize array has both a capacity and a size. The capacity is the length of the array, and the size is the number of elements treated as non-empty. (For instance, if an array has a capacity of 8 and a size of 6, we treat the first 6 elements as valid data and the last 2 as empty space.) In a Playlist object, the size of the song array is stored in the num Songs field. Below are descriptions of how each Playlist method should work Playlist(): Initialize a Playlist with an empty Song array of length MIN_CAPACITY Playlist (int capacity) : Initialize a Playlist with an empty Song array of the given capacity. If the capacity is less than HIN_CAPACITY, use MIN_CAPACITY instead. getCapacity): Return the length of the song array, getNumSongs(): Return the number of Songs in the array (i.e., the field num Songs). getSong(int index): Return the song with the given index in the array. If the index is less than 0 or greater than the index of the last Song, return null. getSongs(): Return a copy of the song array with no extra space. That is, the length of the new array should be equal to the number of Songs. (In CS 1323/4, we used the adjective "perfect size" to describe arrays like this.) addSong (Song song) : Assign the given Song to the first empty element of the array and return true. If the array is full, leave it unchanged and return false. Use the other addSong method to implement this method in a single line.) addSong (int index, Song song): Add the given Song to the array at the given index and return true. Before assigning the song, shift the existing Songs with indices greater than or equal to the given index up to the next- highest index. If any of the following conditions is true, leave the array unchanged and return false; The array is full. The given index is less than 0 or greater than the index of the last Song plus 1. The Song reference is null. addSongs (Playlist playlist): Add the songs in the given Playlist to the end of the array in the given order. If there is insufficient space to add them all, add as many as will fit. Return the number of Songs that were added. If the Playlist reference is null, retum 0. renoveSong(): Remove and return the last song in the array. If the array is empty, return null. (Use the other removesong method to implement this method in a single line.) renovesong(int index): Remove and return the song with the given index in the array. Before returning the song, shift the songs with larger indices down to the next-lowest index. If there is no song in the array with the given index, return null. Hints Treat the field numSongs as an accumulator that tracks the number of songs in the array. When a Song is added, increment the variable. When a Song is removed, decrement it. The value of numSongs should be used along with the capacity to determine whether the song array can be modified. For example, suppose that numSongs is equal to the capacity. In this case, there is no empty space, so no Songs can be added. The removesong methods can be written to set unused elements to null, but this is not a requirement. (In an oversize array, any element with an index greater than or equal to the size is treated as empty space, regardless of whether it is null.) 11 15 16 18 19 10 import static org.junit.jupiter.api. Assertions.assertEquals; 12 class PlaylistTest { 13 14 private static final String TITLE_1 = "Am I the Same Girl?"; private static final String ARTIST_1 = "Barbara Acklin"; private static final int[] TIME_1 = {56, 2}; 17 private static final Song SONG_1 = new Song(TITLE_1, ARTIST_1, TIME_1); private static final String TITLE_2 = "Kissing My Love"; private static final String ARTIST_2 = "Bill Withers"; 21 private static final int[] TIME_2 = {49, 3}; 22 private static final Song SONG_2 = new Song(TITLE_2, ARTIST_2, TIME_2); 23 24 private static final String TITLE_3 = "Feelin' Alright?"; 25 private static final String ARTIST_3 = "Joe Cocker"; 26 private static final int[] TIME_3 {10, 4}; 27 private static final Song SONG_3 = new Song(TITLE_3, ARTIST_3, TIME_3); 28 29 private static final String TITLE_4 = "Lean on Me"; 30 private static final String ARTIST_4 = "Bill Withers"; 31 private static final int[] TIME_4 = {17, 4}; 32 private static final Song SONG_4 = new Song (TITLE_4, ARTIST_4, TIME_4); 33 34 private static final String TITLE_5 = "Little Wing"; 35 private static final String ARTIST_5 = "Stevie Ray Vaughan"; 36 private static final int[] TIME_5 {49, 6}; 37 private static final Song SONG_5 = new Song(TITLE_5, ARTIST_5, TIME_5); gn234568981 @Test void testConstantDeclaration() { try { Field field = Playlist.class.getDeclaredField("MIN_CAPACITY"); int modifiers = field.getModifiers(); assertTrue (Modifier is Private (modifiers)); assertTrue (Modifier.isStatic(modifiers)); assertTrue (Modifier isFinal (modifiers)); } catch (No SuchfieldException e) { fail(); } } @Test void testConstructors() { Playlist playlist new Playlist(); assertEquals(3, playlist.getCapacity(); assertEquals(@, playlist.getNumSongs()); playlist = new Playlist(-1); assertEquals(3, playlist.getCapacity(); assertEquals(@, playlist.getNumSongs()); playlist = new Playlist(1); assertEquals(3, playlist.getCapacity(); assertEquals(@, playlist.getNumSongs()); playlist = new Playlist(42); assertEquals(42, playlist.getCapacity(); assertEquals(@, playlist.getNumSongs()); } 520 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 710 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 @Test void testAddSong1() { Playlist Playlist = new Playlist(); assertEquals(3, playlist.getCapacity(); assertEquals(@, playlist.getNum Songs()); assertNull(playlist.getSong(-1)); assertNull(playlist.getSong()); assertNull(playlist.getSong(1)); assertFalse (playlist.addSong(null)); assertEquals(3, playlist.getCapacity(); assertEquals(@, playlist.getNumSongs(); assertNull(playlist.getSong()); assertTrue (playlist.addSong (SONG 1)); assertEquals(3, playlist.getCapacity(); assertEquals(1, playlist.getNumSongs()); assertTrue (sameSong (SONG_1, playlist.getSong())); assertNull(playlist.getSong(1)); assertTrue (playlist.addSong (SONG_2)); assertEquals(3, playlist.getCapacity(); assertEquals(2, playlist.getNumSongs()); assertTrue (sameSong (SONG_1, playlist.getSong())); assertTrue sameSong (SONG 2, playlist.getSong(1))); 95 96 97 98 assertTrue (same Song (SONG 2, playlist.getsong(1))); assertNull(playlist.getSong(2)); 99 assertTrue (playlist.addSong (SONG_3)); assertEquals(3, playlist.getCapacity(); assertEquals(3, playlist.getNum Songs()); assertTrue (sameSong(SONG_1, playlist.getSong())); assertTrue (sameSong (SONG_2, playlist.getSong(1))); assertTrue (sameSong(SONG 3, playlist.getSong(2))); assertNull(playlist.getSong(3)); assertFalse (playlist.addSong (SONG_4)); assertEquals(3, playlist.getCapacity(); assertEquals(3, playlist.getNumSongs()); assertTrue (sameSong(SONG_1, playlist.getSong())); assertTrue (sameSong (SONG_2, playlist.getSong(1))); assertTrue (sameSong (SONG_3, playlist.getSong (2))); assertNull(playlist.getSong(3)); } 100 101 L02 L03 104 105 106 L07 108 109 110 111 112 113 114 115e 116 117 118 119 L20 L21 L22 L23 L24 L25 L26 L27 L28 L29 L30 L31 32 L33 L34 135 L36 L37 138 @Test void testAddSong20) { Playlist playlist = new Playlist(4); assertEquals(@, playlist.getNumSongs()); assertTrue (playlist.addSong(@, SONG_1)); assertEquals(1, playlist.getNumSongs()); assertTrue (same Song (SONG_1, playlist.getSong())); assertNull(playlist.getSong(1)); assertFalse (playlist.addSong(1, null)); assertEquals(1, playlist.getNumSongs(); assertTrue (sameSong(SONG_1, playlist.getSong())); assertNull(playlist.getSong(1)); assertFalse (playlist.addSong(-1, SONG_2)); assertFalse (playlist.addSong(2, SONG_2)); assertEquals(1, playlist.getNumSongs()); assertTrue (sameSong (SONG_1, playlist.getSong())); assertNull(playlist.getSong(1)); assertTrue (playlist.addSong(@, SONG_2)); assertEquals(2, playlist.getNumSongs()); assertTrue (sameSong (SONG_2, playlist.getSong())); assert rue (sames tsong())); assertTrue (sameSong(SONG_1, playlist.getSong(1))); assertNull(playlist.getSong(2)); assertTrue (playlist.addSong(1, SONG_3)); assertEquals(3, playlist.getNumSongs(); assertTrue (same Song (SONG_2, playlist.getSong())); assertTrue (same Song (SONG_3, playlist.getSong(1))); assertTrue (sameSong(SONG_1, playlist.getSong (2))); assertNull(playlist.getSong(3)); 138 139 140 141 x142 143 144 145 146 147 148 x149 150 151 152 153 154 155 x 156 157 158 159 160 161 162 163 x 164 x 165 x166 167 168 169 170 171 172 173 174 1750 176 177 178 179 180 181 assertFalse (playlist.addSong(4, SONG_4)); assertEquals(3, playlist.getNumSongs()); assertTrue (sameSong (SONG_2, playlist.getSong())); assertTrue (same Song(SONG 3, playlist.getSong(1))); assertTrue (sameSong(SONG_1, playlist.getSong (2))); assertNull(playlist.getSong(3)); assertTrue (playlist.addSong(3, SONG_4)); assertEquals(4, playlist.getNumSongs(); assertTrue (sameSong (SONG_2, playlist.getSong()); assertTrue (sameSong (SONG_3, playlist.getSong(1))); assertTrue (same Song (SONG_1, playlist.getSong(2)); assertTrue (sameSong (SONG_4, playlist.getSong (3)); assertNull(playlist.getSong (4)); assertFalse (playlist.addSong(2, SONG_5)); assertFalse (playlist.addSong(4, SONG_5)); assertFalse(playlist.addSong(10, SONG_5)); assertEquals(4, playlist.getNumSongs(); assertTrue (sameSong (SONG_2, playlist.getSong())); assertTrue (same Song (SONG_3, playlist.getSong(1)); assertTrue (sameSong (SONG_1, playlist.getSong(2))); assertTrue (sameSong (SONG_4, playlist.getSong(3))); assertNull(playlist.getSong(4)); } @Test void testsongs Encapsulation() { Playlist playlist = new Playlist(); playlist.addSong (SONG_5); Song[] songs = playlist.getSongs(); songs[@] = SONG_1; @Test void testSongsEncapsulation() { Playlist Playlist = new Playlist(); playlist.addSong (SONG_5); Song[] songs = playlist.getSongs(); songs[@] = SONG_1; assertTrue (sameSong (SONG_5, playlist.getSong())); } 1750 176 177 178 179 180 181 182 183 184 1850 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 @Test void testAddSongs1() { Playlist Playlist = new Playlist(5); assertEquals(@, playlist.addSongs (null)); assertTrue (sameSongs (new Song[] {}, playlist.getSongs()); assertEquals(@, playlist.addSongs (playlist)); assertTrue (sameSongs (new Song[] {}, playlist.getSongs()); playlist.addSong (SONG 1); assertTrue (sameSongs (new Song[] {SONG_1}, playlist.getSongs()); assertEquals(1, playlist.addSongs (playlist)); assertTrue (sameSongs(new Song[] {SONG_1, SONG_1}, playlist.getSongs()); assertEquals(2, playlist.addSongs (playlist)); assertTrue (sameSongs(new Song[] {SONG_1, SONG_1, SONG_1, SONG_1}, playlist.getSongs()); assertEquals(1, playlist.addSongs (playlist)); assertTrue (same Songs new Song[] {SONG_1, SONG_1, SONG_1, SONG_1, SONG_1}, playlist.getSongs()); assertEquals(@, playlist.addSongs (playlist)); assertTrue (same Songs new Song[] {SONG_1, SONG_1, SONG_1, SONG_1, SONG_1}, playlist.getSongs()); assertEquals(@, playlist.addSongs (null)); assertTrue (same Songs new Song[] {SONG_1, SONG_1, SONG_1, SONG_1, SONG_1}, playlist.getSongs()); @Test void testAddsongs2() { Playlist pi = new Playlist(4); Playlist p2 = new Playlist(4); assertEquals(@, pl.addSongs (p2)); assertEquals(@, p2.addSongs (p1)); assertTrue (sameSongs (new Song[] {}, pl.getSongs()); assertTrue (sameSongs (new Song[] }, p2.getSongs()); p1.addSong(SONG_1); p2.addSong(SONG_2); assertTrue (sameSongs (new Song[] {SONG_1}, p1.getSongs()); assertTrue (sameSongs (new Song[] {SONG_2}, p2.getSongs()); assertEquals(1, p1.addSongs (p2)); assertEquals(2, p2.addSongs (p1)); assertTrue (sameSongs (new Song[] {SONG_1, SONG_2}, p1.getSongs()); assertTrue (sameSongs(new Song[] {SONG_2, SONG_1, SONG_2}, p2.getSongs()); 2210 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 2570 258 259 260 261 262 263 264 assertEquals(2, p1.addSongs (p2)); assertEquals(1, p2.addSongs (p1)); assertTrue (sameSongs (new Song[] {SONG_1, SONG_2, SONG_2, SONG_1}, pi.getSongs()); assertTrue (sameSongs(new Song[] {SONG_2, SONG_1, SONG_2, SONG_1}, p2.getSongs()); assertEquals(@, p1.addSongs (p2)); assertEquals(@, p2.addSongs (p1)); assertTrue (sameSongs (new Song[] {SONG_1, SONG_2, SONG_2, SONG_1}, p1.getSongs()); assertTrue (sameSongs (new Song[] {SONG_2, SONG_1, SONG_2, SONG_1}, p2.getSongs()); } @Test void testRemoveSong1() { Playlist Playlist = new Playlist(); playlist.addSong (SONG 1); playlist.addSong (SONG_2); playlist.addSong (SONG 3); assertEquals(3, playlist.getNumSongs()); assertTrue (sameSong (SONG_3, playlist.removesong()); assertEquals(2, playlist.getNum Songs()); assertTrue (sameSong(SONG_1, playlist.getSong())); assertTrue (sameSong (SONG_2, playlist.getSong(1))); assertNull(playlist.getSong(2)); assertTrue (sameSong(SONG_2, playlist.removeSong()); assertEquals(1, playlist.getNum Songs()); assertTrue (sameSong (SONG_1, playlist.getSong())); assertNull(playlist.getSong(1)); assertTrue (sameSong (SONG_1, playlist.removesong())); assertEquals(@, playlist.getNum Songs()); assertNull(playlist.getSong()); assertNull(playlist.removesong(); assertEquals(@, playlist.getNumSongs()); } 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 2840 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 POZ @Test void testRemoveSong2() { Playlist playlist = new Playlist(5); playlist.addSong (SONG 1); playlist.addSong (SONG_2); playlist.addSong (SONG 3); playlist.addSong (SONG_4); playlist.addSong (SONG_5); assertTrue (sameSong (SONG_5, playlist.removesong(4))); assertTrue (sameSongs (new Song[] {SONG_1, SONG_2, SONG_3, SONG_4}, playlist.getSongs())); assertNull(playlist.removesong(4)); assertTrue (sameSongs (new Song[] {SONG_1, SONG_2, SONG_3, SONG_4}, playlist.getSongs())); assertTrue (sameSong (SONG_1, playlist.removesong())); assertTrue (sameSongs(new Song[] {SONG_2, SONG_3, SONG_4}, playlist.getSongs())); assertTrue (sameSong (SONG_3, playlist.removesong(1))); assertTrue (sameSongs (new Song[] {SONG_2, SONG_4}, playlist.getSongs()); assertTrue (same Song (SONG_2, playlist.remove Song())); assertTrue (sameSongs(new Song[] {SONG_4}, playlist.getSongs()); assertTrue (sameSong(SONG_4, playlist.removesong()); assertTrue (sameSongs(new Song[] {}, playlist.getSongs()); assertNull(playlist.remove Song(-1)); assertTrue (sameSongs(new Song[] {}, playlist.getSongs()); } // Define a helper method that checks if two Songs are the same. private static boolean sameSong (Song song1, Song song2) { // Check that the titles are the same. String titlel = song1.getTitle(); String title2 song2.getTitle(); if (!titlel.equals(title2)) { return false; } 308 309 310 311 312 313 314 315 316 317 318 319e 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 // Check that the artists are the same. String artisti = song1.getArtist(); String artist2 = song2.getArtist(); if (!artisti.equals(artist2)) { return false; } // Check that the times are the same (both length and elements). int[] time1 = song1.getTime(); int[] time2 = song2.getTime(); if (time1.length != time2.length) { return false; } for (int idx = 0; idx
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
