Upload Tutorial 5

This commit is contained in:
Marcus Vinicius de Carvalho 2024-10-16 08:25:34 +08:00
parent 9d4f4352a3
commit 1435fb21b9
11 changed files with 165 additions and 0 deletions

5
T5Q1/src/ClassA.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassA {
public void print(int x, String y) {
System.out.println(x + " " + y);
}
}

5
T5Q1/src/ClassB.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassB extends ClassA {
public void print(int x) {
System.out.println(x);
}
}

5
T5Q1/src/ClassC.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassC extends ClassA {
public void print(String x, String y) {
System.out.println(x + " " + y);
}
}

5
T5Q1/src/ClassD.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassD extends ClassC {
public void print(String a, String b) {
System.out.println(a + " " + b);
}
}

5
T5Q1/src/ClassE.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassE extends ClassC {
public void print(String x) {
System.out.println(x);
}
}

5
T5Q1/src/ClassF.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassF extends ClassE {
public void print(int x) {
System.out.println(x);
}
}

5
T5Q1/src/ClassG.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassG extends ClassF {
public void print(String x) {
System.out.println(x);
}
}

42
T5Q3/src/Polygon.java Normal file
View File

@ -0,0 +1,42 @@
public abstract class Polygon {
//public class Polygon {
public enum KindofPolygon {
POLY_PLAIN,
POLY_RECT,
POLY_TRIANG
};
protected String name;
protected float width;
protected float height;
protected KindofPolygon polytype;
public Polygon(String theName, float theWidth, float theHeight) {
name = theName;
width = theWidth;
height = theHeight;
polytype = KindofPolygon.POLY_PLAIN;
}
public KindofPolygon getPolytype() {
return polytype;
}
public void setPolytype(KindofPolygon value) {
polytype = value;
}
public String getName() {
return name;
}
// public float calArea() {
// return 0;
// }
public void printWidthHeight( ) {
System.out.println("Width = " + width + " Height = " + height);
}
public abstract float calArea();
}

10
T5Q3/src/Rectangle.java Normal file
View File

@ -0,0 +1,10 @@
public class Rectangle extends Polygon {
public Rectangle(String theName, float theWidth, float theHeight) {
super(theName, theWidth, theHeight) ;
this.polytype = KindofPolygon.POLY_RECT;
}
public float calArea() {
return width * height;
}
}

68
T5Q3/src/TestPolygon.java Normal file
View File

@ -0,0 +1,68 @@
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();
}
}

10
T5Q3/src/Triangle.java Normal file
View File

@ -0,0 +1,10 @@
public class Triangle extends Polygon {
public Triangle (String theName, float theWidth, float theHeight) {
super(theName, theWidth, theHeight) ;
this.polytype = KindofPolygon.POLY_TRIANG;
}
public float calArea() {
return 0.5f * width * height;
}
}