How do i do this part of the assignment? Did i create the setters correctly? imp
ID: 3854658 • Letter: H
Question
How do i do this part of the assignment? Did i create the setters correctly?
import java.applet.AudioClip;
import java.applet.Applet;
import java.io.File;
import java.net.URL;
/**
* The <code>Song</code> class represents a song. Each song
* has a title, artist, play time, and file path.
*
* Here is an example of how a song can be created.
* <pre>
* Song song = new Song("Amsterdam", "Paul Oakenfold", 318, "sounds/Amsterdam.mp3");
* </pre>
*
* Here is an example of how a song can be used.
* <pre>
* System.out.println("Artist: " + song.getArtist());
* System.out.println(song);
* </pre>
*
* @author CS121 Instructors
*/
public class Song
{
// Used to play the song.
private AudioClip clip;
private String title;
private String artist;
private int playTime; // in seconds
private String filePath;
private int playCount;
/**
* Constructor: Builds a song using the given parameters.
* @param title song's title
* @param artist song's artist
* @param playTime song's length in seconds
* @param filePath song file to load
*/
public Song(String title, String artist, int playTime, String filePath)
{
this.title = title;
this.artist = artist;
this.playTime = playTime;
this.filePath = filePath;
this.playCount = 0;
String fullPath = new File(filePath).getAbsolutePath();
try {
this.clip = Applet.newAudioClip(new URL("file:" + fullPath));
} catch(Exception e) {
System.out.println("Error loading sound clip for " + fullPath);
System.out.println(e.getMessage());
}
}
/**
* Returns the title of this <code>Song</code>.
* @return the title
*/
public String getTitle()
{
return title;
}
/**
* Returns the artist of this <code>Song</code>.
* @return the artist
*/
public String getArtist()
{
return artist;
}
/**
* Returns the play time of this <code>Song</code> in seconds.
* @return the playTime
*/
public int getPlayTime()
{
return playTime;
}
/**
* Returns the file path of this <code>Song</code>.
* @return the filePath
*/
public String getFilePath()
{
return filePath;
}
/**
* Returns the number of times this song has been played.
* @return the count
*/
public int getPlayCount()
{
return playCount;
}
/**
* Plays this song asynchronously.
*/
public void play()
{
if(clip != null) {
clip.play();
playCount++;
}
}
/**
* Stops this song from playing.
*/
public void stop()
{
if(clip != null) {
clip.stop();
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return String.format("%-20s %-20s %-25s %10d",title, artist, filePath, playTime);
}
public String setArtist( String artist){
this.artist = artist;
return artist;
}
public String setTitle( String title){
this.title = title;
return title;
}
public String setFilePath( String filePath){
this.filePath = filePath;
return filePath;
}
public int setPlayTime(int playTime){
this.playTime = playTime;
return playTime;
}
4.1 Task 1: Add Setters to Song.java Your Song class already has getter (e.g. accessor) methods that were included in project 2. You need to modify the Song class to include setter methods. Write setter (1.e., mutator) methods for the tollowing instance variables: Write setter(i.e., mutator) methods for the following instance variables: . title (String type) artist (String type) playtime (int type) filepath(String type)Explanation / Answer
Hi buddy, You added your setters in Song.java class. But Ideally, setter methods don't return anything. So, your methods must be
public void setArtist( String artist){
this.artist = artist;
}
public void setTitle( String title){
this.title = title;
}
public void setFilePath( String filePath){
this.filePath = filePath;
}
public void setPlayTime(int playTime){
this.playTime = playTime;
}
Getter methods must return a value, but the setter method need not
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.