Question: Create a Playlist class to satisfy all the descriptions JAVA Each Playlist object contains a reference to an array of Song objects that is stored
Create a Playlist class to satisfy all the descriptions
JAVA


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 remove Song. 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 numSongs 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 MIN_CAPACITY, use MIN_CAPACITY instead. .getCapacity: Return the length of the song array. getNum Songs(): Return the number of Songs in the array (.e., the field numSongs). 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, return O. renoveSong(): Remove and return the last Song in the array. If the array is empty, return null. (Use the other remove Song method to implement this method in a single line.) removeSang(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 remove Song 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.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
