/** Triangle.java * Java program which, given three sides of a triangle, determines whether the triangle is * equilateral, isosceles or scalene. * The program checks if the lenght of the sides are valaid and form a triangle . If not it will prompt * for new input. * */ import java.io.*; // java Input/Output package public class Triangle // declares class Triangle // fields { static double first; static double second; static double third; public void startTest() { while (true) { if(!testSuccess()) break; } } public boolean testSuccess() // begin testSuccess method { InputStreamReader stdin = new InputStreamReader(System.in); // class reads characters BufferedReader console = new BufferedReader(stdin); // class buffereing character streams String s1; // input string for triangle sides. String s2; String s3; String c; System.out.println("Please enter side A of the triangle"); try // prompt and input { s1 = console.readLine(); // reading a string first = Double.parseDouble(s1); // parsing a double value from the string if (first > 100.0 || first <= 0) { System.out.println("Side A must be a positive number not greater than 100.0"); return true; } } catch(IOException ioex) // exeption handling { System.out.println("Input error"); // error output if invalid data System.exit(1); // program terminates } System.out.println("Please enter side B of the triangle"); try // prompt and input { s2 = console.readLine(); // reading a string second = Double.parseDouble(s2); // parsing a double value from the string if (second > 100.0 || second <= 0) { System.out.println("Side B must be a positive number not greater than 100.0"); return true; } } catch(IOException ioex) // exeption handling { System.out.println("Input error"); // error output if invalid data System.exit(1); // program terminates } System.out.println("Please enter side C of the triangle"); try // prompt and input { s3 = console.readLine(); // reading a string third = Double.parseDouble(s3); // parsing a double value from the string if (third > 100.0 || third <= 0) { System.out.println("Side C must be a positive number not greater than 100.0"); return true; } } catch(IOException ioex) // exeption handling { System.out.println("Input error"); // error output if invalid data System.exit(1); // program terminates } if (isValidValue(first, second, third)) //if the line length is valid, then decide the type of the triangle compare(first, second, third); else { System.out.println("Invalid line lengths. Cannot form a triangle. Please retry."); return true; // otherwise, ask for another set of inputs } while (true) { System.out.println("Do you wish to continue? (y/n): "); try // prompt and input { c = console.readLine(); // reading a string if (c.equals("y")) break; if (c.equals("n")) return false; else System.out.println("enter either y for yes or n for no"); } catch(IOException ioex) // exeption handling { System.out.println("Input error"); // error output if invalid data System.exit(1); // program terminates } } return true; } // end method testSuccess //test whether the input line length can form a triangle private boolean isValidValue(double first, double second, double third) { //a triangle can be formed if the sum of any two of the line length is greater than the length of the third line if ((first + second) > third && (first + third) > second && (second + third) > first) return true; else return false; } private void compare(double first, double second, double third) { if (first == second && second == third) { System.out.println("The triangle is equilateral"); return; } if (first == second || first == third || second == third) { System.out.println("The triangle is isosceles"); return; } else System.out.println("The triangle is scalene"); return; } } // end class Triangle