Question
Please please please help with Java and especially Junit Tests!!!! API to Implement For this homework, create: a Playable interface a PlayList class, and a
Please please please help with Java and especially Junit Tests!!!!
API to Implement
For this homework, create:
a Playable interface
a PlayList class, and
a Song class
They must meet the APIs listed below. You may add additional methods to each class if you wish (not required).
Playable Interface
public interface Playable
Create a Java interface named Playable that includes the following methods (and only the following methods):
public void play();
public String getName(); // returns the name (for PlayList) or title (for Song) of Playable object.
public int getPlayTimeSeconds(); // For Song: returns the number of seconds in the song. For PlayList: returns the number of seconds in the entire PlayList
public int numberOfSongs(); // For Song: returns 1. For PlayList returns the number of songs in the playlist and all playlists contained within.
Note: when writing getPlayTimeSeconds() and numberOfSongs() in the playlist class it can help to know that since both Playlist and Song implement these methods it doesn't matter what the type of the object is when calling this method.
Both the Song and PlayList classes will implement this interface.
Song Class
public class Song
private String artist // the artist performing the song
private String title // the title of the song
private int minutes // number of min in length
private int seconds // number of seconds of the song (always less than 60)
// Add Getters / Setters for all fields
// Add the following three constructors (remember to initialize ALL fields in each of the constructors - regardless of the number of parameters!)
public Song(String artist, String title)
public Song(String artist, String title, int minutes, int seconds)
public Song(Song s)
public boolean equals(Object o) // two songs are equal if all four fields are equal
public String toString() { // Use this code for toString EXACTLY
return "{Song: title = " + title + " artist = " + artist + "}";
}
public void play() { // Use this code for play EXACTLY
System.out.printf("Playing Song: artist = %-20s title = %s ", artist, title);
}
Make the Song class implement the interface Comparable, and make the ordering criteria as follows: Songs will be ordered by the artist in ascending order. If the artists are the same, then by title in ascending order. If both artist and title are the same, then any order is acceptable.
Dont forget to include any methods needed to implement the Playable interface.
PlayList Class
public class PlayList
private String name // contains the name of the playlist
private ArrayList playableList // ArrayList of Playable elements that make up the playlist
Methods that modify the list should return a boolean: return true if they successfully change the list, otherwise, they return false. Note that a PlayList can contain other PlayLists!
Methods that return a Playable element return null if they fail to find the song in question.
// Add Getters / Setters for name and playableList
// Add a toString() method that returns the name of the playlist followed by its contents (by calling toString() on each item it contains). You should only have one name/title per line
// Add the following two constructors (remember to initialize ALL fields in each of the constructors - regardless of the number of parameters!)
public PlayList() // empty playlist named "Untitled"
public PlayList(String newName) // empty playlist
public boolean loadSongs(String fileName) // loads songs from file, more on this below
public boolean clear() // removes all playable elements in the playlist
public boolean addSong(Song s) // adds Song s to the end of the play list
public Playable removePlayable(int index) // removes Playable element at index from the list and returns it
public Playable removePlayable(Playable p) // removes every occurrence of Playable p from the list and returns p
public Playable getPlayable(int index) // returns the Playable element at the appropriate index
public boolean addPlayList(PlayList pl) //adds the PlayList that is being passed to the end of the playableList and returns true unless the playableList already contains a playlist with the same name, in this case, do not add the playlist and return false. Playlists are uniquely identified by name, so your program should not allow there to be two playlists with the same name.
public void play() // plays the playlist by calling play() on each item in the playlist in order
Write a method in PlayList called sortByName(). This method will sort the Playable items by the value returned by getName() in ascending order.
You must use a comparator object with Collections.sort() to achieve this. The interface to implement is Comparator.
Also add a method sortByTime() in PlayList that sorts by the Song or PlayList's total time in seconds, in ascending order (shortest first). It also must use a comparator object to achieve this. That object will use one of the methods in the Playable interface to get the time for each object.
Dont forget to include any methods needed to implement the Playable interface.
Data Input Format for PlayList.loadSongs() :
The data file that loadSongs() must process will have a simple format. There will be a total of 4 lines for each song, where the fourth line is always blank. (This means there will be one blank line at the end of the file.) The 1st line will be the song title; the 2nd line will be the artist; and the 3rd line will be the song's duration in the form M:SS or MM:SS.
Note that artist and title may be made up of multiple words separated by blanks. You must remove any leading or trailing blanks from any of these lines, if they are found.
Regarding the time format, there will not be any spaces inside the time-value. Either the minutes or seconds may be more or less than two characters. If the integer value for seconds is >= 60, then your code must determine the proper values of minutes and seconds-less-than-60 to store in the Song object.
Below is an example file. Be sure to create your own simple test files to test all possible wrinkles. Note the 2nd song has two lines that have leading spaces. Note that the 2nd song has a time-value of 1:135, which should be stored in the Song object as 3 minutes and 15 seconds. Note there is a final blank line in the file.
Dancing Queen Abba 4:13 Call Me Maybe Artie Jesperson and his Marching Polka Funk Ensemble 1:135 |
Testing
You will need to write at least 2 JUnit tests for each method above, this includes methods required for the Playable interface but exculdes getters and setters (no tests required for getters and setters although you can write some if you would like!) You will need to submit these to Web-Cat along with the rest of your code. Use standard naming conventions for these JUnit tests, however, we do not mind what you call them. Remember, it would be best if you do not test more than one failure case per JUnit test case (method). There is no upper limit on how many JUnit test cases you write. Place all your JUnit test cases in one single file - you do not need separate files to test.
Make sure your JUnit test file has the word 'test' in it (e.g. TestHW4.java or PlayListTest.java, etc.)
Constraints
Do not put your files into a Java package (the word "package" should not appear at the beginning of your code). Do not throw exceptions in your methods unless the method specifications say you should (use Try-Catch instead).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started