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..0455ebe --- /dev/null +++ b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects_2.java @@ -0,0 +1,145 @@ +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 + + 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(); + + System.out.println("Info for t2: "); + t2.showStyle(); + t2.showDim(); + System.out.println("Area is " + t2.area()); + + } + +} diff --git a/Miscellaneous/Cpp/Arrays_of_strings.java b/Miscellaneous/Cpp/Arrays_of_strings.java new file mode 100644 index 0000000..807b383 --- /dev/null +++ b/Miscellaneous/Cpp/Arrays_of_strings.java @@ -0,0 +1,23 @@ +package Cpp; + +public class Arrays_of_strings { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + String numbers[][] = { + + {"Tom", "555-3322"}, + {"Mary", "555-8976"}, + {"Jon", "555-1037"}, + {"Rachel", "555-1400"}, + {"Sherry", "555-8873"} + }; + + System.out.println("numbers[0]: " + numbers[0]); + + System.out.println("numbers[0][1]: " + numbers[0][1]); + + } + +} diff --git a/Miscellaneous/Cpp/Assigning_Objects.java b/Miscellaneous/Cpp/Assigning_Objects.java new file mode 100644 index 0000000..e6a4eb9 --- /dev/null +++ b/Miscellaneous/Cpp/Assigning_Objects.java @@ -0,0 +1,56 @@ +package Cpp; + +class Test{ + + int a, b; + + void set_ab(int a, int b) { + + this.a = a; + this.b = b; + + } + + void show_ab() { + + System.out.println("a is " + a + "\n"); + System.out.println("b is " + b + "\n"); + } +} + +public class Assigning_Objects { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + Test ob1 = new Test(); + Test ob2 = new Test(); + + + ob1.set_ab(10, 20); + ob2.set_ab(0, 0); + + System.out.println("ob1 before assignment: "); + ob1.show_ab(); + System.out.println("ob2 before assignment: "); + ob2.show_ab(); + System.out.println(); + ob2 = ob1; // assign ob1 to ob2 + + System.out.println("ob1 after assignment: "); + ob1.show_ab(); + System.out.println("ob2 after assignment: "); + ob2.show_ab(); + + System.out.println(); + + ob1.set_ab(-1, -1); // change ob1 + + System.out.println("ob1 after changing ob1: "); + ob1.show_ab(); + System.out.println("ob2 after changing ob1: "); + ob2.show_ab(); + + } + +} diff --git a/Miscellaneous/Cpp/Passing_Objects_to_Functions.java b/Miscellaneous/Cpp/Passing_Objects_to_Functions.java new file mode 100644 index 0000000..3d21e39 --- /dev/null +++ b/Miscellaneous/Cpp/Passing_Objects_to_Functions.java @@ -0,0 +1,54 @@ +package Cpp; + +class My_Class { + + int value; + + public My_Class(int value) { + // TODO Auto-generated constructor stub + + this.value = value; + } + + int get_value() { + return value; + } + + void set_value(int value) { + this.value = value; + } + + static void display(My_Class ob) { + + System.out.println(ob.value); + + } + + static void change(My_Class ob) { + ob.set_value(100); + System.out.println("Value of ob inside: "); + + display(ob); + } +} + +public class Passing_Objects_to_Functions { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + My_Class a = new My_Class(10); + + System.out.println("Value of a before calling change(): "); + + My_Class.display(a); + + My_Class.change(a); + + System.out.println("Value of a after calling change(): "); + + My_Class.display(a); + + } + +} diff --git a/Miscellaneous/Cpp/Reference_Variables_and_Assignment.java b/Miscellaneous/Cpp/Reference_Variables_and_Assignment.java new file mode 100644 index 0000000..90f89af --- /dev/null +++ b/Miscellaneous/Cpp/Reference_Variables_and_Assignment.java @@ -0,0 +1,36 @@ +package Cpp; + +class Car{ + + int passengers; + int fuelcap; + int mpg; + +} + +public class Reference_Variables_and_Assignment { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + Car vehicle1 = new Car(); + Car vehicle2 = vehicle1; + + vehicle1.mpg = 26; + + System.out.println(vehicle1.mpg); + System.out.println(vehicle2.mpg); + + ////////////////////////////////////// + + Car vehicle3 = new Car(); + + vehicle2 = vehicle3; + + System.out.println(vehicle1.mpg); + System.out.println(vehicle2.mpg); + System.out.println(vehicle3.mpg); + + } + +} diff --git a/Miscellaneous/Cpp/Reference_Variables_and_Assignment_2.java b/Miscellaneous/Cpp/Reference_Variables_and_Assignment_2.java new file mode 100644 index 0000000..b1f7db4 --- /dev/null +++ b/Miscellaneous/Cpp/Reference_Variables_and_Assignment_2.java @@ -0,0 +1,29 @@ +package Cpp; + +class Car2{ + + int passengers; + int fuelcap; + int mpg; + +} + +public class Reference_Variables_and_Assignment_2 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Car2 vehicle1 = new Car2(); + Car2 vehicle2; + + vehicle2 = vehicle1; + + vehicle1.mpg = 26; + + System.out.println(vehicle1.mpg); + System.out.println(vehicle2.mpg); + + System.out.println(); + + } + +}