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

This is an exercise in working with Java arrays. The code I have isn\'t returnin

ID: 3623232 • Letter: T

Question

This is an exercise in working with Java arrays. The code I have isn't returning the values of the array stated in the main method, instead it's returning unusual statements. I know the correct values isn't being called somewhere, but at this point, I'm not exactly sure where. This program is broken into three parts. Here they are in order:

=================================================
public class Song {
private String title;
private boolean alreadyPlayed;

public boolean getAlreadyPlayed(){
return alreadyPlayed;
}//end get method

public void setAlreadyPlayed (boolean b){
b = alreadyPlayed;
}//end set method

public Song (String s){
s = title;
}//end constructor

public String getTitle(){
title = playList;
return title;
}
}

=================================================
import java.util.Arrays;
import java.util.Random;


public class MusicPlayer {
private Song[] playList;
private static Random rand = new Random();

public void setPlayList (Song song, int pos){
song = playList[pos];
}//end set method

public Song[] getPlayList(){
return playList;
}//end get method

public MusicPlayer (Song[] mySongs){
playList = mySongs;
}//end constructor

public void playInOrder(){
setAllSongsToNotPlayed();
for (int i=0; i<playList.length; i++){
System.out.println(playList[i]);
playList[i].setAlreadyPlayed(true);
}
}

public void setAllSongsToNotPlayed() {
//Arrays.sort(playList, 0, 4);
for(int i=0; i<playList.length; i++){
playList[i].setAlreadyPlayed(false); //resets song selection
}
}

public void randomMix(){
setAllSongsToNotPlayed();
for (int i=0; i<playList.length; i++) {
int randomPosition = rand.nextInt(playList.length);//picks a random array value
Song temp = playList[i];
playList[i] = playList[randomPosition];
playList[randomPosition] = temp;
System.out.println(playList[i]);
}
}
}
=================================================

public class Exercise3 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Song s = new Song("Scar Tissue");
Song s2 = new Song("Vicarious");
Song s3 = new Song("Clint Eastwood");
Song s4 = new Song("Chop Suey");
Song s5 = new Song("Headstrong");
Song[] mySongs = new Song[5];
mySongs[0] = s;
mySongs[1] = s2;
mySongs[2] = s3;
mySongs[3] = s4;
mySongs[4] = s5;
MusicPlayer player = new MusicPlayer(mySongs); // you have to send in a Song[] object
//player.playInOrder();
player.randomMix();
player.randomMix();

}

}
=================================================

Any explanation to help guide me where I went wrong would be amazing! Thanks!

Explanation / Answer

please rate - thanks

in a few places the left hand side and right hand side of = were reversed.

you were never getting the title

I think I've highlighed all the changes but not sure

public class Exercise3 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Song s = new Song("Scar Tissue");
Song s2 = new Song("Vicarious");
Song s3 = new Song("Clint Eastwood");
Song s4 = new Song("Chop Suey");
Song s5 = new Song("Headstrong");
Song[] mySongs = new Song[5];
mySongs[0] = s;
mySongs[1] = s2;
mySongs[2] = s3;
mySongs[3] = s4;
mySongs[4] = s5;
MusicPlayer player = new MusicPlayer(mySongs); // you have to send in a Song[] object
//player.playInOrder();
player.randomMix();
player.randomMix();

}

}

------------------------------------

import java.util.Arrays;
import java.util.Random;


public class MusicPlayer {
private Song[] playList;
private static Random rand = new Random();

public void setPlayList (Song song, int pos){
playList[pos]=song;
}//end set method

public Song[] getPlayList(){
return playList;
}//end get method

public MusicPlayer (Song[] mySongs){
playList = mySongs;
}//end constructor

public void playInOrder(){
setAllSongsToNotPlayed();
for (int i=0; i<playList.length; i++){
System.out.println(playList[i].getTitle());
playList[i].setAlreadyPlayed(true);
}
}

public void setAllSongsToNotPlayed() {
//Arrays.sort(playList, 0, 4);
for(int i=0; i<playList.length; i++){
playList[i].setAlreadyPlayed(false); //resets song selection
}
}

public void randomMix(){
setAllSongsToNotPlayed();
for (int i=0; i<playList.length; i++) {
int randomPosition = rand.nextInt(playList.length);//picks a random array value
Song temp = playList[i];
playList[i] = playList[randomPosition];
playList[randomPosition] = temp;
System.out.println(playList[i].getTitle());
}
}
}

------------------------------------------------------

public class Song {
private String title;
private boolean alreadyPlayed;

public boolean getAlreadyPlayed(){
return alreadyPlayed;
}//end get method

public void setAlreadyPlayed (boolean b){
alreadyPlayed=b;
}//end set method

public Song (String s){
title=s;
}//end constructor

public String getTitle(){
return title;
}
}

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