1-Explain the code line by line public void forward() { if (state.equals(\"playi
ID: 3528348 • Letter: 1
Question
1-Explain the code line by line public void forward() { if (state.equals("playing")) { Mp3 song = songs.get(currentSongPlaying); song.stop(); if (songs.size()-1>currentSongPlaying) { currentSongPlaying = currentSongPlaying + 1; song = songs.get(currentSongPlaying); song.play(this); } else if (songs.size()-1==currentSongPlaying) { state="notPlaying"; } } } /** * method the song that is being played now calls when it has ended */ public void playingHasEnded() { if ((songs.size()>currentSongPlaying) && (state.equals("playing"))) { currentSongPlaying = currentSongPlaying + 1; Mp3 song = songs.get(currentSongPlaying); song.play(this); } else if (state.equals("playing")) { state = "notPlaying"; }Explanation / Answer
public void forward() {
if (state.equals("playing")) { //if mp3player is playing now
Mp3 song = songs.get(currentSongPlaying);//get the current song playing
song.stop(); // and stop it
if (songs.size()-1>currentSongPlaying) { //if there are more songs in the playlist after this song
currentSongPlaying = currentSongPlaying + 1;//go to next song number
song = songs.get(currentSongPlaying); // and get that song
song.play(this); } // and play that song
else if (songs.size()-1==currentSongPlaying) {//if no more songs to play
state="notPlaying"; //change the mp3 player from playing to not playing
}
}
}
/** * method the song that is being played now calls when it has ended */
public void playingHasEnded() {
if ((songs.size()>currentSongPlaying) && (state.equals("playing"))) {
/**if there are more songs in the playlist after this song and mp3 player is in playing state, then play the next song**/
currentSongPlaying = currentSongPlaying + 1;//increment song number to go to next song
Mp3 song = songs.get(currentSongPlaying);//now get the next song
song.play(this); ///play that song
}
else if (state.equals("playing")) {
state = "notPlaying";
/** if mp3player is in playing state and there are no songs in the playlist then change the state of the mp3player to not playing**/
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.