2. Write a fully-documented class named Playlist that stores all SongRecord obje
ID: 3914102 • Letter: 2
Question
2. 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 SongRecord 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 SongRecord 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 SongRecord 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.
Explanation / Answer
package classProg;
// Defines class SongRecord
class SongRecord
{
// Instance variable to store song data
String title;
String artist;
int length;
// Default constructor definition
SongRecord()
{
title = artist = "";
length = 0;
}// End of default constructor
// Parameterized constructor definition to assign parameter value to instance variable
SongRecord(String ti, String ar, int len)
{
title = ti;
artist = ar;
length = len;
}// End of parameterized constructor
// Method to set title
void setTitle(String ti)
{
title = ti;
}// End of method
// Method to set artist
void setArtist(String ar)
{
artist = ar;
}// End of method
// Method to set length
void setLength(int len)
{
length = len;
}// End of method
// Method to return title
String getTitle()
{
return title;
}// End of method
// Method to return artist
String getArtist()
{
return artist;
}// End of method
// Method to return length
int getLength()
{
return length;
}// End of method
// Method to return song information
public String toString()
{
return " Title: " + getTitle() + " Artist: " + getArtist() + " Length: " + getLength();
}// End of method
}// End of class
// Class FullPlaylistException derived from class Throwable for exception handling
class FullPlaylistException extends Throwable
{
// Default constructor to display full message
FullPlaylistException()
{
System.out.println("Playlist is full");
}// End of constructor
}// End of class
// Class Playlist derived from StongRecord and implements Cloneable interface
public class Playlist extends SongRecord implements Cloneable
{
// Declares a final variable for maximum size of 50
static final int MAX = 50;
// Declares an array of objects for class SongRecord
SongRecord sr[];
// Declares a static counter for current size
static int currentSize;
// Default constructor definition to create array of objects
public Playlist()
{
// Dynamically allocates memory of size MAX
sr = new SongRecord[MAX];
// Assigns current size to zero
currentSize = 0;
}// End of default constructor
// Method to clone an object
public Object clone()throws CloneNotSupportedException
{
// Increase the current size by one
currentSize++;
// Returns the clone object
return super.clone();
}// End of method
// Method to compare two objects returns true if same otherwise returns false
public boolean equals (Object obj)
{
// Checks if the object is compared with itself then return true
if (obj == this)
{
return true;
}// End of if condition
// Checks if o is an instance of Record or not "null instance of [type]" also returns false
if (!(obj instanceof SongRecord))
{
return false;
}// End of if condition
// Type casts obj object to SongRecord
SongRecord c = (SongRecord) obj;
// Compare the data members and return true if they are equal or return false
return (title.compareTo(c.title) == 0 && artist.compareTo(c.artist) == 0 && Integer.compare(length, c.length) == 0);
}// End of method
// Method to return current size
public int size()
{
return currentSize;
}// End of method
// Method to add a song to playlist at specified position given as parameter
public void addSong(SongRecord song, int position)
{
// try block begin
try
{
// Checks if the position is less than zero or greater than the current size throw an IllegalArgumentException() exception
if(position < 0 || position > currentSize)
throw new IllegalArgumentException();
// Checks if current size is equals to maximum size then throw FullPlaylistException() exception
if(currentSize == MAX)
throw new FullPlaylistException();
// Loops from current size to the position given as parameter
for(int x = currentSize; x >= position; x--)
// Shifts the objects to one position right
sr[x+1] = sr[x];
// Inserts the new song object to the specified position
sr[position] = song;
// Increase the current counter by one
currentSize++;
}// End of try block
// Catch block to handle IllegalArgumentException
catch(IllegalArgumentException ie)
{
System.out.println(" Invalid position to insert -> " + position);
}// End of catch block
// Catch block to handle FullPlaylistException
catch(FullPlaylistException ie)
{
System.out.println(" Cannot Insert.");
}// End of catch block
}// End of method
// Method to remove a song from the play list at the given position as parameter
public void removeSong(int position)
{
// try block begin
try
{
// Checks if the position is less than zero or greater than the current size throw an IllegalArgumentException() exception
if(position < 0 || position > currentSize)
throw new IllegalArgumentException();
// Loops from position value till current size
for(int x = position; x < currentSize; x++)
// Shifts objects back
sr[x] = sr[x+1];
// Decrease the current counter by one
currentSize--;
}// End of try block
// Catch block to handle IllegalArgumentException
catch(IllegalArgumentException ie)
{
System.out.println(" Invalid position to delete -> " + position);
}// End of catch block
}// End of method
// Method to return the song object of given position as parameter
public SongRecord getSong(int position)
{
// try block begins
try
{
// Checks if the position is less than zero or greater than the current size throw an IllegalArgumentException() exception
if(position < 0 || position > currentSize)
throw new IllegalArgumentException();
}// End of try block
// Catch block to handle IllegalArgumentException
catch(IllegalArgumentException ie)
{
System.out.println(" Invalid position.");
}// End of catch block
// Returns the object at index position given as parameter
return sr[position];
}// End of method
// Method to display all the play list
public void printAllSongs()
{
// Loops till current size
for(int x = 0; x < currentSize; x++)
System.out.print(sr[x]);
}// End of method
// main method definition
public static void main(String[] args)
{
// Creates an array for song title
String title[] = {"Dil", "Danger", "Burning Train", "Horror Night"};
// Creates an array for artist
String artist[] = {"Sarukh", "Amit", "Kajol", "Saif"};
// Creates an array for song length
int len[] = {10, 2, 5, 9};
// Declares an Play list class object
Playlist pl = new Playlist();
// Loops till length of the title array
for(int x = 0; x < title.length; x++)
// Creates an object of class SongRecord using parameterized constructor
// Calls the method to add the object
pl.addSong(new SongRecord(title[x], artist[x], len[x]), x);
// Calls the method to display the complete play list
pl.printAllSongs();
// Calls the method to add a song at 10th position
pl.addSong(new SongRecord("Majboor", "Anil", 6), 10);
System.out.print(" After inserting at 2nd position ");
// Calls the method to add a song at 2nd position
pl.addSong(new SongRecord("Majboor", "Anil", 6), 2);
// Calls the method to display the complete play list
pl.printAllSongs();
System.out.print(" After removing at 3rd position ");
// Calls the method to remove a song at 3rd position
pl.removeSong(3);
// Calls the method to display the complete play list
pl.printAllSongs();
}// End of main method
}// End of driver class
Sample Output:
Title: Dil Artist: Sarukh Length: 10
Title: Danger Artist: Amit Length: 2
Title: Burning Train Artist: Kajol Length: 5
Title: Horror Night Artist: Saif Length: 9
Invalid position to insert -> 10
After inserting at 2nd position
Title: Dil Artist: Sarukh Length: 10
Title: Danger Artist: Amit Length: 2
Title: Majboor Artist: Anil Length: 6
Title: Burning Train Artist: Kajol Length: 5
Title: Horror Night Artist: Saif Length: 9
After removing at 3rd position
Title: Dil Artist: Sarukh Length: 10
Title: Danger Artist: Amit Length: 2
Title: Majboor Artist: Anil Length: 6
Title: Horror Night Artist: Saif Length: 9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.