diff --git a/Chapter7/Abstract_Class.java b/Chapter7/Abstract_Class.java new file mode 100644 index 0000000..11a8783 --- /dev/null +++ b/Chapter7/Abstract_Class.java @@ -0,0 +1,165 @@ + +abstract class Abstract_TwoDShape { + + private double width; + private double height; + private String name; + + // A default constructor + public Abstract_TwoDShape() { + width = height = 0.0; + name = "none"; + } + + // Parameterized constructor. + Abstract_TwoDShape(double w, double h, String n){ + width = w; + height = h; + name = n; + } + + // Construct object with equal width and height. + Abstract_TwoDShape(double x, String n){ + width = height = x; + name = n; + } + + // Construct an object from an object. + Abstract_TwoDShape(Abstract_TwoDShape ob){ + width = ob.width; + height = ob.height; + name = ob.name; + } + + // Accessor methods for width and height. + 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 showDim() { + System.out.println("Width and height are " + width + " and " + height); + } + + // declare an abstract method + abstract double area(); + +} + + +class Triangle_extends_abstract_superclass2 extends Abstract_TwoDShape { + + private String styleString; + + // A default constructor + public Triangle_extends_abstract_superclass2() { + // TODO Auto-generated constructor stub + super(); + + styleString = "none"; + + } + + // Constructor for Triangle + Triangle_extends_abstract_superclass2(String s, double w, double h){ + super(w, h, "triangle"); + + styleString = s; + } + + // One argument constructor. + Triangle_extends_abstract_superclass2(double x){ + super(x, "triangle"); // call superclass constructor + + styleString = "filled"; + } + + // Construct an object from an object. + Triangle_extends_abstract_superclass2(Triangle_extends_abstract_superclass2 ob){ + super(ob); // pass object to TwoDShape constructor + styleString = ob.styleString; + } + + double area() { + return getWidth() * getHeight() / 2; + } + + void showStyle() { + System.out.println("Triangle is " + styleString); + } +} + + +class Rectangle_extends_abstract_superclass2 extends Abstract_TwoDShape { + + // A default constructor + public Rectangle_extends_abstract_superclass2() { + // TODO Auto-generated constructor stub + super(); + } + + // Constructor for Rectangle + Rectangle_extends_abstract_superclass2(double w, double h){ + super(w, h, "rectangle"); // Call superclass constructor + } + + // Construct a square + Rectangle_extends_abstract_superclass2(double x) { + super(x, "rectangle"); // call superclass constructor + } + + // Construct an object from an object. + Rectangle_extends_abstract_superclass2(Rectangle_extends_abstract_superclass2 ob){ + super(ob); // pass object to TwoDShape constructor. + } + + boolean isSquare() { + + if ( getWidth() == getHeight() ) + return true; + + return false; + } + + double area() { + return getWidth() * getHeight(); + } + +} + +public class Abstract_Class { + + public static void main(String[] args) { + + Abstract_TwoDShape shapes[] = new Abstract_TwoDShape[4]; + + shapes[0] = new Triangle_extends_abstract_superclass2("outlined", 8.0, 12.0); + shapes[1] = new Rectangle_extends_abstract_superclass2(10); + shapes[2] = new Rectangle_extends_abstract_superclass2(10, 4); + shapes[3] = new Triangle_extends_abstract_superclass2(7.0); + + for (int i = 0; i < shapes.length; i++) { + System.out.println("Object is " + shapes[i].getName()); + System.out.println("Area is " + shapes[i].area()); + + System.out.println(); + } + + } + +} diff --git a/Chapter7/Dynamic_Disptach_Demo_2.java b/Chapter7/Dynamic_Disptach_Demo_2.java new file mode 100644 index 0000000..9babea9 --- /dev/null +++ b/Chapter7/Dynamic_Disptach_Demo_2.java @@ -0,0 +1,19 @@ + +public class Dynamic_Disptach_Demo_2 { + + public static void main(String[] args) { + + Sup superOb = new Sup(); + + superOb.who(); // In each case, the version of who() to call is determined at run time by ... + + superOb = new Sub1(); + superOb.who(); // ... the type of object being referred to. + + superOb = new Sub2(); + superOb.who(); + + + } + +} diff --git a/Chapter7/SupSubRef2.java b/Chapter7/SupSubRef2.java new file mode 100644 index 0000000..3a9a0a3 --- /dev/null +++ b/Chapter7/SupSubRef2.java @@ -0,0 +1,41 @@ +// A superclass reference can refer to a subclass object. + +class X3 { + int a; + + public X3(int i) { + // TODO Auto-generated constructor stub + + a = i; + } +} + +class Y3 extends X3 { + int b; + + public Y3(int i, int j) { + // TODO Auto-generated constructor stub + super(j); + b = i; + } +} + +public class SupSubRef2 { + + public static void main(String[] args) { + + X3 x3 = new X3(10); + Y3 y3 = new Y3(5, 6); + + System.out.println("X.a: " + x3.a); + + x3 = y3; + System.out.println("X.a: " + x3.a); + + // X references know only about X members + x3.a = 19; // OK +// x3.b = 27; // Error, X does not have a b member. + + } + +} diff --git a/Miscellaneous/C/Uninitialized_array_element_content.java b/Miscellaneous/C/Uninitialized_array_element_content.java new file mode 100644 index 0000000..77b95ac --- /dev/null +++ b/Miscellaneous/C/Uninitialized_array_element_content.java @@ -0,0 +1,24 @@ +package C; + +public class Uninitialized_array_element_content { + + public static void main(String[] args) { + + final int ALL_INPUT_LINES_LONGER_THAN_EIGHTY = 500; + + char more_than_80_characters[] = new char[ALL_INPUT_LINES_LONGER_THAN_EIGHTY]; + + if(more_than_80_characters[0] == 0) + System.out.println("Look! more_than_80_characters[0] is 0"); + else + + System.out.println("See! more_than_80_characters[0] is " + more_than_80_characters[0]); + + + } + +} + +// Adapted from https://github.com/ResilientSpring/The-C-Programming-Language/blob/main/Blend/Uninitialized%20array%20element's%20content%202/bruise%203.c + +// Important Note: https://chatgpt.com/c/68349761-6ad8-8008-8673-728152e81f3a \ No newline at end of file diff --git a/Miscellaneous/Cpp/Array_of_objects_3.java b/Miscellaneous/Cpp/Array_of_objects_3.java new file mode 100644 index 0000000..aa1f547 --- /dev/null +++ b/Miscellaneous/Cpp/Array_of_objects_3.java @@ -0,0 +1,38 @@ +package Cpp; + +class MyClasses{ + private int x; + + void set_x(int i) { + x = i; + } + + int get_x() { + return x; + } +} + +public class Array_of_objects_3 { + + public static void main(String[] args) { + + MyClasses[] objects = new MyClasses[4]; + + objects[0] = new MyClasses(); + objects[0].set_x(11); + + objects[1] = new MyClasses(); + objects[1].set_x(99); + + objects[2].set_x(22); + objects[3].set_x(33); + +// for (int i = 0; i < objects.length; i++) +// objects[i].set_x(i); + + for (int i = 0; i < objects.length; i++) + System.out.println("Objects[" + i + "].get_x(): " + objects[i].get_x()); + + } + +} diff --git a/Miscellaneous/L4_Civil_Servant_Admission_Exam/Intro_to_CS_No17_2026.java b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Intro_to_CS_No17_2026.java new file mode 100644 index 0000000..3ea27b6 --- /dev/null +++ b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Intro_to_CS_No17_2026.java @@ -0,0 +1,29 @@ +package L4_Civil_Servant_Admission_Exam; + +public class Intro_to_CS_No17_2026 { + + public static void main(String[] args) { + + int month = 1; + + while(month <= 4) { + + switch(month) { + + case 1: + System.out.println("31"); + + case 2: + System.out.println("28"); + + case 4: + System.out.println("30"); + + } + + month++; + } + + } + +} diff --git a/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1.java b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1.java new file mode 100644 index 0000000..3f924be --- /dev/null +++ b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1.java @@ -0,0 +1,27 @@ +package L4_Civil_Servant_Admission_Exam; + +public class Programming_No1 { + + long Catalan_Number(int n) { + + long sum; + + if(n == 0) + return 1; + + for (int i = 0; i < n; i++) { + + + + } + + return sum; + + } + + public static void main(String[] args) { + + + } + +} diff --git a/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_2.java b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_2.java new file mode 100644 index 0000000..2fffd89 --- /dev/null +++ b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_2.java @@ -0,0 +1,36 @@ +package L4_Civil_Servant_Admission_Exam; + +import Cpp.static_variable; + +public class Programming_No1_2 { + + long static Catalan_Number(int n) { + + long sum; + + if (n == 0) { + + sum = 1; + + return sum; + + } + + for (int i = 0; i < n; i++) { + + sum = Catalan_Number(i) * Catalan_Number(n - 1 - i); + + } + + return sum; + + } + + public static void main(String[] args) { + + System.out.println(Catalan); + + + } + +} diff --git a/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_3.java b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_3.java new file mode 100644 index 0000000..59c2748 --- /dev/null +++ b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_3.java @@ -0,0 +1,32 @@ +package L4_Civil_Servant_Admission_Exam; + +public class Programming_No1_3 { + + static long Catalan_Number(int n) { + + long sum; + + if (n == 0) { + + sum = 1; + + return sum; + + } + + for (int i = 0; i < n; i++) { + + sum = Catalan_Number(i) * Catalan_Number(n - 1 - i); + + } + + return sum; + + } + + public static void main(String[] args) { + + + } + +} diff --git a/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_4.java b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_4.java new file mode 100644 index 0000000..4a43b43 --- /dev/null +++ b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_4.java @@ -0,0 +1,45 @@ +package L4_Civil_Servant_Admission_Exam; + +public class Programming_No1_4 { + + static long Catalan_Number(int n) { + + // But Catalan_Number will call itself later, which might restart the sum from zero every time + // Catalan_Number is called and cause the intermediate result of the sum to be lost. + // + // Each call to Catalan_Number(n) has its own independent copy of the sum variable on the call stack. + // So even though sum is initialized to 0 every time the function is called, + // this happens in the scope of that one call only.[1] + long sum = 0; + + if (n == 0) { + + sum = 1; + + return sum; + + } + + for (int i = 0; i < n; i++) { + + sum = Catalan_Number(i) * Catalan_Number(n - 1 - i); + + } + + return sum; + + } + + public static void main(String[] args) { + + System.out.println(Catalan_Number(4)); + + } + +} + +/* References: + * + * 1. https://chatgpt.com/c/6869ef92-f4c8-8008-b7b6-c9d0ee97820b + * + * */ diff --git a/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_5.java b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_5.java new file mode 100644 index 0000000..d6a41b3 --- /dev/null +++ b/Miscellaneous/L4_Civil_Servant_Admission_Exam/Programming_No1_5.java @@ -0,0 +1,47 @@ +package L4_Civil_Servant_Admission_Exam; + +public class Programming_No1_5 { + + static long Catalan_Number(int n) { + + // But Catalan_Number will call itself later, which might restart the sum from zero every time + // Catalan_Number is called and cause the intermediate result of the sum to be lost. + // + // Each call to Catalan_Number(n) has its own independent copy of the sum variable on the call stack. + // So even though sum is initialized to 0 every time the function is called, + // this happens in the scope of that one call only.[1] + long sum = 0; + + if (n == 0) { + + sum = 1; + + return sum; + + } + + for (int i = 0; i < n; i++) { + + // Catalan Formula involves Sigma, so you must add all operands.[1] + // Sigma means to "add" all operands, rather than to "multiply" all operands.[1] + sum = sum + Catalan_Number(i) * Catalan_Number(n - 1 - i); + + } + + return sum; + + } + + public static void main(String[] args) { + + System.out.println(Catalan_Number(4)); + + } + +} + +/* References: + * + * 1. https://chatgpt.com/c/6869ef92-f4c8-8008-b7b6-c9d0ee97820b + * + * */ diff --git a/Miscellaneous/SOE/HappyNumber.java b/Miscellaneous/SOE/HappyNumber.java new file mode 100644 index 0000000..4507bc9 --- /dev/null +++ b/Miscellaneous/SOE/HappyNumber.java @@ -0,0 +1,47 @@ +package SOE; + +import java.util.HashSet; +import java.util.Set; + +public class HappyNumber { + + // 計算各位數平方和 + static int next(int n) { + int sum = 0; + while (n > 0) { + int d = n % 10; + sum += d * d; + n /= 10; + } + return sum; + } + + // 遞迴版 isHappy() + static boolean isHappy(int n) { + return isHappyHelper(n, new HashSet<>()); + } + + // 遞迴輔助函式:傳遞 seen 集合 + private static boolean isHappyHelper(int n, Set seen) { + if (n == 1) + return true; // 終止條件:快樂數 + if (seen.contains(n)) + return false; // 終止條件:進入循環 + + seen.add(n); + return isHappyHelper(next(n), seen); + } + + // 測試 + public static void main(String[] args) { + int[] nums = {1, 7, 19, 20, 25, 4}; + + for (int n : nums) { + System.out.printf("%d -> %s%n", n, isHappy(n)); + } + } +} + +// Source: https://chatgpt.com/c/69104fa3-4828-8321-ade1-f983f1f8c14c + +