diff --git a/T3Q1-4/src/VendingMachine.java b/T3Q1-4/src/VendingMachine.java index ba34ec0..db668e9 100644 --- a/T3Q1-4/src/VendingMachine.java +++ b/T3Q1-4/src/VendingMachine.java @@ -18,8 +18,10 @@ public class VendingMachine { public void start() { // This is an infinite loop. This is quite common in applications that must run 24/7 do { + double amountInserted = 0.0; selectDrink(); - checkChange(insertCoins()); + insertCoins(); + checkChange(amountInserted); printReceipt(); } while (true); } @@ -28,7 +30,7 @@ public class VendingMachine { drinkSelection = presentDrinkMenu(); } - private double insertCoins() { + private void insertCoins() { double amount = 0.0; Scanner sc = new Scanner(System.in); @@ -39,7 +41,6 @@ public class VendingMachine { amount += processCoinAmount(coin); System.out.printf("Coins inserted: %.2f \n", amount); } while (amount < availableDrinks[drinkSelection].getPrice()); - return amount; } private void checkChange(double amount) diff --git a/T5Q1/T5Q1.iml b/T5Q1/T5Q1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T5Q1/T5Q1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T5Q1/src/ClassA.java b/T5Q1/src/ClassA.java new file mode 100644 index 0000000..afa22fa --- /dev/null +++ b/T5Q1/src/ClassA.java @@ -0,0 +1,5 @@ +public class ClassA { + public void print(int x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q1/src/ClassB.java b/T5Q1/src/ClassB.java new file mode 100644 index 0000000..fcdb0c3 --- /dev/null +++ b/T5Q1/src/ClassB.java @@ -0,0 +1,5 @@ +public class ClassB extends ClassA { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q1/src/ClassC.java b/T5Q1/src/ClassC.java new file mode 100644 index 0000000..a1fdca0 --- /dev/null +++ b/T5Q1/src/ClassC.java @@ -0,0 +1,5 @@ +public class ClassC extends ClassA { + public void print(String x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q1/src/ClassD.java b/T5Q1/src/ClassD.java new file mode 100644 index 0000000..6434f18 --- /dev/null +++ b/T5Q1/src/ClassD.java @@ -0,0 +1,5 @@ +public class ClassD extends ClassC { + public void print(String a, String b) { + System.out.println(a + " " + b); + } +} diff --git a/T5Q1/src/ClassE.java b/T5Q1/src/ClassE.java new file mode 100644 index 0000000..89f1391 --- /dev/null +++ b/T5Q1/src/ClassE.java @@ -0,0 +1,5 @@ +public class ClassE extends ClassC { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q1/src/ClassF.java b/T5Q1/src/ClassF.java new file mode 100644 index 0000000..cd9a8dc --- /dev/null +++ b/T5Q1/src/ClassF.java @@ -0,0 +1,5 @@ +public class ClassF extends ClassE { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q1/src/ClassG.java b/T5Q1/src/ClassG.java new file mode 100644 index 0000000..54221cc --- /dev/null +++ b/T5Q1/src/ClassG.java @@ -0,0 +1,5 @@ +public class ClassG extends ClassF { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q1/src/Main.java b/T5Q1/src/Main.java new file mode 100644 index 0000000..79c079f --- /dev/null +++ b/T5Q1/src/Main.java @@ -0,0 +1,11 @@ +public class Main { + public static void main(String[] args) { + ClassF z = new ClassF(); + + z.print(9); + z.print(2, "Cx2002"); + z.print("Object"); + z.print("OODP", "Java"); +// z.print("OODP", 2002); // Compilation error + } +} \ No newline at end of file diff --git a/T5Q2-A-Sol1/T5Q2-A-Sol1.iml b/T5Q2-A-Sol1/T5Q2-A-Sol1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T5Q2-A-Sol1/T5Q2-A-Sol1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T5Q2-A-Sol1/src/ClassA.java b/T5Q2-A-Sol1/src/ClassA.java new file mode 100644 index 0000000..afa22fa --- /dev/null +++ b/T5Q2-A-Sol1/src/ClassA.java @@ -0,0 +1,5 @@ +public class ClassA { + public void print(int x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-A-Sol1/src/ClassB.java b/T5Q2-A-Sol1/src/ClassB.java new file mode 100644 index 0000000..fcdb0c3 --- /dev/null +++ b/T5Q2-A-Sol1/src/ClassB.java @@ -0,0 +1,5 @@ +public class ClassB extends ClassA { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q2-A-Sol1/src/ClassC.java b/T5Q2-A-Sol1/src/ClassC.java new file mode 100644 index 0000000..e1107e2 --- /dev/null +++ b/T5Q2-A-Sol1/src/ClassC.java @@ -0,0 +1,3 @@ +public abstract class ClassC extends ClassA { + public abstract void print(String x, String y); +} diff --git a/T5Q2-A-Sol1/src/ClassD.java b/T5Q2-A-Sol1/src/ClassD.java new file mode 100644 index 0000000..6434f18 --- /dev/null +++ b/T5Q2-A-Sol1/src/ClassD.java @@ -0,0 +1,5 @@ +public class ClassD extends ClassC { + public void print(String a, String b) { + System.out.println(a + " " + b); + } +} diff --git a/T5Q2-A-Sol1/src/ClassE.java b/T5Q2-A-Sol1/src/ClassE.java new file mode 100644 index 0000000..7a8da05 --- /dev/null +++ b/T5Q2-A-Sol1/src/ClassE.java @@ -0,0 +1,8 @@ +public class ClassE extends ClassC { + public void print(String x) { + System.out.println(x); + } + public void print(String x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-A-Sol1/src/ClassF.java b/T5Q2-A-Sol1/src/ClassF.java new file mode 100644 index 0000000..cd9a8dc --- /dev/null +++ b/T5Q2-A-Sol1/src/ClassF.java @@ -0,0 +1,5 @@ +public class ClassF extends ClassE { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q2-A-Sol1/src/ClassG.java b/T5Q2-A-Sol1/src/ClassG.java new file mode 100644 index 0000000..54221cc --- /dev/null +++ b/T5Q2-A-Sol1/src/ClassG.java @@ -0,0 +1,5 @@ +public class ClassG extends ClassF { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q2-A-Sol1/src/Main.java b/T5Q2-A-Sol1/src/Main.java new file mode 100644 index 0000000..be9bb2d --- /dev/null +++ b/T5Q2-A-Sol1/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + ClassF z = new ClassF(); + } +} \ No newline at end of file diff --git a/T5Q2-A-Sol2/T5Q2-A-Sol2.iml b/T5Q2-A-Sol2/T5Q2-A-Sol2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T5Q2-A-Sol2/T5Q2-A-Sol2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T5Q2-A-Sol2/src/ClassA.java b/T5Q2-A-Sol2/src/ClassA.java new file mode 100644 index 0000000..afa22fa --- /dev/null +++ b/T5Q2-A-Sol2/src/ClassA.java @@ -0,0 +1,5 @@ +public class ClassA { + public void print(int x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-A-Sol2/src/ClassB.java b/T5Q2-A-Sol2/src/ClassB.java new file mode 100644 index 0000000..fcdb0c3 --- /dev/null +++ b/T5Q2-A-Sol2/src/ClassB.java @@ -0,0 +1,5 @@ +public class ClassB extends ClassA { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q2-A-Sol2/src/ClassC.java b/T5Q2-A-Sol2/src/ClassC.java new file mode 100644 index 0000000..e1107e2 --- /dev/null +++ b/T5Q2-A-Sol2/src/ClassC.java @@ -0,0 +1,3 @@ +public abstract class ClassC extends ClassA { + public abstract void print(String x, String y); +} diff --git a/T5Q2-A-Sol2/src/ClassD.java b/T5Q2-A-Sol2/src/ClassD.java new file mode 100644 index 0000000..6434f18 --- /dev/null +++ b/T5Q2-A-Sol2/src/ClassD.java @@ -0,0 +1,5 @@ +public class ClassD extends ClassC { + public void print(String a, String b) { + System.out.println(a + " " + b); + } +} diff --git a/T5Q2-A-Sol2/src/ClassE.java b/T5Q2-A-Sol2/src/ClassE.java new file mode 100644 index 0000000..baba39b --- /dev/null +++ b/T5Q2-A-Sol2/src/ClassE.java @@ -0,0 +1,6 @@ +public abstract class ClassE extends ClassC { + public void print(String x) { + System.out.println(x); + } + public abstract void print(String x, String y); +} diff --git a/T5Q2-A-Sol2/src/ClassF.java b/T5Q2-A-Sol2/src/ClassF.java new file mode 100644 index 0000000..1cd9000 --- /dev/null +++ b/T5Q2-A-Sol2/src/ClassF.java @@ -0,0 +1,8 @@ +public class ClassF extends ClassE { + public void print(int x) { + System.out.println(x); + } + public void print(String x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-A-Sol2/src/ClassG.java b/T5Q2-A-Sol2/src/ClassG.java new file mode 100644 index 0000000..215ba8b --- /dev/null +++ b/T5Q2-A-Sol2/src/ClassG.java @@ -0,0 +1,8 @@ +public class ClassG extends ClassF { + public void print(String x) { + System.out.println(x); + } + public void print(String x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-A-Sol2/src/Main.java b/T5Q2-A-Sol2/src/Main.java new file mode 100644 index 0000000..f5858db --- /dev/null +++ b/T5Q2-A-Sol2/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + + } +} \ No newline at end of file diff --git a/T5Q2-A/T5Q2-A.iml b/T5Q2-A/T5Q2-A.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T5Q2-A/T5Q2-A.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T5Q2-A/src/ClassA.java b/T5Q2-A/src/ClassA.java new file mode 100644 index 0000000..afa22fa --- /dev/null +++ b/T5Q2-A/src/ClassA.java @@ -0,0 +1,5 @@ +public class ClassA { + public void print(int x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-A/src/ClassB.java b/T5Q2-A/src/ClassB.java new file mode 100644 index 0000000..fcdb0c3 --- /dev/null +++ b/T5Q2-A/src/ClassB.java @@ -0,0 +1,5 @@ +public class ClassB extends ClassA { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q2-A/src/ClassC.java b/T5Q2-A/src/ClassC.java new file mode 100644 index 0000000..e1107e2 --- /dev/null +++ b/T5Q2-A/src/ClassC.java @@ -0,0 +1,3 @@ +public abstract class ClassC extends ClassA { + public abstract void print(String x, String y); +} diff --git a/T5Q2-A/src/ClassD.java b/T5Q2-A/src/ClassD.java new file mode 100644 index 0000000..6434f18 --- /dev/null +++ b/T5Q2-A/src/ClassD.java @@ -0,0 +1,5 @@ +public class ClassD extends ClassC { + public void print(String a, String b) { + System.out.println(a + " " + b); + } +} diff --git a/T5Q2-A/src/ClassE.java b/T5Q2-A/src/ClassE.java new file mode 100644 index 0000000..89f1391 --- /dev/null +++ b/T5Q2-A/src/ClassE.java @@ -0,0 +1,5 @@ +public class ClassE extends ClassC { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q2-A/src/ClassF.java b/T5Q2-A/src/ClassF.java new file mode 100644 index 0000000..cd9a8dc --- /dev/null +++ b/T5Q2-A/src/ClassF.java @@ -0,0 +1,5 @@ +public class ClassF extends ClassE { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q2-A/src/ClassG.java b/T5Q2-A/src/ClassG.java new file mode 100644 index 0000000..54221cc --- /dev/null +++ b/T5Q2-A/src/ClassG.java @@ -0,0 +1,5 @@ +public class ClassG extends ClassF { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q2-A/src/Main.java b/T5Q2-A/src/Main.java new file mode 100644 index 0000000..be9bb2d --- /dev/null +++ b/T5Q2-A/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + ClassF z = new ClassF(); + } +} \ No newline at end of file diff --git a/T5Q2-B-FromSol1/T5Q2-B-FromSol1.iml b/T5Q2-B-FromSol1/T5Q2-B-FromSol1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T5Q2-B-FromSol1/T5Q2-B-FromSol1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T5Q2-B-FromSol1/src/ClassA.java b/T5Q2-B-FromSol1/src/ClassA.java new file mode 100644 index 0000000..afa22fa --- /dev/null +++ b/T5Q2-B-FromSol1/src/ClassA.java @@ -0,0 +1,5 @@ +public class ClassA { + public void print(int x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-B-FromSol1/src/ClassB.java b/T5Q2-B-FromSol1/src/ClassB.java new file mode 100644 index 0000000..fcdb0c3 --- /dev/null +++ b/T5Q2-B-FromSol1/src/ClassB.java @@ -0,0 +1,5 @@ +public class ClassB extends ClassA { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q2-B-FromSol1/src/ClassC.java b/T5Q2-B-FromSol1/src/ClassC.java new file mode 100644 index 0000000..e1107e2 --- /dev/null +++ b/T5Q2-B-FromSol1/src/ClassC.java @@ -0,0 +1,3 @@ +public abstract class ClassC extends ClassA { + public abstract void print(String x, String y); +} diff --git a/T5Q2-B-FromSol1/src/ClassD.java b/T5Q2-B-FromSol1/src/ClassD.java new file mode 100644 index 0000000..6434f18 --- /dev/null +++ b/T5Q2-B-FromSol1/src/ClassD.java @@ -0,0 +1,5 @@ +public class ClassD extends ClassC { + public void print(String a, String b) { + System.out.println(a + " " + b); + } +} diff --git a/T5Q2-B-FromSol1/src/ClassE.java b/T5Q2-B-FromSol1/src/ClassE.java new file mode 100644 index 0000000..7a8da05 --- /dev/null +++ b/T5Q2-B-FromSol1/src/ClassE.java @@ -0,0 +1,8 @@ +public class ClassE extends ClassC { + public void print(String x) { + System.out.println(x); + } + public void print(String x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-B-FromSol1/src/ClassF.java b/T5Q2-B-FromSol1/src/ClassF.java new file mode 100644 index 0000000..80e2ce3 --- /dev/null +++ b/T5Q2-B-FromSol1/src/ClassF.java @@ -0,0 +1,5 @@ +public class ClassF extends ClassE { + public void print(int x) { + System.out.println(x); + } +} \ No newline at end of file diff --git a/T5Q2-B-FromSol1/src/ClassG.java b/T5Q2-B-FromSol1/src/ClassG.java new file mode 100644 index 0000000..54221cc --- /dev/null +++ b/T5Q2-B-FromSol1/src/ClassG.java @@ -0,0 +1,5 @@ +public class ClassG extends ClassF { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q2-B-FromSol1/src/Main.java b/T5Q2-B-FromSol1/src/Main.java new file mode 100644 index 0000000..42980ef --- /dev/null +++ b/T5Q2-B-FromSol1/src/Main.java @@ -0,0 +1,15 @@ +public class Main { + public static void main(String[] args) { + // I - Works +// ClassC c = new ClassD(); +// c.print("hello", "there"); + + // II - Does not compile because class C is abstract +// ClassA a = new ClassC(); +// a.print(1, "there"); + + // III - Doesn't work - Class A does not have print(string, string) method +// ClassA a = new ClassF(); +// a.print("hello", "there"); + } +} \ No newline at end of file diff --git a/T5Q2-B-FromSol2/T5Q2-B-FromSol2.iml b/T5Q2-B-FromSol2/T5Q2-B-FromSol2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T5Q2-B-FromSol2/T5Q2-B-FromSol2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T5Q2-B-FromSol2/src/ClassA.java b/T5Q2-B-FromSol2/src/ClassA.java new file mode 100644 index 0000000..afa22fa --- /dev/null +++ b/T5Q2-B-FromSol2/src/ClassA.java @@ -0,0 +1,5 @@ +public class ClassA { + public void print(int x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-B-FromSol2/src/ClassB.java b/T5Q2-B-FromSol2/src/ClassB.java new file mode 100644 index 0000000..fcdb0c3 --- /dev/null +++ b/T5Q2-B-FromSol2/src/ClassB.java @@ -0,0 +1,5 @@ +public class ClassB extends ClassA { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q2-B-FromSol2/src/ClassC.java b/T5Q2-B-FromSol2/src/ClassC.java new file mode 100644 index 0000000..e1107e2 --- /dev/null +++ b/T5Q2-B-FromSol2/src/ClassC.java @@ -0,0 +1,3 @@ +public abstract class ClassC extends ClassA { + public abstract void print(String x, String y); +} diff --git a/T5Q2-B-FromSol2/src/ClassD.java b/T5Q2-B-FromSol2/src/ClassD.java new file mode 100644 index 0000000..6434f18 --- /dev/null +++ b/T5Q2-B-FromSol2/src/ClassD.java @@ -0,0 +1,5 @@ +public class ClassD extends ClassC { + public void print(String a, String b) { + System.out.println(a + " " + b); + } +} diff --git a/T5Q2-B-FromSol2/src/ClassE.java b/T5Q2-B-FromSol2/src/ClassE.java new file mode 100644 index 0000000..baba39b --- /dev/null +++ b/T5Q2-B-FromSol2/src/ClassE.java @@ -0,0 +1,6 @@ +public abstract class ClassE extends ClassC { + public void print(String x) { + System.out.println(x); + } + public abstract void print(String x, String y); +} diff --git a/T5Q2-B-FromSol2/src/ClassF.java b/T5Q2-B-FromSol2/src/ClassF.java new file mode 100644 index 0000000..1cd9000 --- /dev/null +++ b/T5Q2-B-FromSol2/src/ClassF.java @@ -0,0 +1,8 @@ +public class ClassF extends ClassE { + public void print(int x) { + System.out.println(x); + } + public void print(String x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-B-FromSol2/src/ClassG.java b/T5Q2-B-FromSol2/src/ClassG.java new file mode 100644 index 0000000..215ba8b --- /dev/null +++ b/T5Q2-B-FromSol2/src/ClassG.java @@ -0,0 +1,8 @@ +public class ClassG extends ClassF { + public void print(String x) { + System.out.println(x); + } + public void print(String x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-B-FromSol2/src/Main.java b/T5Q2-B-FromSol2/src/Main.java new file mode 100644 index 0000000..5380632 --- /dev/null +++ b/T5Q2-B-FromSol2/src/Main.java @@ -0,0 +1,15 @@ +public class Main { + public static void main(String[] args) { + // I - Works + ClassC c = new ClassD(); + c.print("hello", "there"); + + // II - Does not compile because class C is abstract +// ClassA a = new ClassC(); +// a.print(1, "there") + + // III - Works + ClassF a = new ClassF(); + a.print("hello", "there"); + } +} \ No newline at end of file diff --git a/T5Q2-C/T5Q2-C.iml b/T5Q2-C/T5Q2-C.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T5Q2-C/T5Q2-C.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T5Q2-C/src/ClassA.java b/T5Q2-C/src/ClassA.java new file mode 100644 index 0000000..afa22fa --- /dev/null +++ b/T5Q2-C/src/ClassA.java @@ -0,0 +1,5 @@ +public class ClassA { + public void print(int x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-C/src/ClassB.java b/T5Q2-C/src/ClassB.java new file mode 100644 index 0000000..fcdb0c3 --- /dev/null +++ b/T5Q2-C/src/ClassB.java @@ -0,0 +1,5 @@ +public class ClassB extends ClassA { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q2-C/src/ClassC.java b/T5Q2-C/src/ClassC.java new file mode 100644 index 0000000..a1fdca0 --- /dev/null +++ b/T5Q2-C/src/ClassC.java @@ -0,0 +1,5 @@ +public class ClassC extends ClassA { + public void print(String x, String y) { + System.out.println(x + " " + y); + } +} diff --git a/T5Q2-C/src/ClassD.java b/T5Q2-C/src/ClassD.java new file mode 100644 index 0000000..6434f18 --- /dev/null +++ b/T5Q2-C/src/ClassD.java @@ -0,0 +1,5 @@ +public class ClassD extends ClassC { + public void print(String a, String b) { + System.out.println(a + " " + b); + } +} diff --git a/T5Q2-C/src/ClassE.java b/T5Q2-C/src/ClassE.java new file mode 100644 index 0000000..89f1391 --- /dev/null +++ b/T5Q2-C/src/ClassE.java @@ -0,0 +1,5 @@ +public class ClassE extends ClassC { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q2-C/src/ClassF.java b/T5Q2-C/src/ClassF.java new file mode 100644 index 0000000..cd9a8dc --- /dev/null +++ b/T5Q2-C/src/ClassF.java @@ -0,0 +1,5 @@ +public class ClassF extends ClassE { + public void print(int x) { + System.out.println(x); + } +} diff --git a/T5Q2-C/src/ClassG.java b/T5Q2-C/src/ClassG.java new file mode 100644 index 0000000..54221cc --- /dev/null +++ b/T5Q2-C/src/ClassG.java @@ -0,0 +1,5 @@ +public class ClassG extends ClassF { + public void print(String x) { + System.out.println(x); + } +} diff --git a/T5Q2-C/src/Main.java b/T5Q2-C/src/Main.java new file mode 100644 index 0000000..45741c6 --- /dev/null +++ b/T5Q2-C/src/Main.java @@ -0,0 +1,32 @@ +public class Main { + public static void main(String[] args) { + // I - Doesn't work because downcast needs to be explicitly declared +// ClassC c = new ClassD(); +// ClassE e = c; + + // II - CanĀ“t upcast because they are from different family branches +// ClassB b = new ClassE(); +// b.print("hello"); + + // III +// ClassA a = new ClassF(); // Ok +// a.print(12, "there"); // Ok +// a.print(88); // Compilation error + + // IV - +// ClassA a = new ClassC(); // Ok +// ClassG g = (ClassG)a; // Runtime error +// g.print("hello"); + + + // V - We lost track of the print(string, string) in C become it become a A first +// ClassA a = new ClassC(); // ok +// ClassG g = (ClassG)a; // Runtime error +// g.print("hello", "there"); + + // VI +// ClassA a = new ClassF(); // OK +// ClassC f = (ClassC)a; // Ok +// f.print(88, "there"); // Ok + } +} \ No newline at end of file diff --git a/T5Q2-Marcus/T5Q2-Marcus.iml b/T5Q2-Marcus/T5Q2-Marcus.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T5Q2-Marcus/T5Q2-Marcus.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T5Q2-Marcus/src/ClassA.java b/T5Q2-Marcus/src/ClassA.java new file mode 100644 index 0000000..754836c --- /dev/null +++ b/T5Q2-Marcus/src/ClassA.java @@ -0,0 +1,9 @@ +public class ClassA { + public void method1() { + System.out.println("ClassA.method1"); + } + + public void method2() { + System.out.println("ClassA.method2"); + } +} diff --git a/T5Q2-Marcus/src/ClassB.java b/T5Q2-Marcus/src/ClassB.java new file mode 100644 index 0000000..5515d18 --- /dev/null +++ b/T5Q2-Marcus/src/ClassB.java @@ -0,0 +1,9 @@ +public class ClassB extends ClassA { + public void method2() { + System.out.println("ClassB.method2"); + } + + public void method3() { + System.out.println("ClassB.method3"); + } +} diff --git a/T5Q2-Marcus/src/ClassC.java b/T5Q2-Marcus/src/ClassC.java new file mode 100644 index 0000000..9f4f167 --- /dev/null +++ b/T5Q2-Marcus/src/ClassC.java @@ -0,0 +1,9 @@ +public class ClassC extends ClassB { + public void method3() { + System.out.println("ClassC.method3"); + } + + public void method4() { + System.out.println("ClassC.method4"); + } +} diff --git a/T5Q2-Marcus/src/Main.java b/T5Q2-Marcus/src/Main.java new file mode 100644 index 0000000..f764a31 --- /dev/null +++ b/T5Q2-Marcus/src/Main.java @@ -0,0 +1,25 @@ +public class Main { + public static void main(String[] args) { + // I +// ClassA a = new ClassB(); +// ClassB b = (ClassB) a; +// b.method2(); // ClassB.method2 +// b.method1(); // ClassA.method1 + + // II +// ClassA c = new ClassB(); +// ClassC d = (ClassC) c; // Runtime error here +// d.method3(); // Never executed + + // III +// ClassA e = new ClassC(); +// ClassB f = (ClassB) e; +// f.method3(); // ClassC.method3 +// f.method2(); // ClassB.method2 +// f.method1(); // ClassA.method1 + + // IV +// ClassC g = (ClassC) new ClassA(); // Runtime error +// g.method1(); // never executed + } +} \ No newline at end of file diff --git a/T5Q3/T5Q3.iml b/T5Q3/T5Q3.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T5Q3/T5Q3.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T5Q3/src/Polygon.java b/T5Q3/src/Polygon.java new file mode 100644 index 0000000..2201a75 --- /dev/null +++ b/T5Q3/src/Polygon.java @@ -0,0 +1,73 @@ +//public abstract class Polygon { +public class Polygon { + public enum KindofPolygon { + POLY_PLAIN, + POLY_RECT, + POLY_TRIANG + }; + protected String name; + protected float width; + protected float height; + protected KindofPolygon polytype; + + public Polygon(String theName, float theWidth, float theHeight) { + name = theName; + width = theWidth; + height = theHeight; + polytype = KindofPolygon.POLY_PLAIN; + } + + public KindofPolygon getPolytype() { + return polytype; + } + + public void setPolytype(KindofPolygon value) { + polytype = value; + } + + public String getName() { + return name; + } + + public float calArea() { + return 0; + } + + public void printWidthHeight( ) { + System.out.println("Width = " + width + " Height = " + height); + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// public abstract float calArea(); + +} \ No newline at end of file diff --git a/T5Q3/src/Rectangle.java b/T5Q3/src/Rectangle.java new file mode 100644 index 0000000..82a36bf --- /dev/null +++ b/T5Q3/src/Rectangle.java @@ -0,0 +1,10 @@ +public class Rectangle extends Polygon { + public Rectangle(String theName, float theWidth, float theHeight) { + super(theName, theWidth, theHeight) ; + this.polytype = KindofPolygon.POLY_RECT; + } + + public float calArea() { + return width * height; + } +} diff --git a/T5Q3/src/TestPolygon.java b/T5Q3/src/TestPolygon.java new file mode 100644 index 0000000..ab056ff --- /dev/null +++ b/T5Q3/src/TestPolygon.java @@ -0,0 +1,77 @@ +public class TestPolygon { + public static void printArea(Rectangle rect) { + float area = rect.calArea(); + System.out.println("The area of the " + rect.getPolytype() + " is " + area); + } + + public static void printArea(Triangle tri) { + float area = tri.calArea(); + System.out.println("The area of the " + tri.getPolytype() + " is " + area); + } + + public static void main(String[] args ) { + + Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f); + printArea(rect); // static binding + rect.printWidthHeight(); + + Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f); + printArea(triangle); // static binding + triangle.printWidthHeight(); + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + public static void printArea(Polygon poly) { + float area = poly.calArea( ); + System.out.println("The area of the " + poly.getPolytype() + " is " + area); + } + + public static void main2(String[] args ) { + + Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f); + printArea(rect); // dynamic binding + rect.printWidthHeight(); + + Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f); + printArea(triangle); // dynamic binding + triangle.printWidthHeight(); + } +} \ No newline at end of file diff --git a/T5Q3/src/Triangle.java b/T5Q3/src/Triangle.java new file mode 100644 index 0000000..b5e6b49 --- /dev/null +++ b/T5Q3/src/Triangle.java @@ -0,0 +1,10 @@ +public class Triangle extends Polygon { + public Triangle (String theName, float theWidth, float theHeight) { + super(theName, theWidth, theHeight) ; + this.polytype = KindofPolygon.POLY_TRIANG; + } + + public float calArea() { + return 0.5f * width * height; + } +}