class Triangle {
double base, height, side1, side2, side3;
// Constructor for Equilateral Triangle
public Triangle(double side) {
this.side1 = this.side2 = this.side3 = side;
}
// Constructor for Isosceles Triangle
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
// Constructor for Scalene Triangle
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
}
class EquilateralTriangle extends Triangle {
public EquilateralTriangle(double side) {
super(side);
}
public double getArea() {
return (Math.sqrt(3) / 4) * side1 * side1;
}
}
class IsoscelesTriangle extends Triangle {
public IsoscelesTriangle(double base, double height) {
super(base, height);
}
public double getArea() {
return (base * height) / 2;
}
}
class ScaleneTriangle extends Triangle {
public ScaleneTriangle(double side1, double side2, double side3) {
super(side1, side2, side3);
}
public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s – side1) * (s – side2) * (s – side3));
}
}
public class TriangleAreaCalculator {
public static void main(String[] args) {
// Example usage:
EquilateralTriangle equilateralTriangle = new EquilateralTriangle(10);
System.out.println(“Area of Equilateral Triangle: ” + equilateralTriangle.getArea());
IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle(10, 12);
System.out.println(“Area of Isosceles Triangle: ” + isoscelesTriangle.getArea());
ScaleneTriangle scaleneTriangle = new ScaleneTriangle(10, 12, 14);
System.out.println(“Area of Scalene Triangle: ” + scaleneTriangle.getArea());
}
}
Leave a Reply