I need help writing the methods for the song and playList cl

I need help writing the methods for the song and playList class in Java. I already have the interface and the getters and setter. Can someone help with the methods?

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, 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 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.

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.

Solution

Song class


public class Song implements Comparable<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)
   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(s.artist, s.title, s.minutes, s.seconds);
   }
   public boolean equals(Object o) // a song is equal if all four fields are equal
   {
       Song s=(Song)o;
       if(this.artist.equals(s.artist)&&this.title.equals(s.title)&&this.minutes==s.minutes&&this.seconds==s.seconds)
           return true;
       return false;
      
   }
   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);
       }
   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;
   }
   @Override
   public int compareTo(Song arg0) {
       int a=artist.compareTo(arg0.artist);
       if(a==0)
       {
           a=title.compareTo(arg0.title);
       }
       return a;
   }

}

PlayList class

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;

public class PlayList {
   private String name; // contains the name of the playlist
   private ArrayList<Song> playableList; // ArrayList of Playable elements that make up the play list
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public ArrayList getPlayableList() {
       return playableList;
   }
   public void setPlayableList(ArrayList playableList) {
       this.playableList = playableList;
   }
   public PlayList()
   {
       name=\"\";
       playableList=new ArrayList<Song>();
   }
   public PlayList(String newName)
   {
       name=newName;
       playableList=new ArrayList<Song>();
   }
   public boolean loadSongs(String fileName) throws FileNotFoundException
   {
       FileReader fr = new FileReader(fileName);
       Scanner sc = new Scanner(fr);
       while(sc.hasNextLine())
       {
       String data = sc.nextLine();
       String[] value = data.split(\",\");
       Song s = new Song(value[0], value[1], Integer.valueOf(value[2]),Integer.valueOf(value[3]));
       addSong(s);
       }
       return true;
      
   }
   public boolean clear() // removes all playable elements in the play list
   {
       playableList.clear();
       if(playableList.isEmpty())
           return true;
       return false;
      
   }
   public boolean addSong(Song s) // adds Song s to the end of the play list
   {
       playableList.add(s);
       return false;
   }
   public Song removePlayable(int index) // removes Playable element at index from the list and returns it{
   {
       Song s=(Song) playableList.remove(index);
       return s;
   }
   public Song removePlayable(Song p) // removes every occurrence of Playable p from the list and returns p
   {
       playableList.remove(p);
       return p;
   }
   public Song getPlayable(int index) // returns the Playable element at the appropriate index
   {
       Song s=playableList.get(index);
       return s;
   }
   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.
   {
       playableList.addAll(pl.getPlayableList());
       return false;
   }
   public void play() // plays the play list by calling play() on each item in the play list in order
   {
       Iterator< Song> ir=playableList.iterator();
       while(ir.hasNext())
       {
           ir.next().play();
       }
      
   }
   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.
   {
       playableList.size();
       return 0;
   }
   void sortByTime()
   {
       Collections.sort(playableList, new SortByTime());
   }
   void sortByArtistName()
   {
       Collections.sort(playableList, new Sortbyname());
   }
  
  
   @Override
   public String toString() {
       return \"PlayList [name=\" + name + \", playableList=\" + playableList + \"]\";
   }

}

SortByName class

import java.util.Comparator;
/**
* implement comparator to sort volume by name
*/
public class Sortbyname implements Comparator<Song> {

   @Override
   public int compare(Song o1, Song o2) {

       return o1.getArtist().compareTo(o2.getArtist());
   }
}

SortByTime class

import java.util.Comparator;

public class SortByTime implements Comparator<Song> {

   @Override
   public int compare(Song arg0, Song arg1) {
       if(arg0.getMinutes()>arg1.getMinutes())
           return 1;
       else if(arg0.getMinutes()<arg1.getMinutes())  
           return -1;
       else if(arg0.getSeconds()>arg1.getSeconds())
           return 1;
       else if(arg0.getSeconds()<arg1.getSeconds())
           return -1;
       return 0;
   }

}

SongDetails file

Arjit,abc,12,3
lata,xyz,4,3
aassad,fsdf,3,5

PlayListTester class

import java.io.FileNotFoundException;

public class PlayListTester {

   public static void main(String[] args) throws FileNotFoundException {
       PlayList pl=new PlayList(\"Test\");
       pl.loadSongs(\"SongDetails\");
       pl.play();
       Song s=new Song(\"a\",\"x\",2,8);
       pl.addSong(s);
       System.out.println(pl);
       pl.sortByArtistName();
       System.out.println(\"List after Sort by name\");
       System.out.println(pl);
       pl.sortByTime();
       System.out.println(\"List after sort by time\");
       System.out.println(pl);
   }

}

Output:

Playing Song: artist = Arjit title = abc
Playing Song: artist = lata title = xyz
Playing Song: artist = aassad title = fsdf
PlayList [name=Test, playableList=[{Song: title = abc artist = Arjit}, {Song: title = xyz artist = lata}, {Song: title = fsdf artist = aassad}, {Song: title = x artist = a}]]
List after Sort by name
PlayList [name=Test, playableList=[{Song: title = abc artist = Arjit}, {Song: title = x artist = a}, {Song: title = fsdf artist = aassad}, {Song: title = xyz artist = lata}]]
List after sort by time
PlayList [name=Test, playableList=[{Song: title = x artist = a}, {Song: title = fsdf artist = aassad}, {Song: title = xyz artist = lata}, {Song: title = abc artist = Arjit}]]

I need help writing the methods for the song and playList class in Java. I already have the interface and the getters and setter. Can someone help with the meth
I need help writing the methods for the song and playList class in Java. I already have the interface and the getters and setter. Can someone help with the meth
I need help writing the methods for the song and playList class in Java. I already have the interface and the getters and setter. Can someone help with the meth
I need help writing the methods for the song and playList class in Java. I already have the interface and the getters and setter. Can someone help with the meth
I need help writing the methods for the song and playList class in Java. I already have the interface and the getters and setter. Can someone help with the meth
I need help writing the methods for the song and playList class in Java. I already have the interface and the getters and setter. Can someone help with the meth

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site