change the following codes: public class Assignment3 { /** * Use this template t
ID: 645752 • Letter: C
Question
change the following codes:
public class Assignment3 {
/**
* Use this template to complete assignment 3. Only the code found in
* the "createTriangles" method will be considered for grading
* (We are automating the testing for this assignment.
* DO NOT EDIT THE MAIN METHOD, except for the values of
* INPUT and OUTPUT
* @param args
*/
public static void main(String[] args)
{
// Change these according to your environment
final String INPUT = "C:/testFiles/in/goodInput1.txt";
final String OUTPUT = "C:/testFiles/out/triangles1.txt";
boolean status = Assignment3.createTriangles(INPUT, OUTPUT);
if (status)
{
System.out.println("OK");
}
else
{
System.out.println("Check your input or output files.");
}
}
/**
* This method will take the read integers from the provided input file location.
* For each integer pair, it will calculate the third side of the Pythagorean theorem.
* The third sides will be placed within the output file.
* If there is a problem with a integer pair, the word "ERROR" will be placed in the
* output file. If there is a problem with the input and output file IO, then the
* method will return false. Otherwise, the method returns true.
* @param inputFile The input file location.
* @param outputFile The output file location.
* @return True, unless there is a problem with the input or output file.
*
*/
public static boolean createTriangles(String inputFile, String outputFile)
{
// Place all your code here
return true;
}
// You may also add methods here
}
Requirment:
triangle:
a2 + b2 = c2
which can alternatively be written as:
c = ?(a2 + b2)
Most of this expression contains simple operators covered in Chapter 3. The one piece that
Explanation / Answer
/**
* The java program that reads a text file called
* goodInput1.txt file that contains pair of
* values of a triangles.
* Read each pair and find third side value
* and write to output file triangles1.txt.
* If value is exist for pair, then write
* an error message to the file.
* */
//Assignment3 .java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Assignment3
{
/**
* Use this template to complete assignment 3. Only the code found in
* the "createTriangles" method will be considered for grading
* (We are automating the testing for this assignment.
* DO NOT EDIT THE MAIN METHOD, except for the values of
* INPUT and OUTPUT
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException
{
// Change these according to your environment
final String INPUT = "goodInput1.txt";
final String OUTPUT = "triangles1.txt";
//The method returns false if there is a error in INPUT or OUTPUT file
//reading
boolean status = Assignment3.createTriangles(INPUT, OUTPUT);
if (status)
{
System.out.println("OK");
}
else
{
System.out.println("Check your input or output files.");
}
}
/**
* This method will take the read integers from the provided input file location.
* For each integer pair, it will calculate the third side of the Pythagorean theorem.
* The third sides will be placed within the output file.
* If there is a problem with a integer pair, the word "ERROR" will be placed in the
* output file. If there is a problem with the input and output file IO, then the
* method will return false. Otherwise, the method returns true.
* @param inputFile The input file location.
* @param outputFile The output file location.
* @return True, unless there is a problem with the input or output file.
* @throws FileNotFoundException
*
*/
public static boolean createTriangles(String inputFile, String outputFile)
throws FileNotFoundException
{
int sideA=0;
int sideB = 0;
Scanner fileReader=null;
PrintWriter writer=new PrintWriter(outputFile);
File inFile=null;
boolean error=false;
boolean ioerror=false;
try
{
//open an file
inFile=new File(inputFile);
//Create file input stream of Scanner class
fileReader=new Scanner(inFile);
while(fileReader.hasNextInt())
{
//read sideA value
sideA=fileReader.nextInt();
//Then check for next int
if(fileReader.hasNextInt())
//iF next is present then read
sideB=fileReader.nextInt();
else
{
//Otherwise
//write Error message if the file has two values to
//calculate sideC value
writer.println("ERROR");
error = true;
}
if(!error)
{
double sideC=Math.sqrt(Math.pow(sideA, 2.0)+Math.pow(sideB, 2.0));
writer.println(sideC);
}
}
//close the input and output file streams
fileReader.close();
writer.close();
}
catch (Exception e)
{
ioerror=true;
System.out.println(e.getMessage());
}
return ioerror;
}
// You may also add methods here
}
-----------------------------------
Sample input file
goodinput1.txt file
91
32
73
62
78
11
-------------------------------------------
sample output:
triangles.txt
96.46242791885346
95.77577981932593
78.77182237323191
Note : input file must be in the current working directory .
Add extra value to the input file so that you get an errro in output file for that case.
Hope this could be helpful
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.