public class Triangle extends Object implements Comparable<Triangle>
Triangle
class is immutable class that contains three variables
to store the length of each side of the triangle, and methods to get its area
and perimeter. The class implements Comparable
interface to compare
two triangles. It also overrides equals
, hashCode
, and
toString
from the Object
class.Modifier and Type | Field and Description |
---|---|
private double |
a
The shortest side of the triangle
|
private double |
b
The side of medium length of the triangle
|
private double |
c
The longest side of the triangle
|
private static int |
numTriangles
Stores the number of objects instantiated from the
Triangle class |
Constructor and Description |
---|
Triangle(double side1,
double side2,
double side3)
Initializes the new
Triangle object using the provided sides in the
arguments. |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Triangle t)
Compares area of this triangle object and the other triangle
t . |
boolean |
equals(Object obj)
Compares this triangle with the argument
obj of Object type. |
double |
getA()
Accessor method that returns the smallest side of this triangle.
|
double |
getArea()
This method returns the area of the triangle.
|
double |
getB()
Accessor method that returns the medium side of this triangle.
|
double |
getC()
Accessor method that returns the longest side of this triangle.
|
static int |
getNumTriangles()
This method returns the number of objects instantiated from the
Triangle class. |
double |
getPerimeter()
This method returns the perimeter of the triangle.
|
int |
hashCode()
Generates a hashCode of this triangle using its sides length.
|
boolean |
isEquilateral()
Method to check if a triangle is equilateral triangle by having equal length
sides.
|
static boolean |
isValid(double side1,
double side2,
double side3)
A
Triangle is considered valid if and only if the sum of two sides be
greater than the third side. |
String |
toString() |
private static int numTriangles
Triangle
classprivate final double a
private final double b
private final double c
public Triangle(double side1, double side2, double side3)
Triangle
object using the provided sides in the
arguments. The new triangle will have its shortest side stored in the
variable a
, the medium length side stored in the variable b
,
and the longest side stored in the variable c
.
Before initializing the triangle object, this constructor verifies that the
provided sides are positive and valid as triangle sides otherwise it throws
IllegalArgumentException
. Verifying that the three sides can form a
valid triangle is to be delegated to isValid(double, double, double)
method.
This constructor also increments the static variable
numTriangles
which tracks the number of Triangle objects
created.
side1
- the first side of this triangleside2
- the second side of this triangleside3
- the third side of this triangleIllegalArgumentException
- if the sides are not positive or the provided sides cannot form a
valid trianglepublic static boolean isValid(double side1, double side2, double side3)
Triangle
is considered valid if and only if the sum of two sides be
greater than the third side. This condition has to be true for all three
combinations of the sides.
Example 1: valid triangle
Assume you have a triangle that has the following sides side1
=
5
, side2
= 10
, and side3
= 7
then:
5 + 10 greater than 7 (true), 5 + 7 greater than 10 (true), and
10 + 7 greater than 5 (true)
Example 2: invalid triangle
Assume you have a triangle that has the following sides side1
=
1
, side2
= 2
, and side3
= 7
then:
1 + 2 greater than 7 (false). Then this is not a valid triangle
side1
- the first side of this triangleside2
- the second side of this triangleside3
- the third side of this trianglepublic boolean isEquilateral()
true
if the triangle is equilateral otherwise false
public double getA()
a
of the triangle. This must be the smallest side.public double getB()
b
of the triangle. This must be the medium length
side.public double getC()
c
of the triangle. This must be the longest side.public double getPerimeter()
public double getArea()
a
, b
, and c
is $$sqrt(s * (s - a) * (s - b)
* (s - c))$$ where $s$ is half the perimeter (semiperimeter).public static int getNumTriangles()
Triangle
class.Triangle
objects.public int compareTo(Triangle t)
t
.compareTo
in interface Comparable<Triangle>
t
- the other triangle-1
if the area of this triangle is less than the
argument's triangle area; +1
if the area of this triangle is
greater than the argument's; and 0
if this area of this
triangle is equal to the argument's within the specified tolerance
ShapeUtil.TOL
.IllegalArgumentException
- if the argument is nullpublic int hashCode()
public boolean equals(Object obj)
obj
of Object type. This
method overrides the base method defined in the root class Object.
Two triangles are equal if the difference between the sides of the first
triangle and the sides of second triangle is within the allowed tolerance
specified in ShapeUtil.TOL
equals
in class Object
true
if the triangle is equal to the argument triangle within
the tolerance specified in ShapeUtil.TOL
.