Question

First, design and implement a class Triangle, which includes the attributes -- sideA, sideB, and sideC;...

First, design and implement a class Triangle, which includes the attributes -- sideA, sideB, and sideC; behaviors: regular constructor, mutators, assessors, and area(). Second, design user-defined Exception to handle the following two execution error: negative edge value the sum of two sides' length is small than that of the third side.

In Java and Test please

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Triangle {

    public static class InvalidTriangleException extends Exception {
        public InvalidTriangleException() {
        }

        public InvalidTriangleException(String message) {
            super(message);
        }
    }

    private double sideA, sideB, sideC;

    public Triangle() {
    }

    public Triangle(double sideA, double sideB, double sideC) throws InvalidTriangleException {
        setSideA(sideA);
        setSideB(sideB);
        setSideC(sideC);
    }

    public double getSideA() {
        return sideA;
    }

    public void setSideA(double sideA) {
        this.sideA = sideA;
    }

    public double getSideB() {
        return sideB;
    }

    public void setSideB(double sideB) {
        this.sideB = sideB;
    }

    public double getSideC() {
        return sideC;
    }

    public void setSideC(double sideC) {
        this.sideC = sideC;
    }

    private void validateSide(double side) throws InvalidTriangleException {
        if (side < 0) {
            throw new InvalidTriangleException("error: negative edge value");
        } else if ((sideA >= sideB + sideC) || (sideB >= sideA + sideC) || (sideC >= sideA + sideB)) {
            throw new InvalidTriangleException("error: the sum of two sides' length is small than that of the third side");
        }
    }

    public double area() throws InvalidTriangleException {
        validateSide(sideA);
        validateSide(sideB);
        validateSide(sideC);
        double p = (sideA + sideB + sideC) / 2;
        return Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
    }
}

class TestTriangle {

    public static void main(String[] args) {
        try {
            Triangle t1 = new Triangle();
            t1.setSideA(3);
            t1.setSideB(4);
            t1.setSideC(5);
            System.out.println("Area is " + t1.area());
        } catch (Triangle.InvalidTriangleException e) {
            System.out.println(e.getMessage());
        }

        try {
            Triangle t1 = new Triangle();
            t1.setSideA(3);
            t1.setSideB(4);
            t1.setSideC(10);
            System.out.println("Area is " + t1.area());
        } catch (Triangle.InvalidTriangleException e) {
            System.out.println(e.getMessage());
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
First, design and implement a class Triangle, which includes the attributes -- sideA, sideB, and sideC;...
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
  • 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...

  • The Triangle Inequality Theorem states that the sum of any two sides is greater than the...

    The Triangle Inequality Theorem states that the sum of any two sides is greater than the other side. Write in Java a Triangle class that has • a constructor that takes the values of three sides as it's parameters, checks if the triangular inequality theorem is satisfied and assigns the values to the class variables. If the triangular inequality is not satisfied, the constructor throws an exception. • a toString() method that returns the three sides of the triangle. The...

  • (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...

  • Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class....

    Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject and then implement the Triangle class. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input....

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • (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...

  • JAVA you will design and write a class that represents a real-world object of your choice....

    JAVA you will design and write a class that represents a real-world object of your choice. In the second part, you will write a program that demonstrates the use of the class. Part I: Select a "real-world" object that has not been used in class lecture and/or the textbook. The object you choose must be defined by at least: Have at least two characteristics (attributes). Have at least two behaviors (operations). The class that you write to represent the object...

  • C++ question-classes ? Implement a Triangle class in C++. The triangle is defined by its three...

    C++ question-classes ? Implement a Triangle class in C++. The triangle is defined by its three side lengths - a, b, and c. The class includes appropriate class constructors and public and private methods that perform the following operations: is_triangle - checks whether the given side lengths form a proper triangle; area - returns the area of the triangle; perimeter - returns the perimeter of the triangle; angle_a, angle_b, angle_c - return the vertex angles (in degrees) opposite to side...

  • Java - In NetBeans IDE: •Design an abstract class called BankAccount which stores the balance. –Implement...

    Java - In NetBeans IDE: •Design an abstract class called BankAccount which stores the balance. –Implement a constructor that takes the balance (float). –Provide abstract methods deposit(amount) and withdraw(amount) •Design a class called SavingsAccount which extends BankAccount. –Implement a constructor that takes the balance as input. –It should store a boolean flag active. If the balance at any point falls below $25, it sets the active flag to false. It should NOT allow the balance to fall below $0. –If...

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