Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that contains...

60.1K

Verified Solution

Question

Programming

Learning Outcomes

  • Using Java, maintain a collection of objects using an array.

  • Construct a class that contains an array as a private instancevariable.

  • Construct methods with arrays as parameters and returnvalues.

  • Use partially filled arrays to implement a class where objectscan be dynamically added.

  • Implement searching and sorting algorithms.

Instructions

For this assignment you will be implementing an application thatmanages a music collection. The application will allow the user toadd albums to the collection and create a playlist of songs fromthat collection. The collection, albums, and playlist should all beimplemented using arrays.

The Song Class

The Song class has the following members:

  • Private instance variables for the title, artist, and length ofthe song in seconds.

  • A constructor that takes a title, artist, number of minutes, andnumber of seconds and creates a Song object. The length of the songis calculated by multiplying the number of minutes by 60 and addingthe number of seconds.

  • Accessor methods for the title, artist, and length.

  • A method called display that displays a song in the followingformat:

  • Title - Artist (Minutes:Seconds)

  • To display the seconds as a two-digit number you can use theformat code %02d. This will add a leading zero if the number ofseconds is less than 10.


The Album Class

The Album class has the following members:

  • Private instance variables for the title, artist, and tracklistof the album. The tracklist should be an array of Song objects.

  • A constructor that takes a title, artist, and an array of Songobjects and initializes the private instance variables. Thetracklist should be initialized by creating a new array and copyingall of the Song objects from the array passed as a parameter.

  • Accessor methods for the title and artist.

  • A method called getNumTracks that returns the number of trackson the album.

  • A method called getTrack that takes a track number between 1 andthe number of tracks, and returns the corresponding song. If thenumber provided is out of range, it returns null.

  • A method called comesBefore that takes another album as aparameter and returns true if the album the method was called oncomes before the album passed as a parameter. The order isdetermined by the artist first, then the title. UsecompareToIgnoreCase and equalsIgnoreCase to do the artist and titlecomparisons.

  • A method called displayAlbum that displays the title, artist,and number of tracks in the following format:

  • Title - Artist (NumTracks tracks)

  • If the number of tracks is one, it should display 1 track in theparentheses.

  • A method called displayTracklist that displays each track withthe corresponding track number before it. Use the format code %2dto align the track numbers to the right.

The AlbumCollection Class

The AlbumCollection class has the following members:

  • A public named constant for the maximum number of albums allowedin a collection.

  • Private instance variables for the number of albums in thecollection, and an array of Album objects.

  • A default constructor that initializes the number of albums tozero and the array to a new array that can store the maximum numberof albums allowed.

  • An accessor function for the number of albums.

  • A boolean method called addAlbum that takes an Album as aparameter. If the collection is full it returns false. Otherwise,it stores the album in the next open spot in the array, andincrements the number of albums.

  • A method called findAlbum that takes a title and artist asparameters and uses a sequential search to find and return thefirst album it finds with the given title and artist. If no albumis found, it returns null.

  • A void method called sortAlbums that sorts all of the albums inthe collection using selection sort, with the comesBefore methodfrom the Album class determining the sorting order.

  • A void method called displayAlbums that displays all of thealbums in the collection.

  • A void method called displaySongs that displays all of thealbums in the collection along with their track lists.

The Playlist Class

The Playlist class has the following members:

  • A public named constant for the maximum number of songs allowedin a playlist.

  • Private instance variables for the number of songs in theplaylist and an array of Song objects.

  • A default constructor that initializes the number of songs tozero and the array to a new array that can store the maximum numberof songs allowed.

  • An accessor function for the number of songs.

  • A method called getLength that returns the total length inseconds of the songs on the playlist.

  • A boolean method called addSong that takes an Song as aparameter. If the playlist is full, it returns false. Otherwise, itstores the song in the next open spot in the array and incrementsthe number of songs.

  • A void method called display that displays all of the songs inthe playlist, followed by the total length of the playlist inminutes and seconds.

  • A void method called clear that empties out the playlist bysetting the number of songs to 0. It does not have to go throughthe array and remove any songs.

The Main Class

The main class should be called YourlastnameProgram3 and has thefollowing members:

  • A private static void method called displayMenu that has noparameters and displays the following menu:

  • Choose one of the following:
    1. Add an album to the collection
    2. Display the albums in the collection
    3. Display the songs in the collection
    4. Sort the albums in the collection
    5. Add a song to the playlist
    6. Display the playlist
    7. Clear playlist
    8. Exit the program

  • A private static method called getChoice that takes noarguments, reads an integer from the user, and returns it.

  • A private static method called createAlbum that does thefollowing:

  1. Prompt the user for an album title.

  2. Prompt the user for an album artist.

  3. Prompt the user for the number of tracks on the album until theuser enters a value greater than zero.

  4. Call the getTracklist method to prompt the user for the trackson the album and get an array of Song objects.

  5. Create a new Album object with the title, artist, and track listprovided and return it.

  • A private static method called getTracklist that takes thenumber of tracks and the album artist as parameters and does thefollowing:

  1. Prompt the user for the title and length in minutes and secondsfor each track.

  2. Create a Song object for each track with the informationprovided and store it in an array of Song objects.

  3. Return the array of Song objects.

  • A private static method called getAlbumFromCollection that takesan AlbumCollection as a parameter and does the following:

  1. Prompt for the title and artist to search for.

  2. Call the findAlbum method to find the album in thecollection.

  3. If the album is not found, display a message indicating that thealbum is not in the collection, and go back to step 1.

  4. Otherwise return the album.

  • A private static method called getSongFromAlbum that takes anAlbum as a parameter and does the following:

  1. Display the tracklist for the album.

  2. Prompt the user to choose a track number.

  3. Call the getTrack method to get the song with the given tracknumber.

  4. If the track number is not valid, go back to step 2.

  5. Otherwise, return the song.

  • A main function that does the following:

  1. Create an empty AlbumCollection and an empty Playlist

  2. Display a welcome message.

  3. Call displayMenu to display the menu.

  4. Call getChoice to get the user’s choice.

  5. If the choice is 1, call createAlbum to create a new album andadd the album to the collection.

  6. If the choice is 2, display the albums in the collection.

  7. If the choice is 3, display the songs in the collection.

  8. If the choice is 4, sort the albums in the collection.

  9. If the choice is 5, call getAlbumFromCollection to select analbum, call getSongFromAlbum to select a song, and add the song tothe playlist.

  10. If the choice is 6, display the playlist.

  11. If the choice is 7, clear the playlist.

  12. If the choice is 8, display a thank you message.

  13. If the choice is any other value, display an error message.

  14. Go back to step 3 unless the choice was 8.

Answer & Explanation Solved by verified expert
4.3 Ratings (825 Votes)
import javautilScanner class Song private String title private String artist private int songLengthInSecs private int songMinutes private int songSeconds public SongString titleString    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students