Untitled
unknown
plain_text
9 months ago
11 kB
6
Indexable
Write a fully-documented class named Playlist that stores all SongRecord objects that belong to a particular playlist. The SongRecord objects should be stored in an array. There should be a maximum of 50 SongRecord objects allowed, a number which should be defined as a final variable. The class will be based on the following ADT specification: public class Playlist The Playlist class implements an abstract data type for a playlist of audio files supporting common operations on such lists of audio files. Constructor for Playlist public Playlist() Construct an instance of the Playlist class with no SongRecord objects in it. Postcondition: This Playlist has been initialized to an empty list of SongRecords. clone public Object clone() Generate a copy of this Playlist. Returns: The return value is a copy of this Playlist. Subsequent changes to the copy will not affect the original, nor vice versa. Note that the return value must be typecast to a Playlist before it can be used. equals public boolean equals (Object obj) Compare this Playlist to another object for equality. Parameters: obj - an object in which this Playlist is compared Returns: A return value of true indicates that obj refers to a Playlist object with the same SongRecords in the same order as this Playlist. Otherwise, the return value is false. Note: If obj is null or it is not a Playlist object, then the return value is false. Note: When comparing equality between two SongRecord objects, you must verify that their titles, artists, and song lengths are all the same. Using the == operator will simply check to see if the two variables refer to the same SongRecord object, which does not take into consideration that two different SongRecord objects can actually represent the same audio file. To solve this problem, you can either check that each of the properties of the two objects are the same (title, artist, and length) inside of this method, or you may simplify this process by implementing an equals method (similar to this one) for the SongRecord class. size public int size() Determines the number of SongRecords currently in this Playlist. Preconditions: This PlayList object has been instantiated. Returns: The number of SongRecords in this Playlist. addSong public void addSong(SongRecord song, int position) Parameters: song - the new SongRecord object to add to this Playlist position - the position in the playlist where the song will be inserted Preconditions: This SongRecord object has been instantiated and 1 < position < songs_currently_in_playlist + 1. The number of SongRecord objects in this Playlist is less than max_songs. Postcondition: The new SongRecord is now stored at the desired position in the Playlist. All SongRecords that were originally in positions greater than or equal to position are moved back one position. (Ex: If there are 5 songs in a Playlist, positions 1-5, and you insert a new SongRecord at position 4, the new SongRecord will now be at position 4, the SongRecord that was at position 4 will be moved to position 5, and the SongRecord that was at position 5 will be moved to position 6). Throws: IllegalArgumentException Indicates that position is not within the valid range. FullPlaylistException Indicates that there is no more room inside of the Playlist to store the new SongRecord object. Note 1: position refers to the position in the Playlist and not the position inside the array. Note 2: Inserting a song to position (songs_currently_in_playlist + 1) is effectively the same as adding a song to the end of the Playlist. removeSong public void removeSong(int position) Parameters: position - the position in the playlist where the song will be removed from. Preconditions: This PlayList object has been instantiated and 1 < position < songs_currently_in_playlist. Postcondition: The SongRecord at the desired position in the Playlist has been removed. All SongRecords that were originally in positions greater than or equal to position are moved forward one position. (Ex: If there are 5 songs in a Playlist, positions 1-5, and you remove the SongRecord at position 4, the SongRecord that was at position 5 will be moved to position 4). Throws: IllegalArgumentException Indicates that position is not within the valid range. Note: position refers to the position in the Playlist and not the position inside the array. getSong public SongRecord getSong(int position) Get the SongRecord at the given position in this Playlist object. Parameters: position - position of the SongRecord to retrieve Preconditions: This Playlist object has been instantiated and 1 < position < songs_currently_in_playlist. Returns: The SongRecord at the specified position in this Playlist object. Throws: IllegalArgumentException Indicates that position is not within the valid range. Note: position refers to the position in the Playlist and not the position inside the array. printAllSongs public void printAllSongs() Prints a neatly formatted table of each SongRecord in the Playlist on its own line with its position number as shown in the sample output. Preconditions: This PlayList object has been instantiated. Postcondition: A neatly formatted table of each SongRecord in the Playlist on its own line with its position number has been displayed to the user. Note: position refers to the position in the Playlist and not the position inside the array. Hint: If your toString() method is implemented correctly as described below, you will simply need to call it and print the results to the user. getSongsByArtist public static Playlist getSongsByArtist(Playlist originalList, String artist) Generates a new Playlist containing all SongRecords in the original Playlist performed by the specified artist. Parameters: originalList - the original Playlist artist - the name of the artist Preconditions: The Playlist referred to by originalList has been instantiated. Returns: A new Playlist object containing all SongRecords in the original Playlist performed by the specified artist. Note: The return value is null if either originalList or artist is null. Note: The order of the SongRecords in the new Playlist should relate to the order of the SongRecords in the old Playlist. For example, if the original Playlist has 8 SongRecords, positions 1-8, and SongRecords 3, 6, and 8 were performed by the specified artist, the new Playlist should have the SongRecord originally at position 3 placed at location 1, the SongRecord originally at position 6 placed at location 2, and the SongRecord originally at position 8 placed at location 3. toString public String toString() Gets the String representation of this Playlist object, which is a neatly formatted table of each SongRecord in the Playlist on its own line with its position number as shown in the sample output. Returns: The String representation of this Playlist object. Note: Position refers to the position in the Playlist and not the position inside the array. Solution Expert-verified 1st step All steps Answer only Step 1 Here's the Java class Playlist that implements the specified ADT: Step 2 public class Playlist { private static final int MAX_SONGS = 50; private SongRecord[] songRecords; private int songsCurrentlyInPlaylist; // Constructor for Playlist public Playlist() { songRecords = new SongRecord[MAX_SONGS]; songsCurrentlyInPlaylist = 0; } // Generate a copy of this Playlist public Object clone() { Playlist copy = new Playlist(); for (int i = 0; i < songsCurrentlyInPlaylist; i++) { copy.addSong(this.songRecords[i].clone(), i + 1); } return copy; } // Compare this Playlist to another object for equality public boolean equals(Object obj) { if (obj == null || !(obj instanceof Playlist)) { return false; } Playlist other = (Playlist) obj; if (this.songsCurrentlyInPlaylist != other.songsCurrentlyInPlaylist) { return false; } for (int i = 0; i < songsCurrentlyInPlaylist; i++) { if (!this.songRecords[i].equals(other.songRecords[i])) { return false; } } return true; } // Determines the number of SongRecords currently in this Playlist public int size() { return songsCurrentlyInPlaylist; } // Add a new SongRecord to this Playlist at a specific position public void addSong(SongRecord song, int position) { if (position < 1 || position > songsCurrentlyInPlaylist + 1) { throw new IllegalArgumentException("Invalid position."); } if (songsCurrentlyInPlaylist >= MAX_SONGS) { throw new FullPlaylistException("No more room to add songs."); } for (int i = songsCurrentlyInPlaylist - 1; i >= position - 1; i--) { songRecords[i + 1] = songRecords[i]; } songRecords[position - 1] = song; songsCurrentlyInPlaylist++; } // Remove the SongRecord at a specific position from this Playlist public void removeSong(int position) { if (position < 1 || position > songsCurrentlyInPlaylist) { throw new IllegalArgumentException("Invalid position."); } for (int i = position; i < songsCurrentlyInPlaylist; i++) { songRecords[i - 1] = songRecords[i]; } songRecords[songsCurrentlyInPlaylist - 1] = null; songsCurrentlyInPlaylist--; } // Get the SongRecord at the specified position in this Playlist public SongRecord getSong(int position) { if (position < 1 || position > songsCurrentlyInPlaylist) { throw new IllegalArgumentException("Invalid position."); } return songRecords[position - 1]; } // Print all SongRecords in this Playlist public void printAllSongs() { for (int i = 0; i < songsCurrentlyInPlaylist; i++) { System.out.println("Position " + (i + 1) + ": " + songRecords[i]); } } // Generate a new Playlist containing all SongRecords by the specified artist public static Playlist getSongsByArtist(Playlist originalList, String artist) { if (originalList == null || artist == null) { return null; } Playlist filteredList = new Playlist(); for (int i = 1; i <= originalList.size(); i++) { SongRecord song = originalList.getSong(i); if (song.getArtist().equals(artist)) { filteredList.addSong(song.clone(), filteredList.size() + 1); } } return filteredList; } // Get the String representation of this Playlist public String toString() { StringBuilder result = new StringBuilder(); for (int i = 0; i < songsCurrentlyInPlaylist; i++) { result.append("Position ").append(i + 1).append(": ").append(songRecords[i]).append("\n"); } return result.toString(); } }
Editor is loading...
Leave a Comment