NTU-OOP-Tuts-2024/T5Q3/src/TestPolygon.java

58 lines
1.2 KiB
Java
Raw Normal View History

2024-10-16 08:25:34 +08:00
public class TestPolygon {
public static void printArea(Rectangle rect) {
float area = rect.calArea();
System.out.println("The area of the " + rect.getPolytype() + " is " + area);
}
public static void printArea(Triangle tri) {
float area = tri.calArea();
System.out.println("The area of the " + tri.getPolytype() + " is " + area);
}
public static void main(String[] args ) {
Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f);
printArea(rect); // static binding
rect.printWidthHeight();
Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f);
printArea(triangle); // static binding
triangle.printWidthHeight();
}
public static void printArea(Polygon poly) {
float area = poly.calArea( );
System.out.println("The area of the " + poly.getPolytype() + " is " + area);
}
public static void main2(String[] args ) {
Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f);
printArea(rect);
rect.printWidthHeight();
Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f);
printArea(triangle);
triangle.printWidthHeight();
}
}