Java Assignment This assignment works with triangles. We will read lines of data
ID: 3780282 • Letter: J
Question
Java Assignment
This assignment works with triangles. We will read lines of data about triangles. The program will determine what kind of triangles we have. It will also find each triangle's perimeter and area. Name your program AssignEC.java.
Input
We have an input file named triangles.txt containing an unknown number of pairs of lines. Each line contains 3 real numbers (doubles), which are the lengths of the three sides. Use a while loop to read the input file and process each triangle. Do NOT use an array to store anything in this program; it is not necessary.
(Input file: triangles.txt)
Processing
Start by printing a program heading like those in the earlier assignments. The program will read line in the file and calculate the perimeter and the area of the triangle. It will also decide whether this triangle has any of the following characteristics (It can have more than one!):
POSSIBLE Is this a possible triangle?
COLLAPSED Is the area of the triangle zero?
EQUILATERAL Are all three sides equal in length?
ISOCELES Are at least two sides equal in length?
RIGHT Is one of the angles a right angle?
SCALENE Is the triangle scalene?
If the triangle is not possible, do not calculate the perimeter and the area.
The program should also count the triangles of each type and print the results at the end EXACTLY in the order shown below.
Properties of Triangles
To find the perimeter P of a triangle with sides A, B, and C is:
P = A + B + C
To find the area A of a triangle with side A, B and C, we can use the following method:
S = (A + B + C) / 2.0
Area = square root of S * (S - A) * (S - B) * (S - C)
For A, B and C to be the side of a POSSIBLE triangle, the three lengths A, B and C must satisfy all three of these inequalities:
A + B >= C
B + C >= A
C + A >= B
For A, B and C to be sides of a RIGHT triangle, the three lengths A, B and must satisfy one of the following three equations:
A * A + B * B = C * C
B * B + C * C = A * A
C * C + A * A = B * B
For A, B and C to be sides of an EQUILATERAL triangle, the three lengths A, B and must satisfy both of the following equations:
A = B
B = C
For A, B and C to be sides of a ISOCELES triangle, the three lengths A, B and must satisfy one of the following three equations:
A = B
B = C
C = A
A triangle is SCALENE if it is not ISOCELES.
Note: When you are checking equality for floating-point numbers, don't expect to find two numbers that are exactly equal. Instead of looking for "A = = B", use "abs(A - B) < 0.01". We will consider two numbers to be equal if they are close enough, that is, no more than 0.01 apart. Use a named constant for the 0.01 value. The abs function can be found in the Java math library. Refer to your text for common math functions and how to use them
Output Header/Welcome
Output Report
Skip two lines before continuing.
For each triangle, print out the information about that triangle in a format approximately like this sample data:
Be sure that your program is fully documented and that your code is indented and lined up cleanly as necessary.
CSCI 210 Java Extra Credit Assignment Triangles and Their Properties Fall 2016 t*6* n* m*2* n* s* a* s*F* A* t*s* i*e* e*t* r*r* C*e* a*0* r*r* t*P* E*r* a*e* a*T* 2*g* 1*a* S*r C*T*Explanation / Answer
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class AssignEC { private int mNoOfPossible; private int mNoOfImpossible; private int mNoOfRight; private int mNoOfEquilateral; private int mNoOfIsoceles; private int mNoOfScalene; private int mNoOfCollapsed; public static void main(String[] args) { AssignEC assignEC = new AssignEC(); assignEC.printWelcomeMessage(); assignEC.processFile("triangles.txt"); } private void processFile(String fileName) { Scanner scanner = null; try { scanner = new Scanner(new File(fileName)); while (scanner.hasNextLine()) { processTriangle(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble()); } printConsolidatedResults(); } catch (FileNotFoundException e) { e.printStackTrace(); } scanner.close(); } private void processTriangle(double sideA, double sideB, double sideC) { if (checkIfPossible(sideA, sideB, sideC)) { mNoOfPossible++; double area = calculateArea(sideA, sideB, sideC); double perimeter = calculatePerimeter(sideA, sideB, sideC); boolean isCollapsed = doubleEquals(area, 0.0); boolean isRight = checkIfRight(sideA, sideB, sideC); boolean isEquilateral = checkIfEquilateral(sideA, sideB, sideC); boolean isIsoceles = checkIfIsoceles(sideA, sideB, sideC); boolean isScalene = !isIsoceles; updateTrianglesCount(isCollapsed, isRight, isEquilateral, isIsoceles, isScalene); printCalculatedString(sideA, sideB, sideC, perimeter, area); printPropertiesString(isCollapsed, isRight, isEquilateral, isIsoceles, isScalene); } else { mNoOfImpossible++; printSides(sideA, sideB, sideC); printImpossibleProperty(); } } private void updateTrianglesCount(boolean isCollapsed, boolean isRight, boolean isEquilateral, boolean isIsoceles, boolean isScalene) { if (isCollapsed) mNoOfCollapsed++; if (isRight) mNoOfRight++; if (isEquilateral) mNoOfEquilateral++; if (isIsoceles) mNoOfIsoceles++; if (isScalene) mNoOfScalene++; } private void printWelcomeMessage() { StringBuilder builder = new StringBuilder(); builder.append("CSCI 210 - Java Extra Credit Assignment") .append(" *****************************************") .append(" Triangles and Their Properties Fall 2016") .append(" *****************************************") .append(" ") .append("Side A Side B Side C Perimeter Area") .append(" ------ ------ ------ --------- ------"); System.out.println(builder.toString()); } private void printPropertiesString(boolean isCollapsed, boolean isRight, boolean isEquilateral, boolean isIsoceles, boolean isScalene) { StringBuilder builder = new StringBuilder("Properties: POSSIBLE"); if (isCollapsed) builder.append(" COLLAPSED"); if (isRight) builder.append(" RIGHT"); if (isEquilateral) builder.append(" EQUILATERAL"); if (isIsoceles) builder.append(" ISOCELES"); if (isScalene) builder.append(" SCALENE"); System.out.println(builder.toString()); } private void printImpossibleProperty() { System.out.println("Properties: IMPOSSIBLE"); } private void printCalculatedString(double sideA, double sideB, double sideC, double perimeter, double area) { System.out.println(sideA + " " + sideB + " " + sideC + " " + perimeter + " " + area); } private void printSides(double sideA, double sideB, double sideC) { System.out.println(sideA + " " + sideB + " " + sideC); } private void printConsolidatedResults() { StringBuilder builder = new StringBuilder(); builder.append(" Number of POSSIBLE triangles: ").append(mNoOfPossible) .append(" Number of IMPOSSIBLE triangles: ").append(mNoOfImpossible) .append(" Number of RIGHT triangles: ").append(mNoOfRight) .append(" Number of EQUILATERAL triangles: ").append(mNoOfEquilateral) .append(" Number of ISOCELES triangles: ").append(mNoOfIsoceles) .append(" Number of SCALENE triangles: ").append(mNoOfScalene) .append(" Number of COLLAPSED triangles: ").append(mNoOfCollapsed); System.out.println(builder.toString()); } private boolean checkIfPossible(double sideA, double sideB, double sideC) { return (sideA + sideB >= sideC || sideB + sideC >= sideA || sideC + sideA >= sideB) && !(doubleEquals(sideA, 0.0) || doubleEquals(sideB, 0.0) || doubleEquals(sideC, 0.0)); } private boolean checkIfEquilateral(double sideA, double sideB, double sideC) { return doubleEquals(sideA, sideB) && doubleEquals(sideB, sideC); } private boolean checkIfIsoceles(double sideA, double sideB, double sideC) { return doubleEquals(sideA, sideB) || doubleEquals(sideB, sideC) || doubleEquals(sideC, sideA); } private boolean checkIfRight(double sideA, double sideB, double sideC) { return doubleEquals(sideA * sideA + sideB * sideB, sideC * sideC) || doubleEquals(sideB * sideB + sideC * sideC, sideA * sideA) || doubleEquals(sideC * sideC + sideA * sideA, sideB * sideB); } private double calculateArea(double sideA, double sideB, double sideC) { double s = (sideA + sideB + sideC) / 2.0; return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)); } private double calculatePerimeter(double sideA, double sideB, double sideC) { return sideA + sideB + sideC; } private boolean doubleEquals(double a, double b) { return Math.abs(a - b) < 0.01; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.