From 1435fb21b96f8df092310912a27a6c0e1243dcd7 Mon Sep 17 00:00:00 2001 From: Ivsucram Date: Wed, 16 Oct 2024 08:25:34 +0800 Subject: [PATCH] Upload Tutorial 5 --- T5Q1/src/ClassA.java | 5 +++ T5Q1/src/ClassB.java | 5 +++ T5Q1/src/ClassC.java | 5 +++ T5Q1/src/ClassD.java | 5 +++ T5Q1/src/ClassE.java | 5 +++ T5Q1/src/ClassF.java | 5 +++ T5Q1/src/ClassG.java | 5 +++ T5Q3/src/Polygon.java | 42 ++++++++++++++++++++++++ T5Q3/src/Rectangle.java | 10 ++++++ T5Q3/src/TestPolygon.java | 68 +++++++++++++++++++++++++++++++++++++++ T5Q3/src/Triangle.java | 10 ++++++ 11 files changed, 165 insertions(+) create mode 100644 T5Q1/src/ClassA.java create mode 100644 T5Q1/src/ClassB.java create mode 100644 T5Q1/src/ClassC.java create mode 100644 T5Q1/src/ClassD.java create mode 100644 T5Q1/src/ClassE.java create mode 100644 T5Q1/src/ClassF.java create mode 100644 T5Q1/src/ClassG.java create mode 100644 T5Q3/src/Polygon.java create mode 100644 T5Q3/src/Rectangle.java create mode 100644 T5Q3/src/TestPolygon.java create mode 100644 T5Q3/src/Triangle.java diff --git a/T5Q1/src/ClassA.java b/T5Q1/src/ClassA.java new file mode 100644 index 0000000..afa22fa --- /dev/null +++ b/T5Q1/src/ClassA.java @@ -0,0 +1,5 @@ +public class ClassA { + public void print(int x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q1/src/ClassB.java b/T5Q1/src/ClassB.java new file mode 100644 index 0000000..fcdb0c3 --- /dev/null +++ b/T5Q1/src/ClassB.java @@ -0,0 +1,5 @@ +public class ClassB extends ClassA { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q1/src/ClassC.java b/T5Q1/src/ClassC.java new file mode 100644 index 0000000..a1fdca0 --- /dev/null +++ b/T5Q1/src/ClassC.java @@ -0,0 +1,5 @@ +public class ClassC extends ClassA { + public void print(String x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q1/src/ClassD.java b/T5Q1/src/ClassD.java new file mode 100644 index 0000000..6434f18 --- /dev/null +++ b/T5Q1/src/ClassD.java @@ -0,0 +1,5 @@ +public class ClassD extends ClassC { + public void print(String a, String b) { + System.out.println(a + " " + b); + } +} diff --git a/T5Q1/src/ClassE.java b/T5Q1/src/ClassE.java new file mode 100644 index 0000000..89f1391 --- /dev/null +++ b/T5Q1/src/ClassE.java @@ -0,0 +1,5 @@ +public class ClassE extends ClassC { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q1/src/ClassF.java b/T5Q1/src/ClassF.java new file mode 100644 index 0000000..cd9a8dc --- /dev/null +++ b/T5Q1/src/ClassF.java @@ -0,0 +1,5 @@ +public class ClassF extends ClassE { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q1/src/ClassG.java b/T5Q1/src/ClassG.java new file mode 100644 index 0000000..54221cc --- /dev/null +++ b/T5Q1/src/ClassG.java @@ -0,0 +1,5 @@ +public class ClassG extends ClassF { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q3/src/Polygon.java b/T5Q3/src/Polygon.java new file mode 100644 index 0000000..34bd4d2 --- /dev/null +++ b/T5Q3/src/Polygon.java @@ -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(); + +} \ No newline at end of file diff --git a/T5Q3/src/Rectangle.java b/T5Q3/src/Rectangle.java new file mode 100644 index 0000000..82a36bf --- /dev/null +++ b/T5Q3/src/Rectangle.java @@ -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; + } +} diff --git a/T5Q3/src/TestPolygon.java b/T5Q3/src/TestPolygon.java new file mode 100644 index 0000000..a153d31 --- /dev/null +++ b/T5Q3/src/TestPolygon.java @@ -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(); + } +} \ No newline at end of file diff --git a/T5Q3/src/Triangle.java b/T5Q3/src/Triangle.java new file mode 100644 index 0000000..b5e6b49 --- /dev/null +++ b/T5Q3/src/Triangle.java @@ -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; + } +}