Question

In: Computer Science

Using Java Create the class RightTriangle which is a triangle with one of its angles equal...

Using Java

Create the class RightTriangle which is a triangle with one of its angles equal to 90º – a “right angle”.

  • Its constructor will require the lengths only of its two legs. It will then calculate the length of the third side (its “hypotenuse”) using the Pythagorean Theorem:

a2 + b2 = c2.

  • This class will have no instance variables itself, so it will need to set the appropriate variables in its parent class, Triangle.
  • It will inherit equals from its parent class
  • You also should enhance the String returned from the toString method of its parent class to identify this as a right triangle
  • It will inherit getNumberOfSides, calculatePerimeter, and calculateArea from its parent.

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);
}
}

Solutions

Expert Solution

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);

}

}


Related Solutions

java create a class for triangle and also driver(area and perimeter) also write its getters and...
java create a class for triangle and also driver(area and perimeter) also write its getters and setters for the right triangle
Prove or disprove the following statement: If a hyperbolic triangle has three equal angles then its...
Prove or disprove the following statement: If a hyperbolic triangle has three equal angles then its sides are also equal to each other.
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
let triangle ABC be a triangle in which all three interior angles are acute and let...
let triangle ABC be a triangle in which all three interior angles are acute and let A'B'C' be the orthic triangle. a.) Prove that the altitudes of triangle ABC are the angle bisectors of triangle A'B'C'. b.) Prove the orthocenter of triangle ABC is the incenter of traingle A'B'C'. c.) Prove that A is the A' -excenter of triangle A'B'C'.
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT