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?
-
Identify the criteria for determining if a governmental or enterprise fund must be reported as a major fund. What other funds should or may be reported as major funds?
-
discuss the integration of advanced structural optimization algorithms with parametric design methodologies to create efficient and sustainable structures, and how these approaches are transforming...
-
The FBI computed the proportion of violent crimes in the United States falling into each of four categories. A simple random sample of 500 violent crimes committed in California were categorized in...
-
On January 1, 2012, Valuation Allowance for Trading Investments had a zero balance. On December 31, 2012, the cost of the trading securities portfolio was $ 105,800 and the fair value was $101,600....
-
a. issue price b. amount of premium amortization c. amount of the interest payment d. book value of the bonds TB Problem Qu. 10-130 (Algo) Grand Company Issued.. Grand Company issued $165,000 of...
-
5-1. A manufacturer of mountain bike will design and implement an automated system for welding bicycle frames. All works related to this project will be conducted within the company without any...
-
You are considering an investment of $10,000 in a mutual fund with a 2% front load and an annual expense ratio of 0.5%. You are also considering an investment in a bank CD paying 2% interest per...
-
In this discussion question you explain how XBRL is used to enhance financial reporting. What is XBRL? Why was XBRL developed? Are all companies required to use XBRL? What is XBRL GL? What is meant...
-
3. A square wire loop is at rest in a uniform magnetic field. The magnetic field, which is pointing into the page, is increasing in magnitude with time. As a result, a current is induced. In which...
-
Evaluate the following expression. [3+7x (14-8)]5 X
-
Find dx dy dx. y = (x + 5x) (x + sec x)5 2. Find Simplify your answer. y = 2x + y
-
Definition of WBS and its importance in project management Explanation of the benefits of using WBS Identification of potential challenges associated with creating and implementing WBS 3.Next,...
-
Consider the following limit xi lim n-x (x) +4 -AT [-1,2] (a) Represent it as a definite integral. (b) Can the definite integral represent the area of the region bounded by x = -1, x = 2, the x-axis,...
-
Which internal control principle is especially diffi cult for small organizations to implement? Why?
-
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: ...
-
Consider the purchases function of a manufacturing company. To overcome a downward profitability trend, management recently instituted a "just-in-time" system of acquiring raw materials for its...
-
Explain how creating a process map and internal threat analysis helps in determining the extent to which substantive testing is to be performed on the accounts associated with the supply chain and...
-
The following are routine procedures for the audit of the purchases process. For each procedure, (1) state whether it is a test of controls or a substantive test of transactions or balances, (2)...
Study smarter with the SolutionInn App