Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
1.2 kB
1
Indexable
Never
abstract class Figure {
    abstract double area();
}

class Rectangle extends Figure {
    private int length;
    private int width;

    Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }

    @Override
    double area() {
        return length * width;
    }
}

class Triangle extends Figure {
    private int base;
    private int height;

    Triangle(int base, int height) {
        this.base = base;
        this.height = height;
    }

    @Override
    double area() {
        return 0.5 * base * height;
    }
}

class Square extends Figure {
    private int side;

    Square(int side) {
        this.side = side;
    }

    @Override
    double area() {
        return side * side;
    }
}

public class Main {
    public static void main(String[] args) {
        Figure rectangle = new Rectangle(5, 3);
        System.out.println("Area of rectangle: " + rectangle.area());

        Figure triangle = new Triangle(4, 6);
        System.out.println("Area of triangle: " + triangle.area());

        Figure square = new Square(4);
        System.out.println("Area of square: " + square.area());
    }
}
Leave a Comment