Introduction In object-oriented programming, aggregation is a relationship between two classes where objects of one class...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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.) 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.)
Expert Answer:
Answer rating: 100% (QA)
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 ... View the full answer
Related Book For
Business Statistics In Practice
ISBN: 9780073401836
6th Edition
Authors: Bruce Bowerman, Richard O'Connell
Posted Date:
Students also viewed these programming questions
-
How can adults continue to function relatively normally after surgery to remove the thymus, tonsils, spleen, or lymph nodes?
-
Graphically determine the resultant of the following three vector displacements: (1) 24 m, 36 north of east; (2) 18 m, 37 east of north; and (3) 26 m, 33 west of south.
-
Indicators For each of these potential predictor variables, say whether they should be represented in a regression model by indicator variables. If so, then suggest what specific indicators should be...
-
Does Fairmont have any contactor personnel whose have terminated but are being paid through payroll after termination (e.g., ghost employees)?
-
I Love My Chocolate Company makes dark chocolate and light chocolate. Both products require cocoa and sugar. The following planning information has been made available: I Love My Chocolate Company...
-
The trial balance for Lindor Corporation, a manufacturing company, for the year ended December 31, 2024, included the following accounts: Account Title Sales revenue Cost of goods sold Selling and...
-
A slab of polymer 1.6 cm thick, originally at 30C, is heated between steel plates with a surface temperature of 70C. The density of the solid is 950 kg/m 3 , its thermal conductivity is 0.12 W/m K,...
-
Discuss the factors that influence compensation structures within the organization. Based on your own experience, which ones do you think are the most important? Why?
-
R. v. Haevischer, 2023 SCC 11. This is a case dealing with the police conduct and conditions of pre-trial confinement which the accused believed amountd to an abuse of process. Who wrote the judgment...
-
Stan promised his favorite nephew Ned that he would leave his farm to Ned if Ned would live with him and tend to the farm for the rest of Stan's life. Ned did so, providing services to Stan that were...
-
The Larisa Company is exiting bankruptcy reorganization with the following account balances: Receivables Inventory Buildings Liabilities Common stock Additional paid-in capital Retained earnings...
-
Your client, D, Inc., has filed a petition. D, Inc.'s debt to C is guaranteed by G, who is the CEO and principal stockholder of D, Inc. C has sued G in state court on the guarantee. G has "asked" you...
-
ThunderEd was incorporated as a nonprofit organization on January 1, 20x1. The nonprofit dedicated to giving kids in middle school through high school knowledge and skills for economic success. The...
-
A, B, and C are partners with capital balances on December 31, 200D of P450,000, P450,000, and P300,000, respectively. Profits are shared equally. C wishes to withdraw and it is agreed that he is to...
-
A stock has had returns of 8 percent, 26 percent, 14 percent, 17 percent, 31 percent, and 1 percent over the last six years. What are the arithmetic and geometric average returns for the stock?
-
Repeat Exercise 6.54 with A = 3. Suppose that the random variable x has an exponential distribution with A = 2. a. Write the formula for the exponential probability curve of x. What are the possible...
-
In this exercise we study the nature of the interaction between x1, square footage, and x2, rating. a. Consider all houses with a rating of 2. In this case, predicted sales price is (using the least...
-
Again consider the automobile parts supplier situation. Remember that a problem-solving team will be assigned to rectify the process producing the cylindrical engine parts if the null hypothesis H0: ...
-
How does a judge decide a case when there is no precedent available in earlier decisions?
-
What is a class action and when do parties use them?
-
One of the major purposes of private law is to settle disputes between businesses. How can the settlement of a particular private dispute make a contribution to the business community as a whole?
Study smarter with the SolutionInn App