Create a class called RightTriangle with the following members: Fields: sideA ,
ID: 3904333 • Letter: C
Question
Create a class called RightTriangle with the following members:
Fields:
sideA, a double variable to store length of side A.
sideB, a double variable to store length of side B.
R_ANGLE, an integer constant to store value of right angle which is 90.
count, a static integer variable to store the number of created objects.
Methods:
side A
RightTriangle, a constructor that gets two double inputs and assigns them to sideA and sideB. The constructor needs to update the number of objects when an object is created.
setSideA, a set method to assign a value to sideA.
setSideB, a set method to assign a value to sideB.
getSideA, a get method to return the value of sideA.
getSideB, a get method to return the value of sideB.
getCount, a static method to return the number of created objects.
hypotenuse, a method that calculates and returns the length of hypotenuse using following
formula:
h?????????????????? = ???????????2 + ??????????2
Note: Import all static members of Math class in RightTriangle class and use “pow” and “sqrt”methods of Math class to calculate hypotenuse.
toString, a method to return a string containing all information for an object (the number of created object, side A, side B, hypotenuse, and right angle).
Note: You need to call “hypotenuse” method in this method
The RightTriangleTest Class
Then create a driver class called RightTriangleTest. In the main method of this class create 3 objects of RightTriangle class using your defined constructor. After creating each object use “System.out.printf” to display information of that object.
side B
Sample Output
Explanation / Answer
RightTriangleTest.java
public class RightTriangleTest {
public static void main(String[] args) {
RightTriangle obj1 = new RightTriangle (1.1, 2.2);
RightTriangle obj2 = new RightTriangle (3.3, 4.4);
RightTriangle obj3 = new RightTriangle (5.5, 6.6);
System.out.printf("%s ",obj1);
System.out.printf("%s ",obj2);
System.out.printf("%s ",obj3);
}
}
RightTriangle.java
public class RightTriangle {
private double sideA, sideB;
private final int R_ANGLE = 90;
private static int count;
public RightTriangle(double a, double b) {
sideA=a;
sideB=b;
count++;
}
public double getSideA() {
return sideA;
}
public void setSideA(double sideA) {
this.sideA = sideA;
}
public double getSideB() {
return sideB;
}
public void setSideB(double sideB) {
this.sideB = sideB;
}
public int getCount() {
return count;
}
public double hypotenuse() {
return Math.sqrt(sideA+sideB);
}
public String toString() {
return "Side A: "+sideA+" Side B: "+sideB+" Hypotenuse: "+hypotenuse()+" Right Angle: "+R_ANGLE;
}
}
Output:
Side A: 1.1 Side B: 2.2 Hypotenuse: 1.8165902124584952 Right Angle: 90
Side A: 3.3 Side B: 4.4 Hypotenuse: 2.7748873851023217 Right Angle: 90
Side A: 5.5 Side B: 6.6 Hypotenuse: 3.478505426185217 Right Angle: 90
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.