From ed651ba40f23fe393973755c94f4d40f8077195e Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Sat, 30 Sep 2023 21:59:01 +0800 Subject: [PATCH 1/4] Committed on or around 2023/09/30 --- ...ass_References_and_Subclass_Objects_2.java | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java diff --git a/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java new file mode 100644 index 0000000..d564db4 --- /dev/null +++ b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java @@ -0,0 +1,127 @@ +package theoretical; + +class TwoDShape_3{ + + private double width; + private double height; + String title; + + public TwoDShape_3(double w, double h) { + width = w; + height = h; + } + + public TwoDShape_3() { + // TODO Auto-generated constructor stub + } + + TwoDShape_3(double width, double height, String title){ + + this.width = width; + this.height = height; + this.title = title; + + } + + + // Construct object from an object. + TwoDShape_3(TwoDShape_3 ob){ + width = ob.width; + height = ob.height; + } + + + double getWidth() { + return width; + } + + double getHeight() { + return height; + } + + void setWidth(double w) { + width = w; + } + + void setHeight(double h) { + height = h; + } + + void showDim() { + + System.out.println("Width and height are " + width + " and " + height); + } + + String showTitle() { + return title; + } + + void showTitle_2() { + System.out.println("Triangle is also " + title); + } + +} + + +class Triangle_3 extends TwoDShape_3{ + + private String style; + + public Triangle_3() { + // TODO Auto-generated constructor stub + } + + public Triangle_3(String s, double w, double h) { + super(w, h); + + style = s; + } + + Triangle_3(String style, String title, double width, double height){ + + super(width, height, title); + + this.style = style; + + } + + Triangle_3(Triangle_3 ob){ + super(ob); // Pass a Triangle reference to TwoDShape's constructor. + + style = ob.style; + } + + + double area() { + return getWidth() * getHeight() / 2; + } + + void showStyle() { + System.out.println("Triangle is " + style); + } + + String showTheTitle() { + + return showTitle(); + + } + + void showTheTitle_2() { + System.out.println("Triangle is also " + super.title); // super is redundant here. + } + + void showTheTitle_3() { + System.out.println("Triangle is also " + title); + } + +} + + +public class Super_Class_References_and_Subclass_Objects_2 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From c74fff2a834a273b131abd8fee11b18c77b7ec86 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Sat, 30 Sep 2023 22:02:28 +0800 Subject: [PATCH 2/4] Committed on or around 2023/09/30 --- .../Super_Class_References_and_Subclass_Objects_2.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java index d564db4..abff18a 100644 --- a/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java +++ b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java @@ -121,6 +121,8 @@ public class Super_Class_References_and_Subclass_Objects_2 { public static void main(String[] args) { // TODO Auto-generated method stub + + Triangle_3 t1 = new Triangle_3("Outlined", 8.0, 12.0); } From b40b402a487126a889fc3f9b4064cdd5f178c887 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Sat, 30 Sep 2023 22:08:44 +0800 Subject: [PATCH 3/4] Committed on or around 2023/09/30 --- ...Super_Class_References_and_Subclass_Objects_2.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java index abff18a..42b70d9 100644 --- a/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java +++ b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java @@ -123,6 +123,17 @@ public static void main(String[] args) { // TODO Auto-generated method stub Triangle_3 t1 = new Triangle_3("Outlined", 8.0, 12.0); + + // make a copy of t1 + Triangle_3 t2 = new Triangle_3(t1); + + + System.out.println("Info for t1: "); + t1.showStyle(); + t1.showDim(); + System.out.println("Area is " + t1.area()); + + System.out.println(); } From e207da4f062f5676d23109eb2f858101bf654ab5 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Sat, 30 Sep 2023 22:12:44 +0800 Subject: [PATCH 4/4] Committed on or around 2023/09/30 --- .../Super_Class_References_and_Subclass_Objects_2.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java index 42b70d9..0455ebe 100644 --- a/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java +++ b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java @@ -134,6 +134,11 @@ public static void main(String[] args) { System.out.println("Area is " + t1.area()); System.out.println(); + + System.out.println("Info for t2: "); + t2.showStyle(); + t2.showDim(); + System.out.println("Area is " + t2.area()); }