In: Computer Science
Using Java
Create the class RightTriangle which is a triangle with one of its angles equal to 90º – a “right angle”.
a2 + b2 = c2.
You also should recalculate the hypotenuse every time one of the two legs changes. How would you do this?
In your main method, create a right triangle with legs of length 3 and 4, use toString to print the lengths of all sides, the perimeter, and the area. Do the same using legs of length 5 and 12.
Triangle.java
public class Triangle{
private double length1, length2, length3;
public Triangle(double len1, double len2, double len3)
{
this.length1 = len1;
this.length2 = len2;
this.length3 = len3;
}
public double getLength1() {
return length1;
}
public void setLength1(double length1) {
this.length1 = length1;
}
public double getLength2() {
return length2;
}
public void setLength2(double length2) {
this.length2 = length2;
}
public double getLength3() {
return length3;
}
public void setLength3(double length3) {
this.length3 = length3;
}
@Override
public int getNumberOfSides(){ return 3; }
@Override
public String toString()
{
return("Number of sides: " + getNumberOfSides() + ", Side1: " +
String.format("%.2f", getLength1())
+ ", Side2: " + String.format("%.2f", getLength2())
+ ", Side3: " + String.format("%.2f", getLength3())
+ ", Area: " + String.format("%.2f", super.getArea())
+ ", Perimeter: " + String.format("%.2f",
super.getPerimeter()));
}
@Override
public boolean equals(Object object) {
if(object instanceof Triangle)
{
return(getLength1() == ((Triangle) object).getLength1()
&& getLength2() == ((Triangle) object).getLength2()
&& getLength3() == ((Triangle) object).getLength3());
}
else
return false;
}
@Override
public void calculatePerimeter() {
setPerimeter(length1 + length2 + length3);
}
@Override
public void calculateArea() {
double p = super.getPerimeter() / 2;
double area = Math.sqrt(p * (p - length1) * (p - length2) * (p -
length3));
super.setArea(area);
}
}
public class Triangle{
private double length1, length2, length3;
public Triangle(double len1, double len2, double len3)
{
this.length1 = len1;
this.length2 = len2;
this.length3 = len3;
}
public double getLength1() {
return length1;
}
public void setLength1(double length1) {
this.length1 = length1;
}
public double getLength2() {
return length2;
}
public void setLength2(double length2) {
this.length2 = length2;
}
public double getLength3() {
return length3;
}
public void setLength3(double length3) {
this.length3 = length3;
}
@Override
public int getNumberOfSides(){ return 3; }
@Override
public String toString()
{
return("Number of sides: " + getNumberOfSides() + ", Side1: " + String.format("%.2f", getLength1())
+ ", Side2: " + String.format("%.2f", getLength2())
+ ", Side3: " + String.format("%.2f", getLength3())
+ ", Area: " + String.format("%.2f", super.getArea())
+ ", Perimeter: " + String.format("%.2f", super.getPerimeter()));
}
@Override
public boolean equals(Object object) {
if(object instanceof Triangle)
{
return(getLength1() == ((Triangle) object).getLength1()
&& getLength2() == ((Triangle) object).getLength2()
&& getLength3() == ((Triangle) object).getLength3());
}
else
return false;
}
@Override
public void calculatePerimeter() {
setPerimeter(length1 + length2 + length3);
}
@Override
public void calculateArea() {
double p = super.getPerimeter() / 2;
double area = Math.sqrt(p * (p - length1) * (p - length2) * (p - length3));
super.setArea(area);
}
}