Page 1 of 1

In this assignment, you will implement an ​abstract​ class called ​Triangle​, which represents a triangle. (Remember, an

Posted: Tue Jul 05, 2022 10:25 am
by answerhappygod
In this assignment, you will implement an ​abstract​ classcalled ​Triangle​, which represents a
triangle. (Remember, an abstract class is a class that cancontain both abstract methods and concrete methods, and cannot be instantiated. This means youcannot create an instance of an abstract class, but you can, and usually do, extend an abstractclass.)
You will also implement two classes that extend the ​Triangle​class: ​EquilateralTriangle​ and RightTriangle​. These classes represent different kinds oftriangles. In this program, the Triangle​ class is the superclass (or parent class), and the​EquilateralTriangle​ and ​RightTriangle classes are the subclasses (or extended classes, or childclasses).
Please finish the below coding:
1.
package triangle;
/**
* Represents an equilateral triangle.
* Extends abstract class Triangle.
*/
public class EquilateralTriangle extends Triangle {
/**
* Creates an equilateral triangle with 3sides of the given side length.
* (All 3 sides of an equilateral trianglehave the same length,
* so we only need one side length to createthe triangle.)
* Calls constructor in parent Triangle classby calling: super(sideA, sideB, sideC)
*
* Example(s):
* - Calling EquilateralTriangle et = newEquilateralTriangle(1) will create an equilateral
* triangle with sides 1, 1, and 1
* - Calling EquilateralTriangle et = newEquilateralTriangle(5) will create an equilateral
* triangle with sides 5, 5, and 5
*
* @param sideLength for all 3 sides
*/
public EquilateralTriangle(double sideLength){
// TODO Implement constructor
}
/**
* Calculates and returns the area of theequilateral triangle.
*
* Example(s):
* - For an equilateral triangle with sidelengths 1, 1, and 1
* - Calling getArea() will return 0.4330...
*
* - For an equilateral triangle with sidelengths 5, 5, and 5
* - Calling getArea() will return 10.8253...
*
* @return the area of the equilateraltriangle
*/
@Override
public double getArea() {
// TODO Implement method
return 0.0;
}
}
2.
package triangle;
/**
* Represents a Right Triangle.
* Extends abstract class Triangle.
*/
public class RightTriangle extends Triangle {
/**
* Creates a right triangle with given 2sides and calculates the hypotenuse (3rd and longest side).
* Calls constructor in parent Triangle classby calling: super(sideA, sideB, sideC)
*
* Example(s):
* - Calling RightTriangle rt = newRightTriangle(1, 1) will create a right
* triangle with sides 1, 1, and 1.4142...
* - Calling RightTriangle rt = newRightTriangle(1, 3) will create a right
* triangle with sides 1, 3, and 3.1622...
*
* @param sideA is the first right-angledside
* @param sideB is the second right-angledside
*/
public RightTriangle(double sideA, double sideB){
// TODO Implement constructor
}
/**
* Calculates the hypotenuse (longest side)for the right triangle.
* @param sideA is the first right-angledside
* @param sideB is the second right-angledside
*
* Example(s):
* - Calling getHypotenuse(1, 1) will return1.4142 ...
* - Calling getHypotenuse(1, 3) will return3.1622 ...
* - Calling getHypotenuse(2, 2) will return2.8284 ...
*
* @return hypotenuse
*/
private static double getHypotenuse(double sideA,double sideB) {
// TODO Implement method
return 0.0;
}
/**
* Calculates and returns the area of theright triangle.
*
* Example(s):
* - For a right triangle with side lengths1, 3, and 3.1622 ...
* - Calling getArea() will return 1.5
*
* - For a right triangle with side lengths2, 2, and 2.8284 ...
* - Calling getArea() will return 2.0
*
* @return the area of the right triangle
*/
@Override
public double getArea() {
// TODO Implement method
return 0.0;
}
}
3.
package triangle;
/**
* Abstract class representing a triangle.
*/
public abstract class Triangle {
/**
* 3 sides of the triangle.
*/
protected double sideA, sideB, sideC;
/**
* Creates a triangle with the 3 given sidelengths.
* Checks whether the 3 given sides arevalid. If not, throws an IllegalArgumentException.
* @param sideA first side of triangle
* @param sideB second side of triangle
* @param sideC third side of triangle
*/
public Triangle(double sideA, double sideB, doublesideC) {
if (!Triangle.hasValidSize(sideA,sideB, sideC)) {
throw newIllegalArgumentException("Triangle sides not valid.");
}
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
/**
* Checks the length of the given sides of atriangle. A triangle is valid if the sum of
* any of its two sides is greater than thethird side. This must be true for all three
* combinations of added side lengths.
* @param sideA first side of triangle
* @param sideB second side of triangle
* @param sideC third side of triangle
*
* Example(s):
* - Calling hasValidSize(1, 1, 1) willreturn true
* - Calling hasValidSize(5, 5, 5) willreturn true
* - Calling hasValidSize(5, 8, 3) willreturn false
*
* @return true if the triangle sides arevalid, otherwise false
*/
private static boolean hasValidSize(double sideA,double sideB, double sideC) {
// TODO Implement method
return false;
}
/**
* Calculates the perimeter of thetriangle.
*
* Example(s):
* - For an equilateral triangle with sidelengths 5, 5, and 5
* - Calling getPerimeter() will return15.0
*
* - For a right triangle with side lengths1, 3, and 3.1622 ...
* - Calling getPerimeter() will return7.1622 ...
*
* @return the perimeter of the triangle
*/
public double getPerimeter() {
// TODO Implement method
return 0.0;
}
/**
* Abstract method which calculates the areaof the triangle.
* Should be overridden by subclassesEquilateralTriangle and RightTriangle.
*
* @return the area of the triangle
*/
public abstract double getArea();
/**
* Runs and controls the program, creatingdifferent kinds of triangles
* and printing useful information about themfor debugging.
*/
public static void run() {
System.out.println("--------------------------------------------");
// Results should be:
// 1.0, 1.0, 1.0
// 0.4330 ...
// 3.0
EquilateralTriangle et = newEquilateralTriangle(1);
System.out.println("EquilateralTriangle's sides are: " + et.sideA + ", "+ et.sideB + ", " +et.sideC);
System.out.println("EquilateralTriangle's area is: " + et.getArea());
System.out.println("EquilateralTriangle's perimeter is: " + et.getPerimeter());
System.out.println("--------------------------------------------");
// Results should be:
// 2.0, 2.0, 2.8284 ...
// 2.0
// 6.8284 ...
RightTriangle rt = newRightTriangle(2, 2);
System.out.println("Right Triangle'ssides are: " + rt.sideA + ", "+ rt.sideB + ", " + rt.sideC);
System.out.println("Right Triangle'sarea is: " + rt.getArea());
System.out.println("Right Triangle'sperimeter is: " + rt.getPerimeter());
System.out.println("--------------------------------------------");
}
/**
* Main method to run the program.
* @param args
*/
public static void main(String args[]) {
//call static run method in Triangleclass
Triangle.run();
}
}