Question: Need help creating JUnit test cases for a Fisher-Yates shuffle algorithm. Below is the code, the project description and requirements, and some pseudocode. CODE: import
Need help creating JUnit test cases for a Fisher-Yates shuffle algorithm. Below is the code, the project description and requirements, and some pseudocode. CODE:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Random;
//Randomly gets songs from the input
//Prints them into the output
//Shuffles the songs in the output
//Reprints the songs in the output
public class Playlist
{
static String[] FYShuffling(String []array1, int d) //shuffle algorithm
{
String []list = new String[d];
String []index2 = new String[d];
for(int i = 0; i < d; i++)
index2[i] = "a";
int index;
Random rand = new Random();
for(int i = 0; i < d; i++)
{
do //while loop goes first, then condition is checked
{
index = rand.nextInt(d);
}
while(index2[index] != "a");
{
index2[index] = "b";
list[i] = array1[index];
}
}
return list;
}
public static void main(String agrs[]) throws Exception //add songs into output then shuffles it
{
String title[] = new String[20];
String shuffle[] = new String[20];
FileWriter fw = null;
BufferedWriter bw = null;
try
{
int i = 0;
fw = new FileWriter("LastNameFirstPlaylist.txt"); //output
bw = new BufferedWriter(fw);
FileReader read = new FileReader("Playlist.txt"); //input
BufferedReader bread = new BufferedReader(read);
while(bread.readLine() != null) //adds songs into output
{
title[i] = bread.readLine() + " ";
i++;
}
bread.close();
read.close();
}
catch (Exception ex)
{
System.out.println(ex);
}
shuffle = FYShuffling(title,20); //shuffles the songs in the output
for(String string2:shuffle)
{
try
{
bw.write(string2);
}
catch(Exception ex)
{
}
System.out.println(string2); //prints back shuffled songs
}
bw.close();
fw.close();
}
} Description and Requirements: 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 FisherYates 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. Pseudocode:
To compare two text files in Junit, you can try the following pseudocode.
Use BufferedReader to read the input files.
BufferedReader Out=new BufferedReader (new FileReader (
BufferedReader In=new BufferedReader (new FileReader (
while ((expectedLine = In.readLine ()) != null) { String actualLine = Out.readLine ();
assertEquals (expectedLine, actualLine); }
Set seed value as 20.
Random r=new Random();
r.setSeed(20);
Compare the output file with attached see next:
if you use
double d = random.nextDouble();
int j = (int)(d*arr.length);
use Target1.txt
else if you use
Random r = new Random(0);
r.setSeed(20);
for (i = n-1 to 0) {
int j = r.nextInt(i);
swap(arr[i],arr[j]);
}
use Target2.txt. Target 1: https://www.dropbox.com/s/bowpp67vsmp01sx/Target1.txt?dl=0 Target 2: https://www.dropbox.com/s/ysnujma2lxika8m/Target2.txt?dl=0 Playlist: https://www.dropbox.com/s/75ouou9flfqt1vr/Playlist.txt?dl=0 Both Target.txts are just a list of songs to compare to. Playlist is the list of given songs to use. LastNameFirstPlaylist is the shuffled playlist that you're using to compare to either Target1 or Target2.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
