Need help with this java project as the methods are proving
Need help with this java project, as the methods are proving to be pretty challenging. Im not worried about the fields, getters, setters, constructors, ect as those are pretty straightforward, but any and all help with the methods would be greatly appreciated!
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
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 length of the song (always less than 60)
// Add Getters / Setters for all fields
// 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) // a song is 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<Song>, and make the ordering criteria as follows: Songs will be ordered by 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.
Don’t forget to include any methods needed to implement the Playable interface.
PlayList Class
public class 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.
private String name // contains the name of the playlist
private ArrayList<Playable> playableList // ArrayList of Playable elements that make up the play list
// Add Getters / Setters for name and songList
// 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
// Two constructors (remember to initialize ALL fields in each of the constructors - regardless of the number of parameters!)
public PlayList() // empty play list named \"Untitled\"
public PlayList(String newName) // empty play list
public boolean loadSongs(String fileName) // loads songs from file, more on this below
public boolean clear() // removes all playable elements in the play list
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 play list by calling play() on each item in the play list in order
public int size() // returns the number of songs in the play list and all songs contained in any of the nested play lists. For example if p1 is a playlist that contains songs s1, s2 and s3 as well as play list p2. Also, suppose that p2 contains songs s4, s5, s6, and s7, then if I called size on p1 it should return 7.
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<Playable>.
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.
Don’t forget to include any methods needed to implement the Playable interface.
Solution
public interface Playable {
// methods declaring
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.
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Song implements Comparable<Song>, Playable{
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 length of the song (always less than 60)
public Song(String artist, String title) {
this.artist= artist;
this.title = title;
}
public Song(String artist, String title, int minutes, int seconds) {
this.artist= artist;
this.title = title;
this.minutes = minutes;
this.seconds = seconds;
}
public Song(Song s) {
this.artist= s.artist;
this.title = s.title;
this.minutes = s.minutes;
this.seconds = s.seconds;
return;
}
public int compareTo(Song s){
if((artist.equals(s.artist)) && title.equals(s.title) && (minutes == s.minutes) && (seconds == s.seconds)){
return 0;
}
return 1;
}
public boolean equals(Song o) { // a song is equal if all four fields are equal
if(compareTo(o) == 0){
return true;
}
return false;
}
//getter setter methods
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
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);
}
@Override
public String getName() {
return artist;
}
@Override
public int getPlayTimeSeconds() {
return (minutes*60) + seconds;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
public class PlayList implements Playable, Comparator<Playable> {
private String name; // contains the name of the playlist
private ArrayList<Playable> playableList; // ArrayList of Playable elements that make up the play list
// constructor
public PlayList() { // empty play list named \"Untitled\"
this.name = \"Untitled\";
playableList = new ArrayList<Playable>();
}
public PlayList(String newName) { // empty play list
this.name = newName;
playableList = new ArrayList<Playable>();
}
public boolean loadSongs(String fileName) {
return true;
}
@Override
public void play() {
System.out.println(\"Now playing: \" + name);
}
@Override
public String getName() {
return name;
}
public int compareTo(Playable s) {
if (name.equals(s.getName())) {
return 0;
} else if (name.compareTo(s.getName()) == 1) {
return 1;
}
return -1;
}
//clear method
public boolean clear() {
this.name = \"\";
playableList.removeAll(playableList);
return true;
}
//adding song
public boolean addSong(Song s){
playableList.add(s);
return true;
}
public Playable removePlayable(int index){ // removes Playable element at index from the list and returns it
Playable obj = playableList.get(index);
playableList.remove(index);
return obj;
}
public Playable removePlayable(Playable p){
playableList.remove(p);
return null;
}
public Playable getPlayable(int index) {
return playableList.get(index);
}
public boolean addPlayList(PlayList pl) {
if(playableList.contains(pl)){
return false;
}
this.name = pl.name;
this.playableList = pl.playableList;
return true;
}
@Override
public int getPlayTimeSeconds() {
throw new UnsupportedOperationException(\"Not supported yet.\"); //To change body of generated methods, choose Tools | Templates.
}
@Override
public int compare(Playable o1, Playable o2) {
throw new UnsupportedOperationException(\"Not supported yet.\"); //To change body of generated methods, choose Tools | Templates.
}
}





