Question: Write complete Java programs to complete the following tasks. Q2: Custom exception class for triangle sides For a triangle with three sides of length a,b,c
Write complete Java programs to complete the following tasks.
Q2: Custom exception class for triangle sides
For a triangle with three sides of length a,b,c to be valid,
the length of each side must be greater than 0, and
the sum of any two sides must be greater than the third side by length
Design a Triangle class that
extends the GeometricObject class below
class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; public GeometricObject() { dateCreated = new java.util.Date(); } public GeometricObject(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) { this.color = color; } public boolean isFilled() { return filled; } public void setFilled(boolean filled) { this.filled = filled; } public java.util.Date getDateCreated() { return dateCreated; } public String toString() { return "created on " + dateCreated + " color: " + color + " and filled: " + filled; } } add three sides a, b, c as private data fields with the following framework
public class Triangle extends GeometricObject { private double a, b, c; public Triangle(double a, double b, double c) { // TODO: // if invalid throw InvalidTriangleException // else assign the tree sides respectively } } Put the Triangle class and GeometricObject in the same file.
It is OK if you put them in different files.
Refer to example Circle
Design the custom exception class InvalidTriangleException
public class InvalidTriangleException extends RuntimeException { private double a, b, c; public InvalidTriangleException(){} public InvalidTriangleException(double a, double b, double c) { // TODO: // use keyword super to invoke the constructor of RuntimeException with an error message // set the three sides } // TODO: // complete three public getters } Refer to example InvalidRadiusException
Design a test class to create three triangles to trigger the exception with the second triangle
Triangle(3,4,5); Triangle(1,2,3); Triangle(10,4,5);
Refer to example TestCircle
Test cases
InvalidTriangleException: Invalid triangle with sides: 1.0, 2.0, 3.0 Created triangle with sides: 3.0, 4.0, 5.0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
