Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

challenge exercise consider how you might play multiple tracks in random order.

ID: 3673750 • Letter: C

Question

challenge exercise consider how you might play multiple tracks in random order. would you want to make sure that all tracks are played equally or prefered favorite tracks? How might a "play count" field in the Track class help wtih this task? This is queston 4.43 in Chapter 4 of Objects First With Java: A Practical Introduction Using BlueJ (5th Edition). I am having trouble solving it and am wondering how I would complete this exercise. Here is the track class:

/**
* Store the details of a music track,
* such as the artist, title, and file name.
*
* @author David J. Barnes and Michael Kölling
* @version 2011.07.31
*/
public class Track
{
// The artist.
private String artist;
// The track's title.
private String title;
// Where the track is stored.
private String filename;
  
/**
* Constructor for objects of class Track.
* @param artist The track's artist.
* @param title The track's title.
* @param filename The track file.
*/
public Track(String artist, String title, String filename)
{
setDetails(artist, title, filename);
}
  
/**
* Constructor for objects of class Track.
* It is assumed that the file name cannot be
* decoded to extract artist and title details.
* @param filename The track file.
*/
public Track(String filename)
{
setDetails("unknown", "unknown", filename);
}
  
/**
* Return the artist.
* @return The artist.
*/
public String getArtist()
{
return artist;
}
  
/**
* Return the title.
* @return The title.
*/
public String getTitle()
{
return title;
}
  
/**
* Return the file name.
* @return The file name.
*/
public String getFilename()
{
return filename;
}
  
/**
* Return details of the track: artist, title and file name.
* @return The track's details.
*/
public String getDetails()
{
return artist + ": " + title + " (file: " + filename + ")";
}
  
/**
* Set details of the track.
* @param artist The track's artist.
* @param title The track's title.
* @param filename The track file.
*/
private void setDetails(String artist, String title, String filename)
{
this.artist = artist;
this.title = title;
this.filename = filename;
}
  
}

Explanation / Answer

we can do this with the help of a playcount variable. I have added the same in the code with the function.

This will keep track of how many times a song is played. Java code:

public class Track
{
// The artist.
private String artist;
// The track's title.
private String title;
// Where the track is stored.
private String filename;
  
   int playCount; // for keeping track of the count.
  
  
/**
* Constructor for objects of class Track.
* @param artist The track's artist.
* @param title The track's title.
* @param filename The track file.
*/
public Track(String artist, String title, String filename)
{
setDetails(artist, title, filename);
}
  
/**
* Constructor for objects of class Track.
* It is assumed that the file name cannot be
* decoded to extract artist and title details.
* @param filename The track file.
*/
public Track(String filename)
{
setDetails("unknown", "unknown", filename);
}
  
   public void resetCount(){
playCount = 0;
}

public void incrementCount(){
playCount++;
}
  
/**
* Return the artist.
* @return The artist.
*/
public String getArtist()
{
return artist;
}
  
/**
* Return the title.
* @return The title.
*/
public String getTitle()
{
return title;
}
  
/**
* Return the file name.
* @return The file name.
*/
public String getFilename()
{
return filename;
}
  
/**
* Return details of the track: artist, title and file name.
* @return The track's details.
*/
public String getDetails()
{
return artist + ": " + title + " (file: " + filename + ")";
}
  
   public void playTrack(int index)
{
if(indexValid(index)) {
Track track = tracks.get(index);
stopPlaying();
player.startPlaying(track.getFilename());
track.incrementCount();
System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
}
}
  
/**
* Set details of the track.
* @param artist The track's artist.
* @param title The track's title.
* @param filename The track file.
*/
private void setDetails(String artist, String title, String filename)
{
this.artist = artist;
this.title = title;
this.filename = filename;
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote