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

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.
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
Using java, create a class called MyString that has one String called word as its attribute...
Using java, create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n, then perform 2n swaps. Use this in an application called Jumble that prompts the user for a...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and height with setter and getter methods. A constructor that sets the values of base and height. A method named toString() that prints the values of base and height. A method named area() that calculates and prints the area of a triangle. Draw the UML diagram for the class. Implement the class. Q2: Write a Java program that creates a two-dimensional array of type integer...
An equilateral triangle is a triangle whose sides are equal. You are to write a class...
An equilateral triangle is a triangle whose sides are equal. You are to write a class called Triangle, using filenames triangle.h and triangle.cpp, that will allow the creation and handling of equilateral triangles, whose sides are integers in the range 1-39. Details: 1. The single constructor for the Triangle class should have 3 parameters: an integer size (required), which is the length of a side; a border character (optional, with a default of '#'); and a fill character (optional, with...
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...
using java Design a class named Triangle that extends GeometricObject. The class contains: • Three double...
using java Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 to denote three sides of a triangle. • A no-arg constructor that creates a default triangle with default values 1.0. • A constructor that creates a triangle with the specified side1, side2, and side3. In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule...
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'.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT