Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Introduction In object-oriented programming, aggregation is a relationship between two classes where objects of one class contain objects of the other. In this lab,
Introduction In object-oriented programming, aggregation is a relationship between two classes where objects of one class contain objects of the other. In this lab, we will implement two such classes: Song and Playlist. Each object of the Song class represents a single musical composition. Each object of the Playlist class represents an ordered, modifiable list of songs. Aggregation is represented in UML diagrams by a line with an empty diamond on one end that connects two classes. Objects of the class touching the diamond contain objects of the other class. The following is a class diagram for the Song and Playlist classes: -songs: Song[] -numSongs: int Playlist -MIN CAPACITY = 3: int -EXPAND THRESHOLD = 4: int -expandBy = 2: int -expandFrequency = 0: int +Playlist(): ctor +Playlist(int capacity): ctor +getCapacity (): int +getNumSongs(): int +getSong (int index): Song +getSongs(): Song [] +addSong (Song song): boolean +expand(): void +addSong(int index, Song song): boolean +addSongs (Playlist playlist): int +removeSong(): Song +removeSong(int index): Song -title: String -artist: String -duration: int[] Song +Song (String title, String artist, int[] duration): ctor +getTitle(): String +getArtist(): String +getDuration (): int[] Song Class Each Song object contains two Strings and an integer array. These fields store the title, artist, and duration (time) of a song. The duration array has either one, two, or three elements that store the number of seconds, minutes, and hours, respectively. Each element is greater than or equal to 0, and the seconds and minutes are limited to the inclusive range from 0 to 59. If the duration of the song is less than an hour, the hours element is omitted. Similarly, if the song is less than a minute, both the minutes and hours elements are omitted. Consider, for example, the song "So What" by Miles Davis, which is 9 minutes and 22 seconds long. A Song object that contains this data will store "So What" and "Miles Davis" in the title and artist fields, and it will store [22, 9] in the duration field. The array has only two elements because the duration of the song is less than an hour. In the previous lab, we used encapsulation to handle invalid input passed to a class's constructor and setters. In this lab, we will assume that the Song constructor is always given valid data. In particular, we will assume that the integer array always satisfies the description above. Instead of checking the constructor input for errors, we're going to use encapsulation for another purpose: to make Song objects immutable. This means that the state of a Song cannot change after it is constructed. Notice that the Song class does not have any setters. This prevents the title and artist fields from changing. It does not, however, prevent the duration array from changing. If the user of our class has a reference to the array, the reference can be used to change the elements without a setter. (Why isn't this a problem for the String fields?) To keep this from happening, make copies of the duration array. Note that a copy must be made in two methods: (1) in the constructor, before assigning the reference to the duration field, and (2) in getDuration, before returning the reference. To simplify your code, try using the copy of method from the Arrays class instead of a for-loop. 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 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. getNumSongs() : Return the number of Songs in the array (i.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. get Songs (): 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.) expand(): Expands the capacity of the songs array when it is full. For example, if the current capacity of the songs array is 3, and we call this method for the first time, it should expand the capacity of the songs array by expand By which is initially 2 (the new capacity will be 3 + 2 = 5). This method first checks the number of times this array has been expanded before (expand Frequency initially equals 0). If this number is greater than or equal to a threshold ( EXPAND_THRESHOLD ), it should double the expansion step ( expand By ). Then, it should actually expand the array capacity by the new expandBy value, and adjust the expandFrequency value. 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: o The given index is less than 0 or greater than the index of the last Song plus 1. o The Song reference is null. After making sure none of the above conditions is satisfied, check if the songs array is full. If the songs array is full, expand the array first by calling expand(), then add the song at the correct index. addSong (Song song) : Assign the given Song to the first empty element of the array and return true. (Use the other addSong method to implement this method in a single line.) addSongs (Playlist playlist): Add the Songs in the given Playlist to the end of the array in the given order. Return the number of Songs that were added. If the Playlist reference is null, return 0. remove Song (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. remove Song(): 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.) 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 we need to expand () the array size before 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.)
Step by Step Solution
★★★★★
3.45 Rating (148 Votes )
There are 3 Steps involved in it
Step: 1
Step 1 Song Class add private data members Create a Song class with two strings and an integer array as shown below public class Song Create 2 String objects and an integer array private final String ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started