Using Java
Create the class RightTriangle.
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 extends Figure{
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);
}
}
Thanks for the question,
Here is the implemented class - RightTriangle that inherits from Triangle class.
I have provided detailed comments so that you can follow the code precisely. The right triangle contains also the main test method that demonstrates the below two points -
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.
Please note : The question didnt had the Figure parent class which the Triangle class extends. So incase there is any error please do comment the errror you are getting so that I can change the code accordngly. But should not be the case.
===========================================================================
public class RightTriangle
extends Triangle {
public
RightTriangle(double sideOne,
double sideTwo) {
super(sideOne, sideTwo,
0);
setLength3(getHypotenuse());
}
// calculate length3 which is the
Hypotenuse
private double getHypotenuse()
{
// length3^2 = length1^2
+ length2^2
return
Math.sqrt(getLength1() * getLength1() + getLength2() *
getLength2());
}
// You also should recalculate the hypotenuse
every time one of the two legs changes.
@Override
public void
setLength1(double length1) {
super.setLength1(length1);
// everytime length 1 or
length 2 changes we call getHypotenuse() method
// and set the length of
hypotenuse to that
setLength3(getHypotenuse());
}
@Override
public void
setLength2(double length2) {
super.setLength2(length2);
// everytime length 1 or
length 2 changes we call getHypotenuse() method
// and set the length of
hypotenuse to that
setLength3(getHypotenuse());
}
@Override
public String toString()
{
return
super.toString();
}
public static void main(String[]
args) {
RightTriangle
rightTriangle = new RightTriangle(3,4);
// length3 should be
5
System.out.println(rightTriangle);
// set length1 to
5
rightTriangle.setLength1(5);
// set length2 to
12
rightTriangle.setLength2(12);
// length3 should be
13
System.out.println(rightTriangle);
}
}
=========================================================
thanks a lot !
Get Answers For Free
Most questions answered within 1 hours.