Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A. Working at Music Best You are planning a road trip and want to create a playlist of your favorite songs. Assume that the song

A. Working at Music Best

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 by1), 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=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 and to create the output. Note: To count the time use system.currentTimeMillis().

Create appropriate JUnits to test your program. Help with JUnits:

Instructions for developing JUnit:

To compare two text files in Junit, you can try the following code. 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 every variable or named constant you use in your program.

You must use meaningful identifier names that suggest the meaning or purpose of the constant, variable, function, etc.

Precede every major block of your code with a comment explaining its purpose. You don't have to describe how it works unless you do something tricky.

You must use indentation and blank lines to make control structures more readable.

Deliverables:

Your main grade will be based on (a) how well your tests cover your own code, (b) how well your code does on your tests (create for all non-trivial methods), and (c) how well your code does on my tests (which you have to add to your test file). For JUnit tests check canvas.

Use cs146S19..project1 as your package, and Test classes should be your main java file, along with your JUnit java tests.

Do not use any fancy libraries. We should be able to compile it under standard installs. Include a readme file on how to compile the project.

***********************************************************************************************************************************************************************************************************

*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

}

*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 have changed the playMusicTest.java as below

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.io.InputStreamReader; import java.util.Random;

public class playMusicTest {

@Test public void playMusictest() throws IOException { //File file = new File("src\RandomMusic\Playlist.txt"); File file1 = new File(playMusicTest.class.getClassLoader() .getResource("RandomMusic/Target1.txt").getFile()); // read input playlist BufferedReader in = new BufferedReader(new InputStreamReader( playMusicTest.class.getClassLoader() .getResourceAsStream("RandomMusic/Playlist.txt"))); // read output target BufferedReader out = new BufferedReader(new FileReader(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 to array 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 < playList.length; k++) { String actualLine = out.readLine(); assertEquals(playList[k], actualLine);

}

}

}

************************************

I am getting nullpointer exception for the playMusicTest. It shows that test pass:1/1 and erros 1. Please provide me with the precise answer. Thanks a lot!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions