diff --git a/Chapter7/theoretical/Using_abstract_classes_2.java b/Chapter7/theoretical/Using_abstract_classes_2.java new file mode 100644 index 0000000..50dfcfd --- /dev/null +++ b/Chapter7/theoretical/Using_abstract_classes_2.java @@ -0,0 +1,73 @@ +package theoretical; + +abstract class TwoDShapes{ + + private double width; + private double height; + private String name; + + public TwoDShapes() { + + width = height = 0.0; + name = "none"; + } + + TwoDShapes(double w, double h, String n){ + width = w; + height = h; + name = n; + } + + TwoDShapes(double x, String n){ + width = height = x; + name = n; + } + + // Construct an object from an object. + TwoDShapes(TwoDShapes ob){ + width = ob.width; + height = ob.height; + name = ob.name; + } + + + double getWidth() { + return width; + } + + double getHeight() {return height;} + void setWidth(double w) { width = w;} + void setHeight(double h) {height = h;} + String getName() {return name;} + void showDimension() { + System.out.println("Width and height are " + width + " and " + height); + } + + // Now, area() is abstract. + abstract double area(); +} + + +class Triangle extends TwoDShapes{ + + private String style; + + public Triangle() { + // TODO Auto-generated constructor stub + super(); + style = "none"; + } + +} + + +public class Using_abstract_classes_2 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + + + } + +} diff --git a/Chapter7/theoretical/Using_super_to_call_superclass_constructor.java b/Chapter7/theoretical/Using_super_to_call_superclass_constructor.java new file mode 100644 index 0000000..9e3e923 --- /dev/null +++ b/Chapter7/theoretical/Using_super_to_call_superclass_constructor.java @@ -0,0 +1,83 @@ +package theoretical; + +class TwoDShaper{ + + private double width; + private double height; + + public TwoDShaper(double w, double h) { + // TODO Auto-generated constructor stub + } + + 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); + } + +} + + +class Triangular extends TwoDShaper{ + + private String style; + + public Triangular(String s, double w, double h) { + super(w, h); + + style = s; + } + + double area() { + return getWidth() * getHeight() / 2; + } + + void showStyle() { + System.out.println("Triangle is " + style); + } +} + +public class Using_super_to_call_superclass_constructor { + + public static void main(String[] args) { + + Triangular t1 = new Triangular("filled", 4.0, 4.0); + Triangular t2 = new Triangular("outlined", 8.0, 12.0); + + 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 t2: "); + t2.showStyle(); + t2.showDim(); + System.out.println("Area is " + t2.area()); + + } + +} diff --git a/Chapter7/theoretical/Using_super_to_call_superclass_constructor_2.java b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_2.java new file mode 100644 index 0000000..1efe849 --- /dev/null +++ b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_2.java @@ -0,0 +1,122 @@ +package theoretical; + +class TwoDShaper_2{ + + private double width; + private double height; + private String title; + + public TwoDShaper_2(double w, double h) { + // TODO Auto-generated constructor stub + } + + public TwoDShaper_2() { + // TODO Auto-generated constructor stub + } + + TwoDShaper_2(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; + } + +} + + +class Triangular_2 extends TwoDShaper_2{ + + private String style; + + public Triangular_2() { + // TODO Auto-generated constructor stub + } + + public Triangular_2(String s, double w, double h) { + super(w, h); + + style = s; + } + + Triangular_2(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 showTitle() { + + return showTitle(); + + } + +} + + +public class Using_super_to_call_superclass_constructor_2 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Triangular_2 t1 = new Triangular_2("filled", 4.0, 4.0); + Triangular_2 t2 = new Triangular_2("outlined", 8.0, 12.0); + Triangular_2 t3 = new Triangular_2("Slovenly", "Classy", 8.0, 9.0); + + 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(); + System.out.println("Area is " + t3.area()); + } + +} diff --git a/Chapter7/theoretical/Using_super_to_call_superclass_constructor_3.java b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_3.java new file mode 100644 index 0000000..e974c2a --- /dev/null +++ b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_3.java @@ -0,0 +1,127 @@ +package theoretical; + + +class TwoDShaper_3{ + + private double width; + private double height; + private String title; + + public TwoDShaper_3(double w, double h) { + // TODO Auto-generated constructor stub + } + + public TwoDShaper_3() { + // TODO Auto-generated constructor stub + } + + TwoDShaper_3(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; + } + +} + + + +class Triangular_3 extends TwoDShaper_3{ + + private String style; + + public Triangular_3() { + // TODO Auto-generated constructor stub + } + + public Triangular_3(String s, double w, double h) { + super(w, h); + + style = s; + } + + Triangular_3(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(); + + } + +} + + + +public class Using_super_to_call_superclass_constructor_3 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Triangular_3 t1 = new Triangular_3("filled", 4.0, 4.0); + Triangular_3 t2 = new Triangular_3("outlined", 8.0, 12.0); + Triangular_3 t3 = new Triangular_3("Slovenly", "Classy", 8.0, 9.0); + + 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(); + t3.showTheTitle(); + System.out.println("Area is " + t3.area()); + + } + +} diff --git a/Chapter7/theoretical/Using_super_to_call_superclass_constructor_4.java b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_4.java new file mode 100644 index 0000000..e58cc11 --- /dev/null +++ b/Chapter7/theoretical/Using_super_to_call_superclass_constructor_4.java @@ -0,0 +1,138 @@ +package theoretical; + + +class TwoDShaper_4{ + + private double width; + private double height; + private String title; + + public TwoDShaper_4(double w, double h) { + width = w; + height = h; + } + + public TwoDShaper_4() { + // TODO Auto-generated constructor stub + } + + TwoDShaper_4(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; + } + +} + + +class Triangular_4 extends TwoDShaper_4{ + + private String style; + + public Triangular_4() { + // TODO Auto-generated constructor stub + } + + public Triangular_4(String s, double w, double h) { + super(w, h); + + style = s; + } + + Triangular_4(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(); + + } + +} + + + +public class Using_super_to_call_superclass_constructor_4 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Triangular_4 t0 = new Triangular_4(); + Triangular_4 t1 = new Triangular_4("filled", 4.0, 4.0); + Triangular_4 t2 = new Triangular_4("outlined", 8.0, 12.0); + Triangular_4 t3 = new Triangular_4("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(); + t3.showTheTitle(); + System.out.println("Area is " + t3.area()); + } + +} 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()); + + } + +}