Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help doing JUnit tests for the following code( except for the void methods). The code is Playable interface , PlayList class, and a

I need help doing JUnit tests for the following code( except for the void methods). The code is Playable interface , PlayList class, and a Song class.

Song.java

import java.util.ArrayList; import java.util.Collections;

public class Song implements Comparable, Playable { private String artist, title; private int minutes, seconds;

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) { if(seconds >= 60){ while(seconds >= 60){ seconds -= 60; this.minutes += 1; } } this.seconds = seconds; }

public Song(String artist, String title){

this.artist = artist; this.title = title; this.minutes = 0; this.seconds = 0; }

public Song(String artist, String title, int minutes, int seconds){

this.artist = artist; this.title = title; this.minutes = minutes;

if(seconds >= 60){ while(seconds >= 60){ seconds -= 60; this.minutes += 1; } } this.seconds = seconds; }

public Song(Song s){ this.title = s.getTitle(); this.artist = s.getArtist(); this.minutes = s.getMinutes(); this.seconds = s.getSeconds(); }

public boolean equals(Object o){ boolean reVal = false; if(o instanceof Song){ Song s = (Song)o; reVal = (this.artist.equals(s.getArtist()))&&(this.title.equals(s.getTitle()) && (this.minutes == s.getMinutes()) && this.seconds == s.getSeconds()); } return reVal; }

public String toString(){ return "{Song:title = "+title+" artist = "+artist+"}"; }

public void play(){ System.out.printf("Playing Song: artist = %-20s title = %s ",artist,title); }

@Override public int compareTo(Song s){ int reVal = this.artist.compareTo(s.getArtist()); if(reVal != 0) return reVal; else return this.title.compareTo(s.getTitle()); }

public int getPlayTimeSeconds(){ return (this.minutes * 60) + this.seconds; }

public String getName() { return title; }

}

PlayList.java

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner;

public class PlayList implements Playable { private String name; private ArrayList playableList = new ArrayList();

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 String toString(){ String str = name + " "; for(Playable pl:playableList){ str += pl.toString() + " "; } return str; }

public PlayList(){ name = "Untitled"; playableList = new ArrayList(); }

public PlayList(String name){ this.name = name; playableList = new ArrayList(); }

public boolean loadSongs(String filename){ File input = new File(filename); Scanner inputFile; try{ inputFile = new Scanner(input); } catch(FileNotFoundException e) { return false; } while(inputFile.hasNextLine()){ String title = inputFile.nextLine().trim(); String artist = inputFile.nextLine().trim(); String time = inputFile.nextLine().trim(); int minutes = Integer.parseInt(time.substring(0, time.indexOf(':'))); int seconds = Integer.parseInt(time.substring(time.indexOf(':')+1));

inputFile.nextLine();

Song s = new Song(artist, title, minutes, seconds); this.addSong(s); } inputFile.close(); return true;

}

public boolean clear(){ playableList.clear(); return true; }

public boolean addSong(Song s){ return playableList.add(s); }

public Playable removePlayable(int index){ if(index < 0 || index >= playableList.size()){ return null; } return playableList.remove(index); }

public Playable removePlayable(Playable p){ while(playableList.contains(p)){ playableList.remove(p); } return p; }

public Playable getPlayable(int index){ if(index < 0 || index >= playableList.size()){ return null; } return playableList.get(index); }

public boolean addPlayList(PlayList pl){ for(Playable p: playableList){ if(p instanceof PlayList){ if(p.getName().equals(pl.getName())){ return false; } } } playableList.add(pl); return true; }

public void play(){ for(Playable p: playableList){ p.play(); } }

public int size(){ int number = 0; for(Playable p : playableList){ if(p instanceof PlayList){ PlayList pl = (PlayList)p; number += pl.size() + 1; } if(p instanceof Song){ System.out.println(p); number += 1; }

} return number; }

public int getPlayTimeSeconds(){ int sec = 0; for(Playable p : playableList){ sec += p.getPlayTimeSeconds(); } return sec; }

public void sortByName(){ Collections.sort(playableList,new PlayableSortByName()); }

}

Playable.java

public interface Playable {

public void play();

public String getName();

public int getPlayTimeSeconds();

}

SortByName.java

import java.util.Comparator;

public class PlayableSortByName implements Comparator {

public int compare(Playable p1, Playable p2){ return p1.getName().compareTo(p2.getName()); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago