SlideShare a Scribd company logo
1 of 8
Download to read offline
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 {
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 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();
}
public PlayList(String newName)
{
name=newName;
playableList=new ArrayList();
}
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 {
@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 {
@Override
public int compare(Song arg0, Song arg1) {
if(arg0.getMinutes()>arg1.getMinutes())
return 1;
else if(arg0.getMinutes()arg1.getSeconds())
return 1;
else if(arg0.getSeconds()

More Related Content

Similar to I need help writing the methods for the song and playList class in J.pdf

8.8 Program Playlist (Java)You will be building a linked list. Ma.pdf
8.8 Program Playlist (Java)You will be building a linked list. Ma.pdf8.8 Program Playlist (Java)You will be building a linked list. Ma.pdf
8.8 Program Playlist (Java)You will be building a linked list. Ma.pdf
ARCHANASTOREKOTA
 
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdfHi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
apleathers
 
It's not working what am I doing wrong- Given main()- complete the Son.pdf
It's not working what am I doing wrong- Given main()- complete the Son.pdfIt's not working what am I doing wrong- Given main()- complete the Son.pdf
It's not working what am I doing wrong- Given main()- complete the Son.pdf
aanyajoshi90
 
Here is the code.compile g++ Playlist.cpp main.cppPlaylist.h.pdf
Here is the code.compile  g++ Playlist.cpp main.cppPlaylist.h.pdfHere is the code.compile  g++ Playlist.cpp main.cppPlaylist.h.pdf
Here is the code.compile g++ Playlist.cpp main.cppPlaylist.h.pdf
ANANDSALESINDIA105
 
I need help writing test Codepackage org.example;import j.pdf
I need help writing test Codepackage org.example;import j.pdfI need help writing test Codepackage org.example;import j.pdf
I need help writing test Codepackage org.example;import j.pdf
mail931892
 
Many of us have large digital music collections that are not always .pdf
Many of us have large digital music collections that are not always .pdfMany of us have large digital music collections that are not always .pdf
Many of us have large digital music collections that are not always .pdf
fazanmobiles
 
816 LAB Playlist output linked list Hey I have most of.pdf
816 LAB Playlist output linked list Hey I have most of.pdf816 LAB Playlist output linked list Hey I have most of.pdf
816 LAB Playlist output linked list Hey I have most of.pdf
sastaindin
 
OverviewThis project will allow you to write a program to get mo.docx
OverviewThis project will allow you to write a program to get mo.docxOverviewThis project will allow you to write a program to get mo.docx
OverviewThis project will allow you to write a program to get mo.docx
jacksnathalie
 
Given main(), complete the SongNode class to include the printSong.pdf
Given main(), complete the SongNode class to include the printSong.pdfGiven main(), complete the SongNode class to include the printSong.pdf
Given main(), complete the SongNode class to include the printSong.pdf
illyasraja7
 
You are required to implement all the classes as specified by UML di.pdf
You are required to implement all the classes as specified by UML di.pdfYou are required to implement all the classes as specified by UML di.pdf
You are required to implement all the classes as specified by UML di.pdf
arorasales234
 
maincpp include ltiostreamgt include ltstringgt.pdf
maincpp  include ltiostreamgt include ltstringgt.pdfmaincpp  include ltiostreamgt include ltstringgt.pdf
maincpp include ltiostreamgt include ltstringgt.pdf
mukulsingh0025
 
In C++ please I need output please You will be building a .pdf
In C++ please I need output please   You will be building a .pdfIn C++ please I need output please   You will be building a .pdf
In C++ please I need output please You will be building a .pdf
sunilverma8487
 
819 LAB Program Playlist c++ You will be building a li.pdf
819 LAB Program Playlist c++ You will be building a li.pdf819 LAB Program Playlist c++ You will be building a li.pdf
819 LAB Program Playlist c++ You will be building a li.pdf
meenaaarika
 
The provided codetrack_file_handling.rb class Track att.pdf
The provided codetrack_file_handling.rb class Track    att.pdfThe provided codetrack_file_handling.rb class Track    att.pdf
The provided codetrack_file_handling.rb class Track att.pdf
fashination
 
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdfIn C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
shreeaadithyaacellso
 
816 LAB Playlist output linked list C++ Given main .pdf
816 LAB Playlist output linked list C++ Given main .pdf816 LAB Playlist output linked list C++ Given main .pdf
816 LAB Playlist output linked list C++ Given main .pdf
aarokyaaqua
 

Similar to I need help writing the methods for the song and playList class in J.pdf (20)

8.8 Program Playlist (Java)You will be building a linked list. Ma.pdf
8.8 Program Playlist (Java)You will be building a linked list. Ma.pdf8.8 Program Playlist (Java)You will be building a linked list. Ma.pdf
8.8 Program Playlist (Java)You will be building a linked list. Ma.pdf
 
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdfHi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
 
It's not working what am I doing wrong- Given main()- complete the Son.pdf
It's not working what am I doing wrong- Given main()- complete the Son.pdfIt's not working what am I doing wrong- Given main()- complete the Son.pdf
It's not working what am I doing wrong- Given main()- complete the Son.pdf
 
Here is the code.compile g++ Playlist.cpp main.cppPlaylist.h.pdf
Here is the code.compile  g++ Playlist.cpp main.cppPlaylist.h.pdfHere is the code.compile  g++ Playlist.cpp main.cppPlaylist.h.pdf
Here is the code.compile g++ Playlist.cpp main.cppPlaylist.h.pdf
 
I need help writing test Codepackage org.example;import j.pdf
I need help writing test Codepackage org.example;import j.pdfI need help writing test Codepackage org.example;import j.pdf
I need help writing test Codepackage org.example;import j.pdf
 
Many of us have large digital music collections that are not always .pdf
Many of us have large digital music collections that are not always .pdfMany of us have large digital music collections that are not always .pdf
Many of us have large digital music collections that are not always .pdf
 
1817 LAB Playlist output linked list Given main compl.pdf
1817 LAB Playlist output linked list Given main compl.pdf1817 LAB Playlist output linked list Given main compl.pdf
1817 LAB Playlist output linked list Given main compl.pdf
 
816 LAB Playlist output linked list Hey I have most of.pdf
816 LAB Playlist output linked list Hey I have most of.pdf816 LAB Playlist output linked list Hey I have most of.pdf
816 LAB Playlist output linked list Hey I have most of.pdf
 
OverviewThis project will allow you to write a program to get mo.docx
OverviewThis project will allow you to write a program to get mo.docxOverviewThis project will allow you to write a program to get mo.docx
OverviewThis project will allow you to write a program to get mo.docx
 
Given main(), complete the SongNode class to include the printSong.pdf
Given main(), complete the SongNode class to include the printSong.pdfGiven main(), complete the SongNode class to include the printSong.pdf
Given main(), complete the SongNode class to include the printSong.pdf
 
You are required to implement all the classes as specified by UML di.pdf
You are required to implement all the classes as specified by UML di.pdfYou are required to implement all the classes as specified by UML di.pdf
You are required to implement all the classes as specified by UML di.pdf
 
maincpp include ltiostreamgt include ltstringgt.pdf
maincpp  include ltiostreamgt include ltstringgt.pdfmaincpp  include ltiostreamgt include ltstringgt.pdf
maincpp include ltiostreamgt include ltstringgt.pdf
 
In C++ please I need output please You will be building a .pdf
In C++ please I need output please   You will be building a .pdfIn C++ please I need output please   You will be building a .pdf
In C++ please I need output please You will be building a .pdf
 
In C++ Plz LAB- Playlist (output linked list) Given main()- complete.pdf
In C++ Plz  LAB- Playlist (output linked list) Given main()- complete.pdfIn C++ Plz  LAB- Playlist (output linked list) Given main()- complete.pdf
In C++ Plz LAB- Playlist (output linked list) Given main()- complete.pdf
 
819 LAB Program Playlist c++ You will be building a li.pdf
819 LAB Program Playlist c++ You will be building a li.pdf819 LAB Program Playlist c++ You will be building a li.pdf
819 LAB Program Playlist c++ You will be building a li.pdf
 
The provided codetrack_file_handling.rb class Track att.pdf
The provided codetrack_file_handling.rb class Track    att.pdfThe provided codetrack_file_handling.rb class Track    att.pdf
The provided codetrack_file_handling.rb class Track att.pdf
 
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdfIn C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
 
Testing Jboss Seam Bottom Up
Testing Jboss Seam Bottom UpTesting Jboss Seam Bottom Up
Testing Jboss Seam Bottom Up
 
Swift Thinking
Swift ThinkingSwift Thinking
Swift Thinking
 
816 LAB Playlist output linked list C++ Given main .pdf
816 LAB Playlist output linked list C++ Given main .pdf816 LAB Playlist output linked list C++ Given main .pdf
816 LAB Playlist output linked list C++ Given main .pdf
 

More from arihanthtextiles

Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdfExplain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
arihanthtextiles
 
Epithelial tissues that are only one cell thick are called _____ Epi.pdf
Epithelial tissues that are only one cell thick are called _____  Epi.pdfEpithelial tissues that are only one cell thick are called _____  Epi.pdf
Epithelial tissues that are only one cell thick are called _____ Epi.pdf
arihanthtextiles
 
Clostridium difficile is a bacteria currently making headlines. It h.pdf
Clostridium difficile is a bacteria currently making headlines. It h.pdfClostridium difficile is a bacteria currently making headlines. It h.pdf
Clostridium difficile is a bacteria currently making headlines. It h.pdf
arihanthtextiles
 
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdfYou cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
arihanthtextiles
 
You have constructed a new reporter gene to help you find novel zebr.pdf
You have constructed a new reporter gene to help you find novel zebr.pdfYou have constructed a new reporter gene to help you find novel zebr.pdf
You have constructed a new reporter gene to help you find novel zebr.pdf
arihanthtextiles
 
A new species of sea horse has been discovered and is being sequenced.pdf
A new species of sea horse has been discovered and is being sequenced.pdfA new species of sea horse has been discovered and is being sequenced.pdf
A new species of sea horse has been discovered and is being sequenced.pdf
arihanthtextiles
 

More from arihanthtextiles (20)

How do autopolyploidy and allopolyploidy differ Only autopolyploidy.pdf
How do autopolyploidy and allopolyploidy differ  Only autopolyploidy.pdfHow do autopolyploidy and allopolyploidy differ  Only autopolyploidy.pdf
How do autopolyploidy and allopolyploidy differ Only autopolyploidy.pdf
 
How do you find the order of growth for the following recurrenceT.pdf
How do you find the order of growth for the following recurrenceT.pdfHow do you find the order of growth for the following recurrenceT.pdf
How do you find the order of growth for the following recurrenceT.pdf
 
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdfExplain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
 
Dynamic routing protocols are used toa) establish paths real ti.pdf
Dynamic routing protocols are used toa) establish paths real ti.pdfDynamic routing protocols are used toa) establish paths real ti.pdf
Dynamic routing protocols are used toa) establish paths real ti.pdf
 
Evaluate expression without a calculator. Write answer in radians..pdf
Evaluate expression without a calculator. Write answer in radians..pdfEvaluate expression without a calculator. Write answer in radians..pdf
Evaluate expression without a calculator. Write answer in radians..pdf
 
Epithelial tissues that are only one cell thick are called _____ Epi.pdf
Epithelial tissues that are only one cell thick are called _____  Epi.pdfEpithelial tissues that are only one cell thick are called _____  Epi.pdf
Epithelial tissues that are only one cell thick are called _____ Epi.pdf
 
Describe in five sentences how the TLR system is used to fight HSV-1 .pdf
Describe in five sentences how the TLR system is used to fight HSV-1 .pdfDescribe in five sentences how the TLR system is used to fight HSV-1 .pdf
Describe in five sentences how the TLR system is used to fight HSV-1 .pdf
 
Daunte is frustrated about managements recent decision to merge hi.pdf
Daunte is frustrated about managements recent decision to merge hi.pdfDaunte is frustrated about managements recent decision to merge hi.pdf
Daunte is frustrated about managements recent decision to merge hi.pdf
 
Concern over the weather associated with El Nino has increased intere.pdf
Concern over the weather associated with El Nino has increased intere.pdfConcern over the weather associated with El Nino has increased intere.pdf
Concern over the weather associated with El Nino has increased intere.pdf
 
Clostridium difficile is a bacteria currently making headlines. It h.pdf
Clostridium difficile is a bacteria currently making headlines. It h.pdfClostridium difficile is a bacteria currently making headlines. It h.pdf
Clostridium difficile is a bacteria currently making headlines. It h.pdf
 
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdfYou cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
 
You have constructed a new reporter gene to help you find novel zebr.pdf
You have constructed a new reporter gene to help you find novel zebr.pdfYou have constructed a new reporter gene to help you find novel zebr.pdf
You have constructed a new reporter gene to help you find novel zebr.pdf
 
Write a Java program that will incorporate either a sequential searc.pdf
Write a Java program that will incorporate either a sequential searc.pdfWrite a Java program that will incorporate either a sequential searc.pdf
Write a Java program that will incorporate either a sequential searc.pdf
 
Answer only with excel instructions.Assume that 96 of ticket hold.pdf
Answer only with excel instructions.Assume that 96 of ticket hold.pdfAnswer only with excel instructions.Assume that 96 of ticket hold.pdf
Answer only with excel instructions.Assume that 96 of ticket hold.pdf
 
A new species of sea horse has been discovered and is being sequenced.pdf
A new species of sea horse has been discovered and is being sequenced.pdfA new species of sea horse has been discovered and is being sequenced.pdf
A new species of sea horse has been discovered and is being sequenced.pdf
 
Which of the following is the mechanism of action for Herceptin Her.pdf
Which of the following is the mechanism of action for Herceptin  Her.pdfWhich of the following is the mechanism of action for Herceptin  Her.pdf
Which of the following is the mechanism of action for Herceptin Her.pdf
 
Which of these statements best describes an element An element is c.pdf
Which of these statements best describes an element  An element is c.pdfWhich of these statements best describes an element  An element is c.pdf
Which of these statements best describes an element An element is c.pdf
 
What is the general goal for descriptive statistics How is the goal.pdf
What is the general goal for descriptive statistics  How is the goal.pdfWhat is the general goal for descriptive statistics  How is the goal.pdf
What is the general goal for descriptive statistics How is the goal.pdf
 
What effects are shown on this graph (assume that all effects where.pdf
What effects are shown on this graph (assume that all effects where.pdfWhat effects are shown on this graph (assume that all effects where.pdf
What effects are shown on this graph (assume that all effects where.pdf
 
What are adaptive peaks How do organisms move from one adaptive pea.pdf
What are adaptive peaks How do organisms move from one adaptive pea.pdfWhat are adaptive peaks How do organisms move from one adaptive pea.pdf
What are adaptive peaks How do organisms move from one adaptive pea.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 

I need help writing the methods for the song and playList class in J.pdf

  • 1. 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
  • 2. // 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 { private String artist; // the artist performing the song private String title; // the title of the song
  • 3. 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() {
  • 4. 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;
  • 5. 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 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(); } public PlayList(String newName) { name=newName; playableList=new ArrayList(); } public boolean loadSongs(String fileName) throws FileNotFoundException { FileReader fr = new FileReader(fileName); Scanner sc = new Scanner(fr); while(sc.hasNextLine()) { String data = sc.nextLine();
  • 6. 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);
  • 7. 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()); }
  • 8. @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 { @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 { @Override public int compare(Song arg0, Song arg1) { if(arg0.getMinutes()>arg1.getMinutes()) return 1; else if(arg0.getMinutes()arg1.getSeconds()) return 1; else if(arg0.getSeconds()