diff --git a/Chapter7/theoretical/Using_super_to_call_superclass_constructor_4.java b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_4.java index 16224c3..e58cc11 100644 --- a/Chapter7/theoretical/Using_super_to_call_superclass_constructor_4.java +++ b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_4.java @@ -8,7 +8,8 @@ class TwoDShaper_4{ private String title; public TwoDShaper_4(double w, double h) { - // TODO Auto-generated constructor stub + width = w; + height = h; } public TwoDShaper_4() { @@ -52,7 +53,7 @@ String showTitle() { } -class Triangular_4 extends TwoDShaper_3{ +class Triangular_4 extends TwoDShaper_4{ private String style; @@ -103,7 +104,11 @@ public static void main(String[] args) { System.out.println("Info for t0: "); t0.showStyle(); + t0.setHeight(9); + t0.setWidth(3); t0.showDim(); + t0.showTitle(); + t0.showTheTitle(); System.out.println("Area is " + t0.area()); System.out.println(); diff --git a/Chapter7/theoretical/Using_super_to_call_superclass_constructor_5.java b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_5.java new file mode 100644 index 0000000..99fdeae --- /dev/null +++ b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_5.java @@ -0,0 +1,144 @@ +package theoretical; + +class TwoDShaper_5{ + + private double width; + private double height; + String title; + + public TwoDShaper_5(double w, double h) { + width = w; + height = h; + } + + public TwoDShaper_5() { + // TODO Auto-generated constructor stub + } + + TwoDShaper_5(double width, double height, String title){ + + this.width = width; + this.height = height; + this.title = title; + + } + + + 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 Triangular_5 extends TwoDShaper_5{ + + private String style; + + public Triangular_5() { + // TODO Auto-generated constructor stub + } + + public Triangular_5(String s, double w, double h) { + super(w, h); + + style = s; + } + + Triangular_5(String style, String title, double width, double height){ + + super(width, height, title); + + this.style = 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); + } + +} + + +public class Using_super_to_call_superclass_constructor_5 { + + public static void main(String[] args) { + Triangular_5 t0 = new Triangular_5(); + Triangular_5 t1 = new Triangular_5("filled", 4.0, 4.0); + Triangular_5 t2 = new Triangular_5("outlined", 8.0, 12.0); + Triangular_5 t3 = new Triangular_5("Slovenly", "Classy", 8.0, 9.0); + + System.out.println("Info for t0: "); + t0.showStyle(); + t0.setHeight(9); + t0.setWidth(3); + t0.showDim(); + t0.showTitle(); + t0.showTheTitle(); + System.out.println("Area is " + t0.area()); + + System.out.println(); + + System.out.println("Info for t1: "); + t1.showStyle(); + t1.showDim(); + 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()); + + System.out.println(); + + System.out.println("Info for t3: "); + t3.showStyle(); + t3.showDim(); + t3.showTitle_2(); + t3.showTheTitle_2(); + System.out.println("Area is " + t3.area()); + + } + +} diff --git a/Chapter7/theoretical/Using_super_to_call_superclass_constructor_6.java b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_6.java new file mode 100644 index 0000000..a0f932b --- /dev/null +++ b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_6.java @@ -0,0 +1,151 @@ +package theoretical; + + +class TwoDShaper_6{ + + private double width; + private double height; + String title; + + public TwoDShaper_6(double w, double h) { + width = w; + height = h; + } + + public TwoDShaper_6() { + // TODO Auto-generated constructor stub + } + + TwoDShaper_6(double width, double height, String title){ + + this.width = width; + this.height = height; + this.title = title; + + } + + + 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 Triangular_6 extends TwoDShaper_6{ + + private String style; + + public Triangular_6() { + // TODO Auto-generated constructor stub + } + + public Triangular_6(String s, double w, double h) { + super(w, h); + + style = s; + } + + Triangular_6(String style, String title, double width, double height){ + + super(width, height, title); + + this.style = 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 Using_super_to_call_superclass_constructor_6 { + + public static void main(String[] args) { + + Triangular_6 t0 = new Triangular_6(); + Triangular_6 t1 = new Triangular_6("filled", 4.0, 4.0); + Triangular_6 t2 = new Triangular_6("outlined", 8.0, 12.0); + Triangular_6 t3 = new Triangular_6("Slovenly", "Classy", 8.0, 9.0); + + System.out.println("Info for t0: "); + t0.showStyle(); + t0.setHeight(9); + t0.setWidth(3); + t0.showDim(); + t0.showTitle(); + t0.showTheTitle(); + System.out.println("Area is " + t0.area()); + + System.out.println(); + + System.out.println("Info for t1: "); + t1.showStyle(); + t1.showDim(); + 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()); + + System.out.println(); + + System.out.println("Info for t3: "); + t3.showStyle(); + t3.showDim(); + t3.showTitle_2(); + t3.showTheTitle_2(); + t3.showTheTitle_3(); + System.out.println("Area is " + t3.area()); + + } + +}