How do you create this method? I dont know if what i did is okay? • First, modif
ID: 3860545 • Letter: H
Question
How do you create this method? I dont know if what i did is okay?
• First, modify your PlayList class header so it implements the MyTunesPlayListInterface. public class PlayList implements MyTunesPlayListInterface Make sure to stub out all the methods (you can use the Eclipse auto-fix suggestion). • You must load a playlist from the playlist.txt file. Implement the loadFromFile() method that takes in a File as an argument and reads in the song data from the provided text file, adding each song to the playlist.
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
/**
* @author
*/
public class PlayList implements MyTunesPlayListInterface {
private String name;
private Song playing;
/**
* Creates a new array list
*/
public ArrayList <Song> songList;
public PlayList(String name){
this.name= name;
this.playing = null;
songList = new ArrayList<Song>();
}
/**
* @return returns the name
*/
public String getName(){
return name;
}
/**
* @return returns the song playing
*/
public Song getPlaying(){
return playing;
}
/**
* @param songList name of array list
* @return returns the list of the songs
*/
public ArrayList <Song> getsongList(ArrayList<Song> songList){
return songList;
}
/**
* @param name sets the name of the song
*/
public void setName(String name){
this.name = name;
}
/**
* @param name adds a song to the play list
*/
public void addSong(Song name){
songList.add(name);
}
/**
* @param i is an integer for the index
* @return removes the song and returns null
*/
public Song removeSong(int i ){
if( i < 0 || i >= songList.size()){
return null;
}
return songList.remove(i);
}
/**
* @return returns the number of songs within the play list
*/
public int getNumSongs(){
return songList.size();
}
/**
* @return returns the total play time of the songs
*/
public int getTotalPlayTime(){
int totalPlayT = 0;
for(int s = 0; s < songList.size(); s++){
Song s1 = songList.get(s);
s1.getPlayTime();
totalPlayT = totalPlayT + songList.get(s).getPlayTime();
}
return totalPlayT;
}
/**
* @param i is an integer for the index
* @return returns null and gets song for the play list
*/
public Song getSong(int i){
if( i < 0 || i >= songList.size()){
return null;
}
return songList.get(i);
}
/**
* @param i is an integer for the index
* @return returns the song that is playing
*/
public Song playSong(int i){
if(i >= 0 && i < songList.size()){
songList.get(i).play();
playing = songList.get(i);
return playing;
}else{
return playing;
}
}
/**
* @return returns various song information, such as the average, longest and shortest play time
*/
public String getInfo(){
String getInfo= "";
if( !songList.isEmpty()){
DecimalFormat df = new DecimalFormat("0.00");
double average = (double)getTotalPlayTime() / songList.size();
Song shortest = songList.get(0);
for(int i = 0; i < songList.size(); i++){
Song s2 = songList.get(i);
if(s2.getPlayTime()< shortest.getPlayTime()){
shortest = s2;
}
}
Song longest = songList.get(0);
for(int p = 0; p < songList.size();p++){
Song s3 = songList.get(p);
if(s3.getPlayTime() >= longest.getPlayTime()){
longest = s3;
}
}
int totalPlayT = 0;
for(int s = 0; s < songList.size(); s++){
Song s1 = songList.get(s);
s1.getPlayTime();
totalPlayT = totalPlayT + songList.get(s).getPlayTime();
}
getInfo += "The average play time is: " + df.format(average)+ " " + "seconds ";
getInfo += "The shortest song is: " + shortest + " ";
getInfo += "The longest song is: " + longest + " ";
getInfo += "Total play time: " + totalPlayT + " " + "seconds ";
} else {
getInfo += "There are no songs. ";
}
return getInfo;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString(){
String getInfo = "";
getInfo += "------------------ ";
getInfo += this.getName() + " (" + songList.size() + " songs) ";
getInfo += "------------------ ";
if(!songList.isEmpty()){
for( Song s: songList){
getInfo += "(" + songList.indexOf(s) + ")" + s.toString() + " ";
}
getInfo += "------------------ ";
}else{
getInfo += "There are no songs. ";
getInfo += "------------------ ";
}
return getInfo;
}
@Override
public void loadFromFile(File playlist) {
// TODO Auto-generated method stub
}
@Override
public void playSong(int index) {
// TODO Auto-generated method stub
}
@Override
public void playSong(Song song) {
// TODO Auto-generated method stub
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public Song[] getSongArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public int moveUp(int index) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int moveDown(int index) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Song[][] getSongSquare() {
// TODO Auto-generated method stub
return null;
}
}
Explanation / Answer
1. MyTunesPlayListInterface
import java.io.File;
public interface MyTunesPlayListInterface {
public void loadFromFile(File playlist);
public Song[] getSongArray();
public void playSong(Song song);
public void stop();
public int moveUp(int index);
public int moveDown(int index);
public Song[][] getSongSquare();
public Song playSong(int index);
}
2. PlayList
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
/**
* @author
*/
public class PlayList implements MyTunesPlayListInterface {
private String name;
private Song playing;
/**
* Creates a new array list
*/
public ArrayList<Song> songList;
public PlayList(String name) {
this.name = name;
this.playing = null;
songList = new ArrayList<Song>();
}
/**
* @return returns the name
*/
public String getName() {
return name;
}
/**
* @return returns the song playing
*/
public Song getPlaying() {
return playing;
}
/**
* @param songList
* name of array list
* @return returns the list of the songs
*/
public ArrayList<Song> getsongList(ArrayList<Song> songList) {
return songList;
}
/**
* @param name
* sets the name of the song
*/
public void setName(String name) {
this.name = name;
}
/**
* @param name
* adds a song to the play list
*/
public void addSong(Song name) {
songList.add(name);
}
/**
* @param i
* is an integer for the index
* @return removes the song and returns null
*/
public Song removeSong(int i) {
if (i < 0 || i >= songList.size()) {
return null;
}
return songList.remove(i);
}
/**
* @return returns the number of songs within the play list
*/
public int getNumSongs() {
return songList.size();
}
/**
* @return returns the total play time of the songs
*/
public int getTotalPlayTime() {
int totalPlayT = 0;
for (int s = 0; s < songList.size(); s++) {
Song s1 = songList.get(s);
totalPlayT = totalPlayT + s1.getPlayTime();
}
return totalPlayT;
}
/**
* @param i
* is an integer for the index
* @return returns null and gets song for the play list
*/
public Song getSong(int i) {
if (i < 0 || i >= songList.size()) {
return null;
}
return songList.get(i);
}
/**
* @return returns various song information, such as the average, longest
* and shortest play time
*/
public String getInfo() {
String getInfo = "";
if (!songList.isEmpty()) {
DecimalFormat df = new DecimalFormat("0.00");
double average = (double) getTotalPlayTime() / songList.size();
Song shortest = songList.get(0);
for (int i = 0; i < songList.size(); i++) {
Song s2 = songList.get(i);
if (s2.getPlayTime() < shortest.getPlayTime()) {
shortest = s2;
}
}
Song longest = songList.get(0);
for (int p = 0; p < songList.size(); p++) {
Song s3 = songList.get(p);
if (s3.getPlayTime() >= longest.getPlayTime()) {
longest = s3;
}
}
int totalPlayT = 0;
for (int s = 0; s < songList.size(); s++) {
Song s1 = songList.get(s);
s1.getPlayTime();
totalPlayT = totalPlayT + songList.get(s).getPlayTime();
}
getInfo += "The average play time is: " + df.format(average) + " " + "seconds ";
getInfo += "The shortest song is: " + shortest + " ";
getInfo += "The longest song is: " + longest + " ";
getInfo += "Total play time: " + totalPlayT + " " + "seconds ";
} else {
getInfo += "There are no songs. ";
}
return getInfo;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
String getInfo = "";
getInfo += "------------------ ";
getInfo += this.getName() + " (" + songList.size() + " songs) ";
getInfo += "------------------ ";
if (!songList.isEmpty()) {
for (Song s : songList) {
getInfo += "(" + songList.indexOf(s) + ")" + s.toString() + " ";
}
getInfo += "------------------ ";
} else {
getInfo += "There are no songs. ";
getInfo += "------------------ ";
}
return getInfo;
}
@Override
public void loadFromFile(File playlist) {
try {
if (playlist != null) {
String splitBy = ",";
BufferedReader br = new BufferedReader(new FileReader(playlist));
String line;
while((line = br.readLine()) !=null){
String[] b = line.split(splitBy);
Song song = new Song(b[0],b[1],b[3],b[3]);
songList.add(song);// add song to arrayList
}
br.close();
}
} catch (Exception e) {
System.out.println("File not found"+e);
}
}
/**
* @param i
* is an integer for the index
* @return returns the song that is playing
*/
@Override
public Song playSong(int index) {
// TODO Auto-generated method stub
if (index >= 0 && index < songList.size()) {
songList.get(index).play();
playing = songList.get(index);
return playing;
} else {
return playing;
}
}
@Override
public void playSong(Song song) {
// TODO Auto-generated method stub
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public Song[] getSongArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public int moveUp(int index) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int moveDown(int index) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Song[][] getSongSquare() {
// TODO Auto-generated method stub
return null;
}
}
3. Song
public class Song {
String name;
String artist;
String duration;
String fileName;
public Song(String aName, String aArtist, String aDuration, String aFileName) {
this.name = aName;
this.artist = aArtist;
this.duration = aDuration;
this.fileName = aFileName;
}
public int getPlayTime() {
// TODO Auto-generated method stub
return 0;
}
public void play() {
// TODO Auto-generated method stub
}
}
4. playlist.txt
Classical,Artist,00:04,sounds/classical.wav
Eighties,Jam Some 80's band,3:00,sounds/eightiesJam.wav
New Age,Javya,04:01,sounds/newAgeRythm.wav
Hitchcock,The Birds,09:21,sounds/hitchcock.wav
You can modify Song.java class as per your requirement.
Let me know if any change required
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.