Question
Wouldn't pass the test case /** * Test the writeToFile method * write the array to the outputFile File * then read it back to
Wouldn't pass the test case
/**
* Test the writeToFile method
* write the array to the outputFile File
* then read it back to make sure formatted correctly to read
*
*/
@Test
public void testWriteToFile() {
double[][] array=null;
try {
TwoDimRaggedArrayUtility.writeToFile(dataSet4, outputFile);
} catch (Exception e) {
fail("This should not have caused an Exception");
}
//now read from the output file
try {
array = TwoDimRaggedArrayUtility.readFile(outputFile);
assertEquals(-2.5, array[0][0],.001);
assertEquals(-5.3, array[0][1],.001);
assertEquals(6.1, array[0][2],.001);
assertEquals(-4.4, array[1][0],.001);
assertEquals(8.2, array[1][1],.001);
assertEquals(2.3, array[2][0],.001);
assertEquals(-7.5, array[2][1],.001);
assertEquals(-4.2, array[3][0],.001);
assertEquals(7.3, array[3][1],.001);
assertEquals(-5.9, array[3][2],.001);
assertEquals(2.6, array[3][3],.001);
} catch (FileNotFoundException e) {
fail("This should not have caused an Exception");
}
}
_______________________________________________________________________
public static void writeToFile(double[][] arr, File file){ try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); for(int i = 0; i < arr.length; ++i){ for(int j = 0; j < arr[i].length; ++j){ bw.write(arr[i][j] + " "); } bw.write(" "); } bw.close(); } catch (IOException e) { e.printStackTrace(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started