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

Objects first with Java 6th edition Exercise 4:35 Regarding the last line of cod

ID: 3824219 • Letter: O

Question

Objects first with Java 6th edition Exercise 4:35

Regarding the last line of code in the playTrack method: getPlayCount(track, track.size());

- I understand the method is being called to count the number of times the track is being played however I dont understand the correlation why we need the collection length via the method size. What exactly is the tracks size - not sure what its referring here.

- It states in part 3 of solution that "when the value of field playCount is equal to the size of the track(?), then its time to reset its value again to 0" - what does this mean? Isn't the playCount just an increment of how many times the track is being played? Why do you need to reset it to 0.

Thanx in advance

This is the other method: getPlayCount

MusicOrganizer Class

import java.util.ArrayList;

/**
* A class to hold details of audio tracks.
* Individual tracks may be played.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class MusicOrganizer
{
// An ArrayList for storing music tracks.
private ArrayList<Track> tracks;
// A player for the music tracks.
private MusicPlayer player;
// A reader that can read music files and load them as tracks.
private TrackReader reader;

/**
* Create a MusicOrganizer
*/
public MusicOrganizer()
{
tracks = new ArrayList<>();
player = new MusicPlayer();
reader = new TrackReader();
readLibrary("../audio");
System.out.println("Music library loaded. " + getNumberOfTracks() + " tracks.");
System.out.println();
}
  
/**
* Add a track file to the collection.
* @param filename The file name of the track to be added.
*/
public void addFile(String filename)
{
tracks.add(new Track(filename));
}
  
/**
* Add a track to the collection.
* @param track The track to be added.
*/
public void addTrack(Track track)
{
tracks.add(track);
}
  
/**
* Play a track in the collection.
* @param index The index of the track to be played.
*/
public void playTrack(int index)
{
if(indexValid(index)) {
Track track = tracks.get(index);
player.startPlaying(track.getFilename());
System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
}
}
  
/**
* Return the number of tracks in the collection.
* @return The number of tracks in the collection.
*/
public int getNumberOfTracks()
{
return tracks.size();
}
  
/**
* List a track from the collection.
* @param index The index of the track to be listed.
*/
public void listTrack(int index)
{
System.out.print("Track " + index + ": ");
Track track = tracks.get(index);
System.out.println(track.getDetails());
}
  
/**
* Show a list of all the tracks in the collection.
*/
public void listAllTracks()
{
System.out.println("Track listing: ");

for(Track track : tracks) {
System.out.println(track.getDetails());
}
System.out.println();
}
  
/**
* List all tracks by the given artist.
* @param artist The artist's name.
*/
public void listByArtist(String artist)
{
for(Track track : tracks) {
if(track.getArtist().contains(artist)) {
System.out.println(track.getDetails());
}
}
}
  
/**
* Remove a track from the collection.
* @param index The index of the track to be removed.
*/
public void removeTrack(int index)
{
if(indexValid(index)) {
tracks.remove(index);
}
}
  
/**
* Play the first track in the collection, if there is one.
*/
public void playFirst()
{
if(tracks.size() > 0) {
player.startPlaying(tracks.get(0).getFilename());
}
}
  
/**
* Stop the player.
*/
public void stopPlaying()
{
player.stop();
}

/**
* Determine whether the given index is valid for the collection.
* Print an error message if it is not.
* @param index The index to be checked.
* @return true if the index is valid, false otherwise.
*/
private boolean indexValid(int index)
{
// The return value.
// Set according to whether the index is valid or not.
boolean valid;
  
if(index < 0) {
System.out.println("Index cannot be negative: " + index);
valid = false;
}
else if(index >= tracks.size()) {
System.out.println("Index is too large: " + index);
valid = false;
}
else {
valid = true;
}
return valid;
}
  
private void readLibrary(String folderName)
{
ArrayList<Track> tempTracks = reader.readTracks(folderName, ".mp3");

// Put all thetracks into the organizer.
for(Track track : tempTracks) {
addTrack(track);
}
}
}

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 2016.02.29
*/
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;
}
  
}

** There is still the TrackReader Class & MusicPlayer Class - let me know if u need any of them as well

Class MusicOrganizer Method playTrack public void playTrack(int index) if(indexValid (index) Track track tracks get (index) player.startPlaying(track.getFilename0); System.out printin "Now playing: track.getArtist0 track getTitle0); The method getPlayCount has been called to count the number of times a track is played. It accepts the instance of class Track called track along with its collection length called tracks defined by the method size. getPlayCount(track, tracks.size(0):

Explanation / Answer

In method playTrack, we pass the track and the size of the collection to method getPlayCount.
Let suppose you have 20 tracks in a collection, so bascially we pass the track that we want to play
along with the size of the our collection i.e. 20 tracks.

It is being set to 0 when the field playCount is equal to the size of the track, because let say we
have 20 tracks in our list, and the track that we want to play is the number 20 itself, then the next track
which would be played is the number one track in our collection. That is why it is being set to zero. Also it is being set
to zero and not one is because of the fact that indexing in java and any other major language starts from 0.

It is important to understand that the size here is not the size of the track or length of the track, but
it is the size of the collection. Collection is nothing but a playlist in simple terms. Hope it helps!