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

Create a shuffle method using the below pseudo code as a guide: create a new emp

ID: 3528393 • Letter: C

Question

Create a shuffle method using the below pseudo code as a guide: create a new empty arraylist (called newList) while randomly select the index of one song on the playlist remove it from the current playlist and place it at the end of newList songs = newList Generate a random number using the Random class in the Java library. Its method is: public int nextInt(int n). This returns a pseudorandom, uniformly distributed int value as low as 0 and as high as n. Therefore, nextInt(songs.size()) gives you a random index. Remember that the size of songs lessens by one each time you add a randomly selected song to newList. You will need to account for this each time you generate a random number.

Explanation / Answer

This should do the trick

public void shuffle()
    {
        ArrayList<Mp3> newList = new ArrayList<Mp3>();
        int n = 0;
        while (songs.size() != 0)
        {
            n = random.nextInt(songs.size());
            newList.add(songs.get(n));
            songs.remove(n);
        }
        for(int i = 0; i < newList.size(); i++)
        {
        songs.add(i, newList.get(i));
        //System.out.println(song.get(i)
        }
    }

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