Q:

Write a java class Triangle that has the three sides of the triangle as attributes. It also has the following methods

0

The triangle inequality principle states that the sum of the lengths of any two sides of a triangle always exceeds the length of the third side.

Pythagoras' theorem states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides

 

Write a java class Triangle that has the three sides of the triangle as attributes. It also has the following methods:

isItTriangle(): boolean. Returns true if the three sides accurately represents a triangle. (Any side cannot exceed the sum of both other sides). Otherwise returns false.

isItRight(): boolean. Returns true if the all three sides represent a triangle and it is a right angle tiangle satisfying Pythagoras theorm. otherwise returns false.

longest(): double. Returns the longest side.

Triangle

+ side1 : double

+ side2 : double

+ side3 : double

+ isItTriangle(): boolean

+ isItRight(): boolean

+ longets():double

Write a class testTriangle that:

Declare a Triangle object t and prompts the user to enter the lengths of three sides of a triangle.

displays a message indicating whether the lengths correctly represent a triangle, and if it is, displays a message indicating whether the triangle is a right triangle.

Sample Run 1

Enter the three sides 4 6 8

It is a triangle

Not a Right angle triangle

The longest side is 8.0

 

Sample Run 2

Enter the three sides 1 2 8

It is not a triangle

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the three sides: ");

        if(scanner.hasNextDouble()) {
            double side1 = scanner.nextDouble();

            if(scanner.hasNextDouble()) {
                double side2 = scanner.nextDouble();

                if(scanner.hasNextDouble()) {
                    double side3 = scanner.nextDouble();

                    Triangle t = new Triangle(side1, side2, side3);

                    if (t.isItTriangle()) {
                        System.out.println("It is a triangle");
                        if (t.isItRight()) {
                            System.out.println("It is a right angle triangle");
                        } else {
                            System.out.println("Not a right angle triangle");
                        }
                        System.out.println("The longest side is " + t.longest());
                    } else {
                        System.out.println("It is not a triangle");
                    }
                } else {
                    System.out.println("Invalid input: please enter three numeric values");
                }
            } else {
                System.out.println("Invalid input: please enter three numeric values");
            }
        } else {
            System.out.println("Invalid input: please enter three numeric values");
        }
        scanner.close();
    }

    public static class Triangle {
        private double side1;
        private double side2;
        private double side3;

        public Triangle(double side1, double side2, double side3) {
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
        }

        public boolean isItTriangle() {
            if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
                return true;
            } else {
                return false;
            }
        }

        public boolean isItRight() {
            if (isItTriangle()) {
                double hypotenuse = Math.max(Math.max(side1, side2), side3);
                double sumOfSquares = Math.pow(side1, 2) + Math.pow(side2, 2) + Math.pow(side3, 2) - Math.pow(hypotenuse, 2);
                if (sumOfSquares == 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        public double longest() {
            return Math.max(Math.max(side1, side2), side3);
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Create the class TV with the following UML... >>