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

JAVA PROGRAM You are planning a road trip and want to create a playlist of your

ID: 3730838 • Letter: J

Question

JAVA PROGRAM

You are planning a road trip and want to create a playlist of your favorite songs. Assume that the

song titles are in an array of strings. Create a shuffle of your songs (permutation of your original

songs).

Use the Fisher–Yates shuffle algorithm that works in O(n) running time. We will use a method that

creates pseudo-random numbers (see end for help) in O(1) running time.

The basic idea is to start from the last element, swap it with a randomly selected element from the

whole array (including last). In the next step you will consider the array from 0 to n-2 (size reduced

by 1), and repeat the process until you reach the first element. Write a program that uses the

provided Playlist.txt as input and outputs the shuffled array in a file called

LastNameFirstNamePlaylist.txt.

Follow the next pseudocode:

To shuffle an array a of n elements (indices 0..n-1):

for i from n - 1 downto 1

j = random integer with 0 <= j <= i

exchange a[j] and a[i]

Create appropriate JUnits to test your program.

Extra Help for JUnit tests: Create the 'same' pseudo-random numbers by using a specific seed Random generator (once) in the class public class MYCLASSÍ private Random myRandGen double myrandom() return myRandGen.nextDouble(); Ilrandom in 0-1 And in the constructor public MYCLASS(int dimension_in) t myRandGen-new java.util.Random(0); l/seed is 0 Usage (int)(myrandom) size of array)

Explanation / Answer

// Please change the input and output directories.

import java.io.FileWriter;

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.Arrays;

import java.util.List;

import java.util.Random;

import java.util.stream.Collectors;

import java.util.stream.Stream;

public class MYCLASS {

private Random myRandGen;

static void randomize(String arr[], int n) throws IOException {

// Creating a object for Random class

Random r = new Random();

// Start from the last element and swap one by one. We don't

// need to run for the first element that's why i > 0

for (int i = n - 1; i > 0; i--) {

// Pick a random index from 0 to i

int j = r.nextInt(i);

// Swap arr[i] with the element at random index

String temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

FileWriter writer = new FileWriter("C:\Users\Dell\Desktop\LastNameFirstNamePlaylist.txt");

writer.write(Arrays.toString(arr));

writer.close();

// Prints the random array

System.out.println(Arrays.toString(arr));

}

// Driver Program to test above function

public static void main(String[] args) {

String fileName = "C:\Users\Dell\Desktop\Playlist.txt";

try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

List<String> list = stream.collect(Collectors.toList());

String[] playlist = new String[list.size()];

int i = 0;

for (String string : list) {

playlist[i] = string;

i++;

}

System.out.println(Arrays.toString(playlist));

int n = playlist.length;

randomize(playlist, n);

} catch (IOException e) {

e.printStackTrace();

}

}

}