For the code of the function listed next Implement test harn

For the code of the function listed next. Implement test harness to unit test the method. Perform basic path analysis to identify inputs and expected outputs. Use this set of inputs/outputs in your test harness of the unit test. Perform equivalent partition of input space to determine inputs and outputs. Use this set of inputs/outputs in your test harness of the unit test. Check for exceptions if any

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileRead{
public static void main(String[] args) throws IOException {
File file = new File(\"file.txt\");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int total = 0;
int counter = 0;
String line = \" \"; // reading first line
while((line=br.readLine()) != null){
String[] valueStr = line.trim().split(\"\\\\s+\"); // spliting line by space
int field1 = Integer.parseInt(valueStr[0]); // reading first int of a line
int field2 = Integer.parseInt(valueStr[1]); // second int
  
if(field1 == 0){
total = total + field1;
counter++;
}
else{
if(field2==0){
//printing total and counter
System.out.println(\"Total: \"+total+\", Counter: \"+counter);
counter = 0; // reseting counter
}
else{
total = total- field2;
}
}
System.out.println(\"End Record\");
}
System.out.println(\"Counter: \"+counter);
}
}

Solution


package fileread;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileRead{
public static void main(String[] args) throws IOException {
try{   
File file = new File(\"D:/input.txt\");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int total = 0;
int counter = 0;
String line = \" \"; // reading first line
while((line=br.readLine()) != null){
String[] valueStr = line.trim().split(\"\\\\s+\"); // spliting line by space
int field1 = Integer.parseInt(valueStr[0]); // reading first int of a line
int field2 = Integer.parseInt(valueStr[1]); // second int
  
if(field1 == 0){
total = total + field1;
counter++;
}
else{
if(field2==0){
//printing total and counter
System.out.println(\"Total: \"+total+\", Counter: \"+counter);
counter = 0; // reseting counter
}
else{
total = total- field2;
}
}
System.out.println(\"End Record\");
}
System.out.println(\"Counter: \"+counter);
}
catch(Exception e) // handled the expection
{
System.out.println(e);
  
}
}
}

Unit Test:

In this test we test our code for each input and observe its behaviour.

>>>For input in file \"int the\" error is \"java.lang.NumberFormatException: For input string: \"int\"\"

>>>for input in file

1

2

3

exception is \"java.lang.ArrayIndexOutOfBoundsException: 1\"

>>>for input in file \"1 2 3 4 \" output

End Record
java.lang.NumberFormatException: For input string: \"\"

>>> and if input is in the form of

0 2

3 4

5 0

output is

End Record
End Record
Total: -4, Counter: 1
End Record
Counter: 0

>>> for input in the form of

1 2

3 4

5 6

output is

End Record
End Record
End Record
Counter: 0

For the code of the function listed next. Implement test harness to unit test the method. Perform basic path analysis to identify inputs and expected outputs. U
For the code of the function listed next. Implement test harness to unit test the method. Perform basic path analysis to identify inputs and expected outputs. U
For the code of the function listed next. Implement test harness to unit test the method. Perform basic path analysis to identify inputs and expected outputs. U

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site