A. Working at Music Best
You are planning a road trip and want to create a playlist ofyour favorite songs. Assume that the song titles are in an array ofstrings. Create a shuffle of your songs (permutation of youroriginal songs). Use the Fisher-Yates shuffle algorithm that worksin O(n) running time. We will use a method that createspseudo-random numbers (see end for help) in O(1) running time. Thebasic idea is to start from the last element, swap it with arandomly selected element from the whole array (including last). Inthe next step, you will consider the array from 0 to n-2 (sizereduced by1), and repeat the process until you reach the firstelement. Write a program that uses the provided Playlist.txt asinput and outputs the shuffled array in a file calledLastNameFirstNamePlaylist.txt. Follow the next pseudocode: Toshuffle an array a of n elements (indices 0..n-1):
for i=n-1 down to 1 j= random integer with 0 <= j < i
exchange a[j] and a[i]
Count the time to read from the file, to shuffle the songs andto create the output. Note: To count the time usesystem.currentTimeMillis().
Create appropriate JUnits to test your program. Help withJUnits:
Instructions for developing JUnit:
• To compare two text files in Junit, you can try the followingcode. 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 nextDouble() use Target1.txt to compare
double d = random.nextDouble();
int j = (int)(d*arr.length);
else if you use nextInt() use Target2.txt
Programming Standards:
• Your header comment must describe what your program does.
• You must include a comment explaining the purpose of everyvariable or named constant you use in your program.
• You must use meaningful identifier names that suggest themeaning or purpose of the constant, variable, function, etc.
• Precede every major block of your code with a commentexplaining its purpose. You don't have to describe how it worksunless you do something tricky.
• You must use indentation and blank lines to make controlstructures more readable.
Deliverables:
Your main grade will be based on (a) how well your tests coveryour own code, (b) how well your code does on your tests (createfor all non-trivial methods), and (c) how well your code does on mytests (which you have to add to your test file). For JUnit testscheck canvas.
Use cs146S19..project1 as your package, and Testclasses should be your main java file, along with your JUnit javatests.
Do not use any fancy libraries. We should be able to compile itunder standard installs. Include a readme file on how to compilethe project.
***********************************************************************************************************************************************************************************************************
MY SOLUTION:
package RandomMusic;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
*playMusicTest.java
public class playMusicTest {
@Test
public void playMusictest() throws IOException
{
 Â
File file = new File(\"src\\RandomMusic\\Playlist.txt\");
File file1 = new File(\"src\\RandomMusic\\Target1.txt\");
// read input playlist
BufferedReader in = new BufferedReader(new FileReader(file));
// read output target
BufferedReader out = new BufferedReader(newFileReader(file1));
 Â
String[] playList = new String[459]; // create an array
int i=0; // element of array
String str;
 Â
// read content of Playlist.txt then copy the content toarray
while((str = in.readLine())!=null)
{
playList[i] = str;
i++;
}
 Â
// random number
Random rand = new Random(0);
rand.setSeed(20);
 Â
// swap the chosing song.
for(int j = playList.length - 1;j>0;j--)
{
int index = rand.nextInt(j);
String tmp;
tmp = playList[index];
playList[index] = playList[j];
playList[j]= tmp;
}
 Â
// compare output of playlist = content of target file
for(int k=0; k{
String actualLine = out.readLine();
assertEquals(playList[k],actualLine);
 Â
}
 Â
}
}
*RandomMusic.java
package RandomMusic;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
import java.io.*;
public class RandomMusic {
 Â
String[] playList = new String[459]; // String of playList
int i = 0;
Random rand; // random
BufferedReader in; // read file
 Â
 Â
// Open a Text File
public void openFile() throws IOException
{
try {
File file = new File(\"src\\RandomMusic\\Playlist.txt\");
in = new BufferedReader(new FileReader(file));
String str;
while((str = in.readLine())!=null)
{
playList[i] = str;
i++;
}
 Â
} catch(Exception e) {
System.out.println(\"Could not find the data file!\");
}
 Â
}
 Â
// close File
public void closeFile() throws IOException
{
in.close();
 Â
}
// Swap song in playlist
public void swap(int index, int j)
{
String tmp;
tmp = playList[index];
playList[index] = playList[j];
playList[j]= tmp;
}
 Â
// Random song in playlist then swap if the song chose
public void playMusic()
{
rand = new Random();
for(int j = playList.length - 1;j>0;j--)
{
int index = rand.nextInt(j);
swap(index,j);
}
}
 Â
// print out playlist
public void printOut()
{
for(int j=0; j{
System.out.println(playList[j]);
}
}
 Â
// Main program
public static void main(String[] args) throws IOException
{
RandomMusic favorMusic = new RandomMusic(); // createRandomMusic
favorMusic.openFile(); // read file
favorMusic.playMusic(); // random music
favorMusic.printOut(); // print out playlist
favorMusic.closeFile(); // close file
}
}
*openFileTest
package RandomMusic;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
// Check playlist file is exists
public class openFileTest {
@Test
public void openFiletest() throws IOException
{
File file = new File(\"src\\RandomMusic\\Playlist.txt\");
assertTrue(file.exists());
}
 Â
}
************************************************
I am getting error in playMusicTest.java.... Please help.