Description: The following program takes three numbers as input and checks to see if these may form a triangle or not.
Primary Inputs: Three numbers
Primary Output: Determine the type of triangle formed by the input numbers (if any).
Platform Used: JDK 1.6 with Notepad.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.io.*; class TriangleTest { public static void main(String[] args) { Console con = System.console(); System.out.print("Enter the first number: "); double side1 = Double.parseDouble(con.readLine()); System.out.print("Enter the second number: "); double side2 = Double.parseDouble(con.readLine()); System.out.print("Enter the third number: "); double side3 = Double.parseDouble(con.readLine()); if (isTriangle(side1, side2, side3)) { getType(side1, side2, side3); } } static boolean isTriangle(double side1, double side2, double side3) { if (side1 <= 0 || side2 <= 0 || side3 <= 0) { System.out.println("Do not represent a triangle"); return false; } else if ((side1 + side2 <= side3) || (side1 + side3 <= side2) || (side3 + side2 <= side1)) { System.out.println("Do not represent a triangle"); return false; } return true; } static void getType(double a, double b, double c) { if (a == b && b == c) System.out.println("This is an equilateral triangle!"); else if (a == b || b == c || a == c) System.out.println("This is an isosceles triangle!"); else if (isRightTriangle(a, b, c)) System.out.println("This is a right triangle!"); else System.out.println("This is a triangle!"); } static boolean isRightTriangle(double a, double b, double c) { if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (c * c + b * b == a * a)) return true; return false; } } |