Part 1: Write a Java program that fulfils the following requirements (80%) The program…
- User
Part 1: Write a Java program that fulfils the following requirements (80%) The program should able to accept three integers representing the three sides of a triangle. Based on the input values, the program should able to determine whether the triangle is: ⚫ Equilateral triangle - all sides with the same length; or ⚫ Isosceles triangle - two sides with the same length; or ⚫ Scalene triangle - all sides are with different lengths; or ⚫ Right angled triangle – one of the angles is 90 degrees. For examples, • If the input values are 10, 10, and 10, the program should indicate that the input values will form an “Equilateral triangle”. • If the input values are 4, 3, and 5, the program should indicate that the input values will form a “Right angled triangle”.
- Assistant
Sure! Below is a Java program that accepts three integers representing the sides of a triangle and determines whether the triangle is Equilateral, Isosceles, Scalene, or Right-angled.
import java.util.Scanner; public class TriangleType { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Accept three integers representing the sides of a triangle System.out.print("Enter side 1: "); int side1 = scanner.nextInt(); System.out.print("Enter side 2: "); int side2 = scanner.nextInt(); System.out.print("Enter side 3: "); int side3 = scanner.nextInt(); // Close the scanner to prevent resource leak scanner.close(); // Check if the sides can form a valid triangle if (isValidTriangle(side1, side2, side3)) { // Check if the triangle is Equilateral if (side1 == side2 && side2 == side3) { System.out.println("The input values will form an Equilateral triangle."); } // Check if the triangle is Right-angled else if (isRightAngled(side1, side2, side3)) { System.out.println("The input values will form a Right angled triangle."); } // Check if the triangle is Isosceles else if (side1 == side2 || side2 == side3 || side1 == side3) { System.out.println("The input values will form an Isosceles triangle."); } // If none of the above, the triangle is Scalene else { System.out.println("The input values will form a Scalene triangle."); } } else { System.out.println("The input values cannot form a triangle."); } } /** * Checks if the three sides can form a valid triangle using the triangle inequality theorem. */ public static boolean isValidTriangle(int a, int b, int c) { // The sum of any two sides must be greater than the third side return (a + b > c) && (a + c > b) && (b + c > a); } /** * Checks if the triangle is right-angled using the Pythagorean theorem. */ public static boolean isRightAngled(int a, int b, int c) { // Sort the sides to identify the hypotenuse (longest side) int[] sides = {a, b, c}; java.util.Arrays.sort(sides); int sideA = sides[0]; int sideB = sides[1]; int hypotenuse = sides[2]; // Apply the Pythagorean theorem return (sideA * sideA + sideB * sideB) == (hypotenuse * hypotenuse); } }Explanation:
-
Input Handling:
- The program uses a
Scannerto read three integer inputs from the user, representing the sides of the triangle.
- The program uses a
-
Valid Triangle Check:
- Before determining the type of triangle, the program checks if the given sides satisfy the triangle inequality theorem using the
isValidTrianglemethod.- The sum of any two sides must be greater than the third side.
- If this condition is not met, the sides cannot form a triangle.
- Before determining the type of triangle, the program checks if the given sides satisfy the triangle inequality theorem using the
-
Determining the Triangle Type:
- Equilateral Triangle:
- All three sides are equal (
side1 == side2 && side2 == side3).
- All three sides are equal (
- Right-Angled Triangle:
- The program checks for a right-angled triangle using the Pythagorean theorem in the
isRightAngledmethod. - It first sorts the sides to identify the hypotenuse (the longest side).
- It then checks if the squares of the other two sides sum up to the square of the hypotenuse.
- This must be checked after checking for an equilateral triangle because an equilateral triangle cannot be right-angled.
- The program checks for a right-angled triangle using the Pythagorean theorem in the
- Isosceles Triangle:
- Two sides are equal (
side1 == side2 || side2 == side3 || side1 == side3).
- Two sides are equal (
- Scalene Triangle:
- If none of the above conditions are met, the triangle is scalene (all sides are of different lengths).
- Equilateral Triangle:
-
Output:
- The program prints out the type of triangle formed by the input values.
Example Runs:
-
Equilateral Triangle Input:
Enter side 1: 10 Enter side 2: 10 Enter side 3: 10 The input values will form an Equilateral triangle. -
Right-Angled Triangle Input:
Enter side 1: 3 Enter side 2: 4 Enter side 3: 5 The input values will form a Right angled triangle. -
Isosceles Triangle Input:
Enter side 1: 5 Enter side 2: 5 Enter side 3: 8 The input values will form an Isosceles triangle. -
Scalene Triangle Input:
Enter side 1: 7 Enter side 2: 8 Enter side 3: 9 The input values will form a Scalene triangle. -
Invalid Triangle Input:
Enter side 1: 1 Enter side 2: 2 Enter side 3: 3 The input values cannot form a triangle.
Notes:
- The program prioritizes checking for an equilateral triangle first because an equilateral triangle is a special case where all sides are equal.
- It checks for a right-angled triangle after checking for an equilateral triangle because an equilateral triangle cannot be right-angled.
- The
isRightAngledmethod uses sorting to ensure that the largest side is considered the hypotenuse. - The program handles integer inputs as specified.
You can compile and run this program using a Java compiler. Ensure that you have Java installed on your system and set up properly.
Compiling and Running the Program:
- Save the code in a file named
TriangleType.java. - Open a command prompt or terminal in the directory containing the file.
- Compile the program with the command:
javac TriangleType.java - Run the compiled program with the command:
java TriangleType
Feel free to test the program with different input values to verify its correctness.
-