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

Part II: Shuffle.java Write a static method called shuffle, of type void, that r

ID: 3621799 • Letter: P

Question

Part II: Shuffle.java
Write a static method called shuffle, of type void, that receives an array of type String, and randomly shuffles its values. For example, if the input array is {"a","b","c"}, when shuffle terminates the array might be {"b","a","c"}. The shuffle method must use a Random object or the method Math.random(). The method Math.random() returns a double value in the range [0.0, 1.0). For your reference, note that the following expression uses Math.random() to create an integer between 0 and N-1 inclusive.
(int) (Math.random() * N) The amount of work performed by the shuffle method should be proportional to the size of the array (little work for small arrays and more work for larger arrays) and the execution of the method should result in a truly random shuffle of the array contents regardless of the size of the array.
How exactly Math.random() or the Random object is used to shuffle the values is a creativity exercise.Next, write a void method called printData that accepts an array of type String as a parameter and prints the contents of the array to the screen, one string per line. Next, write a method called readData that has no parameters and returns an array of type String. This method should prompt the user to enter the file name containing the desired data, and read in that file name (this requires the creation of Scanner object for keyboard input). Keep prompting until a valid file name is entered. Then create a Scanner object on the input file. The file starts with an integer specifying the number of songs in the file, and then is followed by that number of song names, one per line. Your task will be to create a String array of the correct size, fill it with the data from the file, and return the array to the caller. Warning: Remember the issues that occur when combining token-based input (e.g., nextInt()) with line-based input (e.g., nextLine()).Finally write a main method that calls readData to read the data file. Afterward it should print the contents of the array, then shuffle it, and finally print the contents again

This is a sample execution to show you how Shuffle.java should behave:
Please enter the name of a file containing a playlist. play.txt
File does not exist.
Please try again. playlist.txt
Songs read from file:
Bob Dylan - Like a Rolling Stone
The Rolling Stones - Satisfaction
John Lennon - Imagine
Marvin Gaye - What's Going On
Aretha Franklin - Respect
The Beach Boys - Good Vibrations
Chuck Berry - Johnny B. Goode
The Beatles - Hey Jude
Nirvana - Smells Like Teen Spirit
Ray Charles - What'd I Say
Shuffled playlist:
Ray Charles - What'd I Say
The Rolling Stones - Satisfaction
Nirvana - Smells Like Teen Spirit
Chuck Berry - Johnny B. Goode
The Beach Boys - Good Vibrations
The Beatles - Hey Jude
John Lennon - Imagine
Marvin Gaye - What's Going On
Aretha Franklin - Respect
Bob Dylan - Like a Rolling Stone

Please explain what your code do.
I'm just using this as reference

Explanation / Answer

The Java Collections shuffle "traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive."

That is a reasonable algorithm to implement here.

public static void shuffle(String[] array)
{
for(int i = array.length-1; i>=0; i--)
{
// select element from 0 to i to swap with ith element
int idx = (int)(Math.random()*i);

String temp = array[i];
array[i] = array[idx];
array[idx] = temp;
}
}

I will assume that, as you are being asked to implement a shuffle method, you have learned the keyboard I/O to code the read/write parts of the assignment.