Implement the Record class. Add the following private instance variables to the Record class: A String representing
Fantastic news! We've Found the answer you've been seeking!
Question:
- Implement the Record class.
- Add the following private instance variables to the Record class:
- A String representing the title of the record.
- A String representing the record artist.
- An int representing the year the record was released.
- Add the following private instance variables to the Record class:
- Add a constructor that takes two Strings and an int and uses them to initialize the Record object. This class will not have a default constructor.
- Add an accessor method for each private instance variable. Mutators will not be necessary.
- Add a void method called display that displays the record information in the following format:
- Title - Artist (YearReleased)
- For example, the record Unknown Pleasures by Joy Division, released would be displayed as:
- Unknown Pleasures - Joy Division (1979)
- Add a boolean method called comesBefore that takes a Record object as a parameter and compares the Record the method was called on (this) with the Record passed as a parameter (record).
- If the artist of this comes before the artist of record alphabetically, return true. Use the compareToIgnoreCase method of the String class to determine this.
- If the record artists are the same, and the year this was released is earlier than the year record was released, return true.
- Otherwise return false.
- Add a unit test for the Record class to your main class.
- Write a static void method called testComesBefore that takes two Record objects as parameters and uses the comesBefore method to compare them.
- In the main method, create some Record objects and use testComesBefore to test three cases:
- The two records have different artists and the first one comes alphabetically before the second.
- The two records have the same artist and the first one was released before the second.
- The first record comes after the second record.
- Run the program to see if comesBefore works correctly.
- The output of the tests should look something like this:
- Testing comesBefore: Unknown Pleasures comes before Closer Testing comesBefore: New Day Rising comes before Unknown Pleasures Testing comesBefore: Closer does not come before Disintegration
- Implement the RecordCollection class.
- Add the following private instance variables to the RecordCollection class:
- An int representing the number of records in the record collection.
- An array of Record objects, representing the records in the collection.
- Add the following private instance variables to the RecordCollection class:
- Add a constructor for the RecordCollection class that takes an int representing the maximum number of records as a parameter and does the following:
- Initializes the number of records to zero.
- Initializes the array so its length is the maximum number of records.
- Add a public accessor method for the number of records.
- Add a method called display that displays all of the records in the collection. If there are no records in the collection, display the following:
- No records in the collection
- Add a unit test for the display method of the RecordCollection class to your main class.
- Write a static void method called testRCDisplay that takes a RecordCollection as a parameter, calls display on it, uses getNumRecords to get the number of records in the collection, and displays that as well.
- Add code to the main function to create a RecordCollection object and pass it to textRCDisplay.
- Run the program to see if display works correctly.
- The output of the test should look something like this:
- Testing display: No records in the collection Number of records: 0
- Implement a boolean method called addRecord to the RecordCollection that does the following:
- The method should take a Record as a parameter.
- If the record is null or the array is full return false.
- Otherwise, add the record to the next open spot, increment the number of records, and return true.
- Add a unit test for the addRecord method to your main class.
- Write a static void method called testAddRecord that takes a RecordCollection and a Record as parameters, uses the addRecord to add the record to the collection, displays the collection, and displays the number of records in the collection.
- Add code in the main function to call testAddRecord several times to add several Record objects.
- Run your program to see if the addRecord method works correctly.
- The output should look something like this:
- Testing addRecord Adding Unknown Pleasures - Joy Division (1979) Collection: Unknown Pleasures - Joy Division (1979) Number of records: 1 Testing addRecord Adding Closer - Joy Division (1980) Collection: Unknown Pleasures - Joy Division (1979) Closer - Joy Division (1980) Number of records: 2 Testing addRecord Adding Disintegration - Cure, the (1989) Collection: Unknown Pleasures - Joy Division (1979) Closer - Joy Division (1980) Disintegration - Cure, the (1989) Number of records: 3 Testing addRecord Adding New Day Rising - Husker Du (1985) Collection: Unknown Pleasures - Joy Division (1979) Closer - Joy Division (1980) Disintegration - Cure, the (1989) New Day Rising - Husker Du (1985) Number of records: 4
- Implement a method called findRecord to the RecordCollection class that does the following:
- The method should take a String representing a title and a String representing an artist as parameters.
- Start at the beginning of the array.
- Loop until the record with the given title and artist is found or the end of the array is reached.
- If a record is found, return it.
- Otherwise return null
- Add a unit test for the findRecord method to your main class.
- Write a static void method called testFindRecord that takes a RecordCollection, and two String objects representing a title and an artist.
- The method should display the title and artist, then use the findRecord method to try to find a record with those attributes.
- Add calls to testFindRecord to the main function to test the following cases:
- Use the title and artist of a record in the collection.
- Use the title and artist of a record not in the collection.
- Run your program to see if the findRecord method works correctly.
- The output should look something like this:
- Testing findRecord Title: New Day Rising Artist: Husker Du Found: New Day Rising - Husker Du (1985) Testing findRecord Title: Nevermind Artist: Nirvana Record not found
- Implement a void method called sortRecords to the RecordCollection class that does the following:
- Perform a selection sort to sort the records in the array.
- Use the comesBefore method to determine if one record is "smaller" than another record.
- Make sure only to search the locations that have valid entries.
- Add a unit test for the sortRecords method to your main class.
- Write a static void method called testSortRecords that takes a RecordCollection, calls the sortRecords method, and displays the collection.
- Add a call to testSortRecords to your main method.
- Run your program to see if the sortRecords method works correctly.
- The output should look something like this:
- Testing sortRecords Disintegration - Cure, the (1989) New Day Rising - Husker Du (1985) Unknown Pleasures - Joy Division (1979) Closer - Joy Division (1980)
Related Book For
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch
Posted Date: