Question

(JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore,...

(JAVA)

Implement a Triangle class. Any triangle can be represented by its THREE sides.

Therefore, your class will have THREE private member variables → side1, side2 and side3. Use the double data type to represent the triangle sides.

In addition, please provide public methods that perform the following FIVE tasks:

▪ An input method that obtains the appropriate values for the three sides from the user. While entering the values of the three sides, please remember the triangle property that the sum of ANY two sides needs to be greater than the third side.

▪ A method that calculates and returns the Perimeter of the Triangle: The perimeter of a triangle is simply the sum of all three sides of the triangle. ▪ An output method that prints all three sides of the Triangle and other information (as show in the sample I/O) on the user screen. (No other methods should print out any information)

▪ A boolean method that determines whether the triangle is Equilateral: A triangle is equilateral if and only if all the THREE sides of the triangle are equal. If the triangle is equilateral, a message conveying this should be sent to the screen. If the triangle is not equilateral, then the corresponding message should be sent to the screen (for example, if a = 3.4, b = 5.6 and c = 23.7, then the output message should read → the triangle IS NOT equilateral).

• A method that calculates and returns the area of the triangle: The area of a triangle is given by the following formula: Given the three sides of the triangle (say a, b and c), S = (a + b + c)/2. Then, Area = square root (S * (S – a) * (S – b) * (S – c)).

Write the corresponding main method that tests the Triangle class and produce the following outputs:

Here is some sample output:

Please enter the value for side 1: 12.1

Please enter the value for side 2: 14.1

Please enter the value for side 3: 15. 1

The three sides that you entered are: 12.1, 13.1 and 14.1

The perimeter of the triangle is: 41.3 T

he triangle IS NOT equilateral.

The area of the triangle is: 80.11.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Triangles {

        public static double perimeter(double lengths[]) {
                return lengths[0] + lengths[1] + lengths[2];
        }

        public static boolean isEquilateral(double lengths[]) {
                return (lengths[0] == lengths[1]) && (lengths[1] == lengths[2]);
        }

        public static double area(double lengths[]) {
                double p = (lengths[0] + lengths[1] + lengths[2]) / 2.0;
                return Math.sqrt(p * (p - lengths[0]) * (p - lengths[1]) * (p - lengths[2]));
        }
        
        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);

                double lengths[] = new double[3];
                for (int i = 0; i < 3; i++) {
                        System.out.print("Enter length of side " + (i + 1) + ": ");
                        try {
                                lengths[i] = Double.parseDouble(in.next());
                                if (lengths[i] <= 0) {
                                        System.out.println("Error: Length can not be 0 or lesser.");
                                        i--;
                                }
                        } catch (NumberFormatException e) {
                                System.out.println("Error: Invalid number");
                                i--;
                        }
                }

                System.out.println("Three sides that you enetered are: ");
                System.out.println(lengths[0] + " " + lengths[1] + " " + lengths[2]);
                
                System.out.println("The perimeter of the triangle is: " + perimeter(lengths));
                if (isEquilateral(lengths)) {
                        System.out.println("The triangle IS equilateral.");
                } else {
                        System.out.println("The triangle IS NOT equilateral.");
                }
                
                System.out.printf("The area of the triangle is: %.2f\n", area(lengths));

                in.close();
        }

}

) return (lengths|θ] lengths!!]) && (lengths | 1] lengths[2]); := -= <terminated»Triangles (2) Dava Application CProgram File


   Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
(JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Must be in Python 3.6 (please have a screen shot on the code) ex 2.14 :...

    Must be in Python 3.6 (please have a screen shot on the code) ex 2.14 : # Enter three points for a triangle x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle: ")) # Compute the length of the three sides side1 = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ** 0.5 side2 = ((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 -...

  • Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...

    Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • Java Please - Design and implement a class Triangle. A constructor should accept the lengths of...

    Java Please - Design and implement a class Triangle. A constructor should accept the lengths of a triangle’s 3 sides (as integers) and verify that the sum of any 2 sides is greater than the 3rd(i.e., that the 3 sides satisfy the triangle inequality). The constructor should mark the triangle as valid or invalid; do not throw an exception. Provide get and set methods for the 3 sides, and recheck for validity in the set methods. Provide a toString method...

  • Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3,...

    Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3, y3) of a triangle and displays its area. The formula for computing the distance of two points (x1, y1) and (x2, y2) is d = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); or d = Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5); The formula for computing the...

  • this is what i have so far but it does not work. please help thank you...

    this is what i have so far but it does not work. please help thank you Part 2: Perimeter of a Triangle Write a program named Triangle.java that asks for the lengths of the three sides of a triangle and computes the perimeter if the input is valid.The input is valid if the sum of every pair of two sides is greater than the remaining side. For example, the lengths 3, 4, and 5 define a valid triangle: 3 plus...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • Write the java program: A right triangle can have sides whose lengths are all integers. The...

    Write the java program: A right triangle can have sides whose lengths are all integers. The set of three integer values for the length of the sides of a triangle is called a Pythagorean triple. The length of the three sides must satisfy the relationship that the sum of the squares of the sides is equal to the square of the hypotenuse. Write a Java application that prompts the user for an integer that represents the largest side value and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT