MIT-Curricular/OOP/Java/Lab/Week8/SquareTriangle/SquareTriangle.java

48 lines
926 B
Java
Raw Normal View History

abstract class Shape {
abstract double calculateArea();
}
class Square extends Shape {
private double side;
public Square(double side) {
this.side = side;
}
double calculateArea() {
return side * side;
}
}
class Triangle extends Shape {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
double calculateArea() {
return 0.5 * base * height;
}
}
public class SquareTriangle {
public static void main(String[] args) {
Square square = new Square(5);
Triangle triangle = new Triangle(4, 6);
System.out.println(
"Area of square: " + square.calculateArea() + " square units"
);
System.out.println(
"Area of triangle: " + triangle.calculateArea() + " square units"
);
}
}