From 9fbc9278e9c29b56b5ece7d250568664c6e4299c Mon Sep 17 00:00:00 2001 From: Ivsucram Date: Thu, 20 Feb 2025 22:26:49 +0800 Subject: [PATCH 1/3] Tutorial 3 --- T3Q1-1/T3Q1-1.iml | 11 +++ T3Q1-1/src/VendingMachine.java | 78 ++++++++++++++++ T3Q1-1/src/VendingMachineApp.java | 14 +++ T3Q1-2/T3Q1-2.iml | 11 +++ T3Q1-2/src/VendingMachine.java | 104 +++++++++++++++++++++ T3Q1-2/src/VendingMachineApp.java | 14 +++ T3Q1-3/T3Q1-3.iml | 11 +++ T3Q1-3/src/VendingMachine.java | 117 +++++++++++++++++++++++ T3Q1-3/src/VendingMachineApp.java | 13 +++ T3Q1-3/src/drinks/Beer.java | 10 ++ T3Q1-3/src/drinks/Coke.java | 10 ++ T3Q1-3/src/drinks/Drink.java | 19 ++++ T3Q1-3/src/drinks/GreenTea.java | 10 ++ T3Q1-4/T3Q1-4.iml | 11 +++ T3Q1-4/src/VendingMachine.java | 124 +++++++++++++++++++++++++ T3Q1-4/src/VendingMachineApp.java | 9 ++ T3Q1-4/src/drinks/Beer.java | 10 ++ T3Q1-4/src/drinks/Coke.java | 10 ++ T3Q1-4/src/drinks/Drink.java | 19 ++++ T3Q1-4/src/drinks/GreenTea.java | 10 ++ T3Q1-extra1/T3Q1-extra1.iml | 11 +++ T3Q1-extra1/src/VendingMachine.java | 100 ++++++++++++++++++++ T3Q1-extra1/src/VendingMachineApp.java | 9 ++ T3Q1-extra1/src/coins/Coin.java | 30 ++++++ T3Q1-extra1/src/coins/Dollar.java | 13 +++ T3Q1-extra1/src/coins/Fifty.java | 13 +++ T3Q1-extra1/src/coins/Ten.java | 13 +++ T3Q1-extra1/src/coins/Twenty.java | 13 +++ T3Q1-extra1/src/drinks/Beer.java | 11 +++ T3Q1-extra1/src/drinks/Coke.java | 11 +++ T3Q1-extra1/src/drinks/Drink.java | 23 +++++ T3Q1-extra1/src/drinks/GreenTea.java | 11 +++ T3Q2-1/T3Q2-1.iml | 11 +++ T3Q2-1/src/Circle.java | 15 +++ T3Q2-1/src/Cylinder.java | 17 ++++ T3Q2-1/src/Point.java | 13 +++ T3Q2-1/src/Test.java | 21 +++++ T3Q2-2/T3Q2-2.iml | 11 +++ T3Q2-2/src/Circle.java | 14 +++ T3Q2-2/src/Cylinder.java | 18 ++++ T3Q2-2/src/Point.java | 13 +++ T3Q2-2/src/Test.java | 21 +++++ T3Q2-3/T3Q2-3.iml | 11 +++ T3Q2-3/src/Circle.java | 14 +++ T3Q2-3/src/Cylinder.java | 17 ++++ T3Q2-3/src/Point.java | 14 +++ T3Q2-3/src/Test.java | 21 +++++ T3Q2-4/T3Q2-4.iml | 11 +++ T3Q2-4/src/Circle.java | 15 +++ T3Q2-4/src/Cylinder.java | 17 ++++ T3Q2-4/src/Point.java | 13 +++ T3Q2-4/src/Test.java | 21 +++++ 52 files changed, 1181 insertions(+) create mode 100644 T3Q1-1/T3Q1-1.iml create mode 100644 T3Q1-1/src/VendingMachine.java create mode 100644 T3Q1-1/src/VendingMachineApp.java create mode 100644 T3Q1-2/T3Q1-2.iml create mode 100644 T3Q1-2/src/VendingMachine.java create mode 100644 T3Q1-2/src/VendingMachineApp.java create mode 100644 T3Q1-3/T3Q1-3.iml create mode 100644 T3Q1-3/src/VendingMachine.java create mode 100644 T3Q1-3/src/VendingMachineApp.java create mode 100644 T3Q1-3/src/drinks/Beer.java create mode 100644 T3Q1-3/src/drinks/Coke.java create mode 100644 T3Q1-3/src/drinks/Drink.java create mode 100644 T3Q1-3/src/drinks/GreenTea.java create mode 100644 T3Q1-4/T3Q1-4.iml create mode 100644 T3Q1-4/src/VendingMachine.java create mode 100644 T3Q1-4/src/VendingMachineApp.java create mode 100644 T3Q1-4/src/drinks/Beer.java create mode 100644 T3Q1-4/src/drinks/Coke.java create mode 100644 T3Q1-4/src/drinks/Drink.java create mode 100644 T3Q1-4/src/drinks/GreenTea.java create mode 100644 T3Q1-extra1/T3Q1-extra1.iml create mode 100644 T3Q1-extra1/src/VendingMachine.java create mode 100644 T3Q1-extra1/src/VendingMachineApp.java create mode 100644 T3Q1-extra1/src/coins/Coin.java create mode 100644 T3Q1-extra1/src/coins/Dollar.java create mode 100644 T3Q1-extra1/src/coins/Fifty.java create mode 100644 T3Q1-extra1/src/coins/Ten.java create mode 100644 T3Q1-extra1/src/coins/Twenty.java create mode 100644 T3Q1-extra1/src/drinks/Beer.java create mode 100644 T3Q1-extra1/src/drinks/Coke.java create mode 100644 T3Q1-extra1/src/drinks/Drink.java create mode 100644 T3Q1-extra1/src/drinks/GreenTea.java create mode 100644 T3Q2-1/T3Q2-1.iml create mode 100644 T3Q2-1/src/Circle.java create mode 100644 T3Q2-1/src/Cylinder.java create mode 100644 T3Q2-1/src/Point.java create mode 100644 T3Q2-1/src/Test.java create mode 100644 T3Q2-2/T3Q2-2.iml create mode 100644 T3Q2-2/src/Circle.java create mode 100644 T3Q2-2/src/Cylinder.java create mode 100644 T3Q2-2/src/Point.java create mode 100644 T3Q2-2/src/Test.java create mode 100644 T3Q2-3/T3Q2-3.iml create mode 100644 T3Q2-3/src/Circle.java create mode 100644 T3Q2-3/src/Cylinder.java create mode 100644 T3Q2-3/src/Point.java create mode 100644 T3Q2-3/src/Test.java create mode 100644 T3Q2-4/T3Q2-4.iml create mode 100644 T3Q2-4/src/Circle.java create mode 100644 T3Q2-4/src/Cylinder.java create mode 100644 T3Q2-4/src/Point.java create mode 100644 T3Q2-4/src/Test.java diff --git a/T3Q1-1/T3Q1-1.iml b/T3Q1-1/T3Q1-1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T3Q1-1/T3Q1-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q1-1/src/VendingMachine.java b/T3Q1-1/src/VendingMachine.java new file mode 100644 index 0000000..d7291db --- /dev/null +++ b/T3Q1-1/src/VendingMachine.java @@ -0,0 +1,78 @@ +import java.util.Scanner; + +public class VendingMachine { + + public VendingMachine() {} + + public double selectDrink() { + Scanner sc = new Scanner(System.in); + + int drinkSelection; + double drinkCost = 0; + + System.out.println("====== Vending Machine ======"); + System.out.println("|1. Buy Beer ($3.00) |"); + System.out.println("|2. Buy Coke ($1.00) |"); + System.out.println("|3. Buy Green Tea ($5.00) |"); + System.out.println("|============================"); + + do { + System.out.println("Please enter selection: "); + drinkSelection = sc.nextInt(); + } while (drinkSelection < 1 || drinkSelection > 3); + if (drinkSelection == 1) drinkCost = 3.00; + else if (drinkSelection == 2) drinkCost = 1.00; + else if (drinkSelection == 3) drinkCost = 5.00; + return drinkCost; + } + + public double insertCoins(double drinkCost) { + double amount = 0.0; + + Scanner sc = new Scanner(System.in); + System.out.println("Please insert coins: "); + System.out.println("========== Coins Input ==========="); + System.out.println("|Enter 'Q' for ten cents input |"); + System.out.println("|Enter 'T' for twenty cents input|"); + System.out.println("|Enter 'F' for fifty cents input |"); + System.out.println("|Enter 'N' for a dollar input |"); + System.out.println("=================================="); + + do { + char coin = sc.next().charAt(0); + switch (coin) + { + case 'Q': case 'q': + amount += 0.10; + break; + case 'T': case 't': + amount += 0.20; + break; + case 'F': case 'f': + amount += 0.50; + break; + case 'N': case 'n': + amount += 1.00; + break; + } + System.out.printf("Coins inserted: %.2f \n", amount); + } while (amount < drinkCost); + return amount; + } + + public void checkChange(double amount, double drinkCost) + { + double change = 0.0; + if (amount > drinkCost) + { + change = amount - drinkCost; + System.out.printf("Change $ %.2f \n", change); + } + } + + public void printReceipt() + { + System.out.println("Please collect your drink"); + System.out.println("Thank you!!"); + } +} \ No newline at end of file diff --git a/T3Q1-1/src/VendingMachineApp.java b/T3Q1-1/src/VendingMachineApp.java new file mode 100644 index 0000000..85d0c3c --- /dev/null +++ b/T3Q1-1/src/VendingMachineApp.java @@ -0,0 +1,14 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + double drinkCost = 0; + double amountInserted = 0.0; + + VendingMachine vendingMachine = new VendingMachine(); + drinkCost = vendingMachine.selectDrink(); + amountInserted = vendingMachine.insertCoins(drinkCost); + vendingMachine.checkChange(amountInserted, drinkCost); + vendingMachine.printReceipt(); + } +} diff --git a/T3Q1-2/T3Q1-2.iml b/T3Q1-2/T3Q1-2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T3Q1-2/T3Q1-2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q1-2/src/VendingMachine.java b/T3Q1-2/src/VendingMachine.java new file mode 100644 index 0000000..1c1e393 --- /dev/null +++ b/T3Q1-2/src/VendingMachine.java @@ -0,0 +1,104 @@ +import java.util.Scanner; + +public class VendingMachine { + + public VendingMachine() {} + + public double selectDrink() { + int drinkSelection; + double drinkCost = 0; + + drinkSelection = presentDrinkMenu(); + + if (drinkSelection == 1) drinkCost = 3.00; + else if (drinkSelection == 2) drinkCost = 1.00; + else if (drinkSelection == 3) drinkCost = 5.00; + return drinkCost; + } + + public double insertCoins(double drinkCost) { + double amount = 0.0; + + Scanner sc = new Scanner(System.in); + printCoinOptions(); + + do { + char coin = sc.next().charAt(0); + amount += processCoinAmount(coin); + System.out.printf("Coins inserted: %.2f \n", amount); + } while (amount < drinkCost); + return amount; + } + + public void checkChange(double amount, double drinkCost) + { + double change = 0.0; + if (amount > drinkCost) + { + change = amount - drinkCost; + System.out.printf("Change $ %.2f \n", change); + } + } + + public void printReceipt() + { + System.out.println("Please collect your drink"); + System.out.println("Thank you!!"); + } + + private int presentDrinkMenu() { + Scanner sc = new Scanner(System.in); + + int drinkSelection; + + System.out.println("====== Vending Machine ======"); + System.out.println("|1. Buy Beer ($3.00) |"); + System.out.println("|2. Buy Coke ($1.00) |"); + System.out.println("|3. Buy Green Tea ($5.00) |"); + System.out.println("|============================"); + + do { + System.out.println("Please enter selection: "); + drinkSelection = sc.nextInt(); + } while (drinkSelection < 1 || drinkSelection > 3); + + return drinkSelection; + } + + private void printCoinOptions() { + System.out.println("Please insert coins: "); + System.out.println("========== Coins Input ==========="); + System.out.println("|Enter 'Q' for ten cents input |"); + System.out.println("|Enter 'T' for twenty cents input|"); + System.out.println("|Enter 'F' for fifty cents input |"); + System.out.println("|Enter 'N' for a dollar input |"); + System.out.println("=================================="); + } + + private double processCoinAmount(char coin) { + /* + Note. The code below is just an enhanced way of writing the following: + + switch (coin) + { + case 'Q': case 'q': return 0.10; + case 'T': case 't': return 0.20; + case 'F': case 'f': return 0.50; + case 'N': case 'n': return 1.00; + } + return 0.0; + + This enhanced way or writing switch-cases was introduced in Java 12 and Java 13 (we are currently at Java LTS 22, with Java 23 and Java 24 under early-access); + The "->" was introduced in Java 8. It denotes a Lambda expression: https://www.w3schools.com/java/java_lambda.asp + + The Switch with a lambda-like syntax expression was introduced in Java 14: https://www.javacodegeeks.com/2020/05/switch-as-an-expression-in-java-with-lambda-like-syntax.html + */ + return switch (coin) { + case 'Q', 'q' -> 0.10; + case 'T', 't' -> 0.20; + case 'F', 'f' -> 0.50; + case 'N', 'n' -> 1.00; + default -> 0.0; + }; + } +} \ No newline at end of file diff --git a/T3Q1-2/src/VendingMachineApp.java b/T3Q1-2/src/VendingMachineApp.java new file mode 100644 index 0000000..85d0c3c --- /dev/null +++ b/T3Q1-2/src/VendingMachineApp.java @@ -0,0 +1,14 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + double drinkCost = 0; + double amountInserted = 0.0; + + VendingMachine vendingMachine = new VendingMachine(); + drinkCost = vendingMachine.selectDrink(); + amountInserted = vendingMachine.insertCoins(drinkCost); + vendingMachine.checkChange(amountInserted, drinkCost); + vendingMachine.printReceipt(); + } +} diff --git a/T3Q1-3/T3Q1-3.iml b/T3Q1-3/T3Q1-3.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T3Q1-3/T3Q1-3.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q1-3/src/VendingMachine.java b/T3Q1-3/src/VendingMachine.java new file mode 100644 index 0000000..7b92f16 --- /dev/null +++ b/T3Q1-3/src/VendingMachine.java @@ -0,0 +1,117 @@ +import java.util.Scanner; +/* +You could also use "import drinks.*" instead of the 4 lines below + */ +import drinks.Beer; +import drinks.Coke; +import drinks.Drink; +import drinks.GreenTea; + +public class VendingMachine { + private static final Drink[] availableDrinks = new Drink[3]; + int drinkSelection = 0; + + public VendingMachine() { + availableDrinks[0] = new Beer(); + availableDrinks[1] = new Coke(); + availableDrinks[2] = new GreenTea(); + } + + public double selectDrink() { + drinkSelection = presentDrinkMenu() - 1; // This " - 1 " is to keep "drinkSelection" within the "availableDrinks" array range + return availableDrinks[drinkSelection].getPrice(); + } + + public double insertCoins() { + double amount = 0.0; + + Scanner sc = new Scanner(System.in); + printCoinOptions(); + + do { + char coin = sc.next().charAt(0); + amount += processCoinAmount(coin); + System.out.printf("Coins inserted: %.2f \n", amount); + } while (amount < availableDrinks[drinkSelection].getPrice()); + return amount; + } + + public void checkChange(double amount) + { + double change = 0.0; + if (amount > availableDrinks[drinkSelection].getPrice()) + { + change = amount - availableDrinks[drinkSelection].getPrice(); + System.out.printf("Change $ %.2f \n", change); + } + } + + public void printReceipt() + { + System.out.println("Please collect your drink"); + System.out.println("Thank you!!"); + } + + private Integer presentDrinkMenu() { + Scanner sc = new Scanner(System.in); + int drinkSelection; + + System.out.print("====== Vending Machine ====== \n"); + /* + Note. The for loop below is just an enhanced way of writing the following: + + for (int i = 0; i < availableDrinks.length; i++) { + System.out.printf("|%d. Buy %s ($%.2f) \n", i + 1, availableDrinks[i].getName(), availableDrinks[i].getPrice()); + } + */ + int i = 1; + for (Drink drink : availableDrinks) { + System.out.printf("|%d. Buy %s ($%.2f) \n", i++, drink.getName(), drink.getPrice()); + } + System.out.print("|============================ \n"); + + do { + System.out.println("Please enter selection: "); + drinkSelection = sc.nextInt(); + } while (drinkSelection < 1 || drinkSelection > (availableDrinks.length + 1)); + + return drinkSelection; + } + + private void printCoinOptions() { + System.out.println("Please insert coins: "); + System.out.println("========== Coins Input ==========="); + System.out.println("|Enter 'Q' for ten cents input |"); + System.out.println("|Enter 'T' for twenty cents input|"); + System.out.println("|Enter 'F' for fifty cents input |"); + System.out.println("|Enter 'N' for a dollar input |"); + System.out.println("=================================="); + } + + private double processCoinAmount(char coin) { + /* + Note. The code below is just an enhanced way of writing the following: + + switch (coin) + { + case 'Q': case 'q': return 0.10; + case 'T': case 't': return 0.20; + case 'F': case 'f': return 0.50; + case 'N': case 'n': return 1.00; + } + return 0.0; + + This enhanced way or writing switch-cases was introduced in Java 12 and Java 13 (we are currently at Java LTS 22, with Java 23 and Java 24 under early-access); + The "->" was introduced in Java 8. It denotes a Lambda expression: https://www.w3schools.com/java/java_lambda.asp + + The Switch with a lambda-like syntax expression was introduced in Java 14: https://www.javacodegeeks.com/2020/05/switch-as-an-expression-in-java-with-lambda-like-syntax.html + */ + return switch (coin) { + case 'Q', 'q' -> 0.10; + case 'T', 't' -> 0.20; + case 'F', 'f' -> 0.50; + case 'N', 'n' -> 1.00; + default -> 0.0; + }; + } +} \ No newline at end of file diff --git a/T3Q1-3/src/VendingMachineApp.java b/T3Q1-3/src/VendingMachineApp.java new file mode 100644 index 0000000..7f2a53f --- /dev/null +++ b/T3Q1-3/src/VendingMachineApp.java @@ -0,0 +1,13 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + double amountInserted = 0.0; + + VendingMachine vendingMachine = new VendingMachine(); + vendingMachine.selectDrink(); + amountInserted = vendingMachine.insertCoins(); + vendingMachine.checkChange(amountInserted); + vendingMachine.printReceipt(); + } +} diff --git a/T3Q1-3/src/drinks/Beer.java b/T3Q1-3/src/drinks/Beer.java new file mode 100644 index 0000000..0f65611 --- /dev/null +++ b/T3Q1-3/src/drinks/Beer.java @@ -0,0 +1,10 @@ +package drinks; + +public class Beer extends Drink { + private static final String NAME = "Beer"; + private static final double PRICE = 3.0; + + public Beer() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-3/src/drinks/Coke.java b/T3Q1-3/src/drinks/Coke.java new file mode 100644 index 0000000..0469a7f --- /dev/null +++ b/T3Q1-3/src/drinks/Coke.java @@ -0,0 +1,10 @@ +package drinks; + +public class Coke extends Drink { + private static final String NAME = "Coke"; + private static final double PRICE = 1.0; + + public Coke() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-3/src/drinks/Drink.java b/T3Q1-3/src/drinks/Drink.java new file mode 100644 index 0000000..c346f83 --- /dev/null +++ b/T3Q1-3/src/drinks/Drink.java @@ -0,0 +1,19 @@ +package drinks; + +public class Drink { + private String name; + private double price; + + public Drink(String name, double price) { + this.name = name; + this.price = price; + } + + public double getPrice() { + return price; + } + + public String getName() { + return name; + } +} diff --git a/T3Q1-3/src/drinks/GreenTea.java b/T3Q1-3/src/drinks/GreenTea.java new file mode 100644 index 0000000..c53fd7c --- /dev/null +++ b/T3Q1-3/src/drinks/GreenTea.java @@ -0,0 +1,10 @@ +package drinks; + +public class GreenTea extends Drink { + private static final String NAME = "Green Tea"; + private static final double PRICE = 5.0; + + public GreenTea() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-4/T3Q1-4.iml b/T3Q1-4/T3Q1-4.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T3Q1-4/T3Q1-4.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q1-4/src/VendingMachine.java b/T3Q1-4/src/VendingMachine.java new file mode 100644 index 0000000..db668e9 --- /dev/null +++ b/T3Q1-4/src/VendingMachine.java @@ -0,0 +1,124 @@ +import drinks.Beer; +import drinks.Coke; +import drinks.Drink; +import drinks.GreenTea; + +import java.util.Scanner; + +public class VendingMachine { + private static final Drink[] availableDrinks = new Drink[3]; + int drinkSelection = 0; + + public VendingMachine() { + availableDrinks[0] = new Beer(); + availableDrinks[1] = new Coke(); + availableDrinks[2] = new GreenTea(); + } + + 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(); + insertCoins(); + checkChange(amountInserted); + printReceipt(); + } while (true); + } + + private void selectDrink() { + drinkSelection = presentDrinkMenu(); + } + + private void insertCoins() { + double amount = 0.0; + + Scanner sc = new Scanner(System.in); + printCoinOptions(); + + do { + char coin = sc.next().charAt(0); + amount += processCoinAmount(coin); + System.out.printf("Coins inserted: %.2f \n", amount); + } while (amount < availableDrinks[drinkSelection].getPrice()); + } + + private void checkChange(double amount) + { + double change = 0.0; + if (amount > availableDrinks[drinkSelection].getPrice()) + { + change = amount - availableDrinks[drinkSelection].getPrice(); + System.out.printf("Change $ %.2f \n", change); + } + } + + private void printReceipt() + { + System.out.println("Please collect your drink"); + System.out.println("Thank you!!"); + } + + private Integer presentDrinkMenu() { + Scanner sc = new Scanner(System.in); + int drinkSelection; + + System.out.print("====== Vending Machine ====== \n"); + /* + Note. The for loop below is just an enhanced way of writing the following: + + for (int i = 0; i < availableDrinks.length; i++) { + System.out.printf("|%d. Buy %s ($%.2f) \n", i + 1, availableDrinks[i].getName(), availableDrinks[i].getPrice()); + } + */ + int i = 1; + for (Drink drink : availableDrinks) { + System.out.printf("|%d. Buy %s ($%.2f) \n", i++, drink.getName(), drink.getPrice()); + } + System.out.print("|============================ \n"); + + do { + System.out.println("Please enter selection: "); + drinkSelection = sc.nextInt() - 1; // This " - 1 " is to keep "drinkSelection" within the "availableDrinks" array range; + } while (drinkSelection < 0 || drinkSelection > availableDrinks.length); + + return drinkSelection ; + } + + private void printCoinOptions() { + System.out.println("Please insert coins: "); + System.out.println("========== Coins Input ==========="); + System.out.println("|Enter 'Q' for ten cents input |"); + System.out.println("|Enter 'T' for twenty cents input|"); + System.out.println("|Enter 'F' for fifty cents input |"); + System.out.println("|Enter 'N' for a dollar input |"); + System.out.println("=================================="); + } + + private double processCoinAmount(char coin) { + /* + Note. The code below is just an enhanced way of writing the following: + + switch (coin) + { + case 'Q': case 'q': return 0.10; + case 'T': case 't': return 0.20; + case 'F': case 'f': return 0.50; + case 'N': case 'n': return 1.00; + } + return 0.0; + + This enhanced way or writing switch-cases was introduced in Java 12 and Java 13 (we are currently at Java LTS 22, with Java 23 and Java 24 under early-access); + The "->" was introduced in Java 8. It denotes a Lambda expression: https://www.w3schools.com/java/java_lambda.asp + + The Switch with a lambda-like syntax expression was introduced in Java 14: https://www.javacodegeeks.com/2020/05/switch-as-an-expression-in-java-with-lambda-like-syntax.html + */ + return switch (coin) { + case 'Q', 'q' -> 0.10; + case 'T', 't' -> 0.20; + case 'F', 'f' -> 0.50; + case 'N', 'n' -> 1.00; + default -> 0.0; + }; + } +} \ No newline at end of file diff --git a/T3Q1-4/src/VendingMachineApp.java b/T3Q1-4/src/VendingMachineApp.java new file mode 100644 index 0000000..0684b49 --- /dev/null +++ b/T3Q1-4/src/VendingMachineApp.java @@ -0,0 +1,9 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + VendingMachine vendingMachine = new VendingMachine(); + vendingMachine.start(); + } + +} diff --git a/T3Q1-4/src/drinks/Beer.java b/T3Q1-4/src/drinks/Beer.java new file mode 100644 index 0000000..0f65611 --- /dev/null +++ b/T3Q1-4/src/drinks/Beer.java @@ -0,0 +1,10 @@ +package drinks; + +public class Beer extends Drink { + private static final String NAME = "Beer"; + private static final double PRICE = 3.0; + + public Beer() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-4/src/drinks/Coke.java b/T3Q1-4/src/drinks/Coke.java new file mode 100644 index 0000000..0469a7f --- /dev/null +++ b/T3Q1-4/src/drinks/Coke.java @@ -0,0 +1,10 @@ +package drinks; + +public class Coke extends Drink { + private static final String NAME = "Coke"; + private static final double PRICE = 1.0; + + public Coke() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-4/src/drinks/Drink.java b/T3Q1-4/src/drinks/Drink.java new file mode 100644 index 0000000..c346f83 --- /dev/null +++ b/T3Q1-4/src/drinks/Drink.java @@ -0,0 +1,19 @@ +package drinks; + +public class Drink { + private String name; + private double price; + + public Drink(String name, double price) { + this.name = name; + this.price = price; + } + + public double getPrice() { + return price; + } + + public String getName() { + return name; + } +} diff --git a/T3Q1-4/src/drinks/GreenTea.java b/T3Q1-4/src/drinks/GreenTea.java new file mode 100644 index 0000000..c53fd7c --- /dev/null +++ b/T3Q1-4/src/drinks/GreenTea.java @@ -0,0 +1,10 @@ +package drinks; + +public class GreenTea extends Drink { + private static final String NAME = "Green Tea"; + private static final double PRICE = 5.0; + + public GreenTea() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-extra1/T3Q1-extra1.iml b/T3Q1-extra1/T3Q1-extra1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T3Q1-extra1/T3Q1-extra1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q1-extra1/src/VendingMachine.java b/T3Q1-extra1/src/VendingMachine.java new file mode 100644 index 0000000..63fe9ac --- /dev/null +++ b/T3Q1-extra1/src/VendingMachine.java @@ -0,0 +1,100 @@ +import drinks.*; +import coins.*; + +import java.util.Scanner; + +public class VendingMachine { + private static final Drink[] availableDrinks = new Drink[3]; + private static final Coin[] acceptableCoins = new Coin[4]; + private static final Scanner sc = new Scanner(System.in); + + int drinkSelection = 0; + private boolean isRunning = true; + + public VendingMachine() + { + availableDrinks[0] = new Beer(); + availableDrinks[1] = new Coke(); + availableDrinks[2] = new GreenTea(); + + acceptableCoins[0] = new Ten(); + acceptableCoins[1] = new Twenty(); + acceptableCoins[2] = new Fifty(); + acceptableCoins[3] = new Dollar(); + } + + public void start() + { + while (isRunning) + { + collectOrder(); + checkChange(collectCoins()); + printReceipt(); + } + } + + public void stop() + { + isRunning = false; + } + + private void collectOrder() + { + System.out.print("====== Vending Machine ====== \n"); + int i = 1; + for (Drink drink : availableDrinks) + System.out.printf("|%d. Buy %s ($%.2f) \n", i++, drink.getName(), drink.getPrice()); + System.out.print("|============================ \n"); + + do { + System.out.println("Please enter selection: "); + drinkSelection = sc.nextInt() - 1; + } while (drinkSelection < 0 || drinkSelection > availableDrinks.length); + } + + private double collectCoins() + { + double amount = 0.0; + presentCoinMenu(); + + do { + amount += processCoinAmount(); + System.out.printf("Coins inserted: %.2f \n", amount); + } while (amount < availableDrinks[drinkSelection].getPrice()); + + return amount; + } + + private void checkChange(double amount) + { + if (amount > availableDrinks[drinkSelection].getPrice()) + System.out.printf("Change $ %.2f \n", amount - availableDrinks[drinkSelection].getPrice()); + } + + private void printReceipt() + { + System.out.println("Please collect your drink"); + System.out.println("Thank you!!"); + } + + private void presentCoinMenu() + { + System.out.println("Please insert coins: "); + System.out.println("========== Coins Input ==========="); + for (Coin coin : acceptableCoins) + System.out.printf("|Enter '%c' for %s input \n", coin.getSymbol(), coin.getDescription()); + System.out.println("=================================="); + } + + private double processCoinAmount() + { + char insertedCoin = sc.next().charAt(0); + insertedCoin = Character.toUpperCase(insertedCoin); + + for (Coin coin : acceptableCoins) + if (insertedCoin == coin.getSymbol()) + return coin.getValue(); + + return 0.0; + } +} \ No newline at end of file diff --git a/T3Q1-extra1/src/VendingMachineApp.java b/T3Q1-extra1/src/VendingMachineApp.java new file mode 100644 index 0000000..0684b49 --- /dev/null +++ b/T3Q1-extra1/src/VendingMachineApp.java @@ -0,0 +1,9 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + VendingMachine vendingMachine = new VendingMachine(); + vendingMachine.start(); + } + +} diff --git a/T3Q1-extra1/src/coins/Coin.java b/T3Q1-extra1/src/coins/Coin.java new file mode 100644 index 0000000..6efa000 --- /dev/null +++ b/T3Q1-extra1/src/coins/Coin.java @@ -0,0 +1,30 @@ +package coins; + +public class Coin +{ + private final double value; + private final char symbol; + private final String description; + + public Coin(double value, char symbol, String description) + { + this.value = value; + this.symbol = symbol; + this.description = description; + } + + public double getValue() + { + return value; + } + + public char getSymbol() + { + return symbol; + } + + public String getDescription() + { + return description; + } +} diff --git a/T3Q1-extra1/src/coins/Dollar.java b/T3Q1-extra1/src/coins/Dollar.java new file mode 100644 index 0000000..461cf75 --- /dev/null +++ b/T3Q1-extra1/src/coins/Dollar.java @@ -0,0 +1,13 @@ +package coins; + +public class Dollar extends Coin +{ + private static final double VALUE = 1.00; + private static final char SYMBOL = 'N'; + private static final String DESCRIPTION = "a dollar"; + + public Dollar() + { + super(VALUE, SYMBOL, DESCRIPTION); + } +} diff --git a/T3Q1-extra1/src/coins/Fifty.java b/T3Q1-extra1/src/coins/Fifty.java new file mode 100644 index 0000000..8fd4bf8 --- /dev/null +++ b/T3Q1-extra1/src/coins/Fifty.java @@ -0,0 +1,13 @@ +package coins; + +public class Fifty extends Coin +{ + private static final double VALUE = 0.50; + private static final char SYMBOL = 'F'; + private static final String DESCRIPTION = "fifty cents"; + + public Fifty() + { + super(VALUE, SYMBOL, DESCRIPTION); + } +} diff --git a/T3Q1-extra1/src/coins/Ten.java b/T3Q1-extra1/src/coins/Ten.java new file mode 100644 index 0000000..a17d774 --- /dev/null +++ b/T3Q1-extra1/src/coins/Ten.java @@ -0,0 +1,13 @@ +package coins; + +public class Ten extends Coin +{ + private static final double VALUE = 0.10; + private static final char SYMBOL = 'Q'; + private static final String DESCRIPTION = "ten cents"; + + public Ten() + { + super(VALUE, SYMBOL, DESCRIPTION); + } +} diff --git a/T3Q1-extra1/src/coins/Twenty.java b/T3Q1-extra1/src/coins/Twenty.java new file mode 100644 index 0000000..19c2d86 --- /dev/null +++ b/T3Q1-extra1/src/coins/Twenty.java @@ -0,0 +1,13 @@ +package coins; + +public class Twenty extends Coin +{ + private static final double VALUE = 0.20; + private static final char SYMBOL = 'T'; + private static final String DESCRIPTION = "twenty cents"; + + public Twenty() + { + super(VALUE, SYMBOL, DESCRIPTION); + } +} diff --git a/T3Q1-extra1/src/drinks/Beer.java b/T3Q1-extra1/src/drinks/Beer.java new file mode 100644 index 0000000..c3dd5f1 --- /dev/null +++ b/T3Q1-extra1/src/drinks/Beer.java @@ -0,0 +1,11 @@ +package drinks; + +public class Beer extends Drink +{ + private static final String NAME = "Beer"; + private static final double PRICE = 3.0; + + public Beer() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-extra1/src/drinks/Coke.java b/T3Q1-extra1/src/drinks/Coke.java new file mode 100644 index 0000000..b4bbc19 --- /dev/null +++ b/T3Q1-extra1/src/drinks/Coke.java @@ -0,0 +1,11 @@ +package drinks; + +public class Coke extends Drink +{ + private static final String NAME = "Coke"; + private static final double PRICE = 1.0; + + public Coke() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-extra1/src/drinks/Drink.java b/T3Q1-extra1/src/drinks/Drink.java new file mode 100644 index 0000000..5fe7fcd --- /dev/null +++ b/T3Q1-extra1/src/drinks/Drink.java @@ -0,0 +1,23 @@ +package drinks; + +public class Drink +{ + private final String name; + private final double price; + + public Drink(String name, double price) + { + this.name = name; + this.price = price; + } + + public double getPrice() + { + return price; + } + + public String getName() + { + return name; + } +} diff --git a/T3Q1-extra1/src/drinks/GreenTea.java b/T3Q1-extra1/src/drinks/GreenTea.java new file mode 100644 index 0000000..1a4190b --- /dev/null +++ b/T3Q1-extra1/src/drinks/GreenTea.java @@ -0,0 +1,11 @@ +package drinks; + +public class GreenTea extends Drink +{ + private static final String NAME = "Green Tea"; + private static final double PRICE = 5.0; + + public GreenTea() { + super(NAME, PRICE); + } +} diff --git a/T3Q2-1/T3Q2-1.iml b/T3Q2-1/T3Q2-1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T3Q2-1/T3Q2-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q2-1/src/Circle.java b/T3Q2-1/src/Circle.java new file mode 100644 index 0000000..bc96152 --- /dev/null +++ b/T3Q2-1/src/Circle.java @@ -0,0 +1,15 @@ +public class Circle extends Point { + private double radius; + + public Circle() { radius = 1; } + public Circle(double radius) { this.radius = radius; } + public Circle(int x, int y, double radius) { super(x, y); this.radius = radius; } + public Circle(int x, int y) { super(x, y); radius = 1; } + + public double getRadius() { return radius; } + public void setRadius(double radius) { this.radius = radius; } + + public double area() { return Math.PI * radius * radius; } + + public String toString() { return "Circle of radius " + radius + " at point [" + x + "," + y + "]"; } +} diff --git a/T3Q2-1/src/Cylinder.java b/T3Q2-1/src/Cylinder.java new file mode 100644 index 0000000..0d2a1b2 --- /dev/null +++ b/T3Q2-1/src/Cylinder.java @@ -0,0 +1,17 @@ +public class Cylinder extends Circle { + private double height; + + public Cylinder() { height = 1; } + public Cylinder(double height) { this.height = height;} + public Cylinder(double radius, double height) { super(radius); this.height = height; } + public Cylinder(int x, int y, double radius) { super(x, y, radius); height = 1; } + public Cylinder(int x, int y, double radius, double height) { super(x, y, radius); this.height = height; } + + public double getHeight() { return height; } + public void setHeight(double height) { this.height = height; } + + public double area() { return 2 * (super.area() + Math.PI * super.getRadius() * height); } + public double volume() { return super.area() * height; }; + + public String toString() { return "Cylinder of height " + height + ", radius " + getRadius() + " at point [" + x + "," + y + "]"; } +} diff --git a/T3Q2-1/src/Point.java b/T3Q2-1/src/Point.java new file mode 100644 index 0000000..b0d59ae --- /dev/null +++ b/T3Q2-1/src/Point.java @@ -0,0 +1,13 @@ +public class Point extends Object { + protected int x, y; + + public Point() { x = 0; y = 0; } + public Point(int x, int y) { this.x = x; this.y = y; } + + public void setPoint(int x, int y) { this.x = x; this.y = y; } + + public int getX() { return x; } + public int getY() { return y; } + + public String toString() { return "[" + x + "," + y + "]"; } +} \ No newline at end of file diff --git a/T3Q2-1/src/Test.java b/T3Q2-1/src/Test.java new file mode 100644 index 0000000..531bce8 --- /dev/null +++ b/T3Q2-1/src/Test.java @@ -0,0 +1,21 @@ +public class Test { + + public static void main(String[] args) { + Circle testCircle1 = new Circle(); + Circle testCircle2 = new Circle(2, 4, 35); + Cylinder testCylinder1 = new Cylinder(); + Cylinder testCylinder2 = new Cylinder(3, 3, 8, 20); + + System.out.println("Area of Circle 1 = " + testCircle1.area()); + System.out.println("Area of Circle 2 = " + testCircle2.area()); + System.out.println("Surface Area of Cylinder 1 = " + testCylinder1.area()); + System.out.println("Surface Area of Cylinder 2 = " + testCylinder2.area()); + System.out.println("Volume of Cylinder 1 = " + testCylinder1.volume()); + System.out.println("Volume of Cylinder 2 = " + testCylinder2.volume()); + + System.out.println("Description of Circle 1 = " + testCircle1.toString()); + System.out.println("Description of Circle 2 = " + testCircle2.toString()); + System.out.println("Description of Cylinder 1 = " + testCylinder1.toString()); + System.out.println("Description of Cylinder 2 = " + testCylinder2.toString()); + } +} diff --git a/T3Q2-2/T3Q2-2.iml b/T3Q2-2/T3Q2-2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T3Q2-2/T3Q2-2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q2-2/src/Circle.java b/T3Q2-2/src/Circle.java new file mode 100644 index 0000000..9b5c6d9 --- /dev/null +++ b/T3Q2-2/src/Circle.java @@ -0,0 +1,14 @@ +public class Circle extends Point { + private double radius; + + public Circle() { radius = 1; } + public Circle(double radius) { this.radius = radius; } + public Circle(int x, int y, double radius) { super(x, y); this.radius = radius; } + public Circle(int x, int y) { super(x, y); radius = 1; } + + public double getRadius() { return radius; } + public void setRadius(double radius) { this.radius = radius; } + + public double area() { return Math.PI * radius * radius; } + public String toString() { return String.format("Circle of radius %.2f at point %s", radius, super.toString()); } +} diff --git a/T3Q2-2/src/Cylinder.java b/T3Q2-2/src/Cylinder.java new file mode 100644 index 0000000..dbbc986 --- /dev/null +++ b/T3Q2-2/src/Cylinder.java @@ -0,0 +1,18 @@ +public class Cylinder extends Circle { + private double height; + + public Cylinder() { height = 1; } + public Cylinder(double height) { this.height = height;} + public Cylinder(double radius, double height) { super(radius); this.height = height; } + public Cylinder(int x, int y, double radius) { super(x, y, radius); height = 1; } + public Cylinder(int x, int y, double radius, double height) { super(x, y, radius); this.height = height; } + + public double getHeight() { return height; } + public void setHeight(double height) { this.height = height; } + + public double area() { return 2 * (super.area() + Math.PI * super.getRadius() * height); } + public double volume() { return super.area() * height; }; + + // WRONG - It goes against Java's encapsulation rules. A class should only have access to its parent, without bypassing it. + public String toString() { return String.format("Cylinder of height %.2f, radius %.2f at point %s", height, getRadius(), super.super.toString()); } +} diff --git a/T3Q2-2/src/Point.java b/T3Q2-2/src/Point.java new file mode 100644 index 0000000..780e3e8 --- /dev/null +++ b/T3Q2-2/src/Point.java @@ -0,0 +1,13 @@ +public class Point { + protected int x, y; + + public Point() { x = 0; y = 0; } + public Point(int x, int y) { this.x = x; this.y = y; } + + public void setPoint(int x, int y) { this.x = x; this.y = y; } + + public int getX() { return x; } + public int getY() { return y; } + + public String toString() { return String.format("[%d,%d]", x, y); } +} \ No newline at end of file diff --git a/T3Q2-2/src/Test.java b/T3Q2-2/src/Test.java new file mode 100644 index 0000000..531bce8 --- /dev/null +++ b/T3Q2-2/src/Test.java @@ -0,0 +1,21 @@ +public class Test { + + public static void main(String[] args) { + Circle testCircle1 = new Circle(); + Circle testCircle2 = new Circle(2, 4, 35); + Cylinder testCylinder1 = new Cylinder(); + Cylinder testCylinder2 = new Cylinder(3, 3, 8, 20); + + System.out.println("Area of Circle 1 = " + testCircle1.area()); + System.out.println("Area of Circle 2 = " + testCircle2.area()); + System.out.println("Surface Area of Cylinder 1 = " + testCylinder1.area()); + System.out.println("Surface Area of Cylinder 2 = " + testCylinder2.area()); + System.out.println("Volume of Cylinder 1 = " + testCylinder1.volume()); + System.out.println("Volume of Cylinder 2 = " + testCylinder2.volume()); + + System.out.println("Description of Circle 1 = " + testCircle1.toString()); + System.out.println("Description of Circle 2 = " + testCircle2.toString()); + System.out.println("Description of Cylinder 1 = " + testCylinder1.toString()); + System.out.println("Description of Cylinder 2 = " + testCylinder2.toString()); + } +} diff --git a/T3Q2-3/T3Q2-3.iml b/T3Q2-3/T3Q2-3.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T3Q2-3/T3Q2-3.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q2-3/src/Circle.java b/T3Q2-3/src/Circle.java new file mode 100644 index 0000000..fe57827 --- /dev/null +++ b/T3Q2-3/src/Circle.java @@ -0,0 +1,14 @@ +public class Circle extends Point { + private double radius; + + public Circle() { radius = 1; } + public Circle(double radius) { this.radius = radius; } + public Circle(int x, int y, double radius) { super(x, y); this.radius = radius; } + public Circle(int x, int y) { super(x, y); radius = 1; } + + public double getRadius() { return radius; } + public void setRadius(double radius) { this.radius = radius; } + + public double area() { return Math.PI * radius * radius; } + public String toString() { return String.format("Circle of radius %.2f at point %s", radius, super.toString()); } +} \ No newline at end of file diff --git a/T3Q2-3/src/Cylinder.java b/T3Q2-3/src/Cylinder.java new file mode 100644 index 0000000..3fcf66e --- /dev/null +++ b/T3Q2-3/src/Cylinder.java @@ -0,0 +1,17 @@ +public class Cylinder extends Circle { + private double height; + + public Cylinder() { height = 1; } + public Cylinder(double height) { this.height = height;} + public Cylinder(double radius, double height) { super(radius); this.height = height; } + public Cylinder(int x, int y, double radius) { super(x, y, radius); height = 1; } + public Cylinder(int x, int y, double radius, double height) { super(x, y, radius); this.height = height; } + + public double getHeight() { return height; } + public void setHeight(double height) { this.height = height; } + + public double area() { return 2 * (super.area() + Math.PI * super.getRadius() * height); } + public double volume() { return super.area() * height; }; + + public String toString() { return String.format("Cylinder of height %.2f, radius %.2f at point %s", height, getRadius(), printPoint() ); } +} diff --git a/T3Q2-3/src/Point.java b/T3Q2-3/src/Point.java new file mode 100644 index 0000000..2b6a423 --- /dev/null +++ b/T3Q2-3/src/Point.java @@ -0,0 +1,14 @@ +public class Point { + protected int x, y; + + public Point() { x = 0; y = 0; } + public Point(int x, int y) { this.x = x; this.y = y; } + + public void setPoint(int x, int y) { this.x = x; this.y = y; } + + public int getX() { return x; } + public int getY() { return y; } + + public String toString() { return String.format("[%d,%d]", x, y); } + public String printPoint() { return toString(); } +} \ No newline at end of file diff --git a/T3Q2-3/src/Test.java b/T3Q2-3/src/Test.java new file mode 100644 index 0000000..531bce8 --- /dev/null +++ b/T3Q2-3/src/Test.java @@ -0,0 +1,21 @@ +public class Test { + + public static void main(String[] args) { + Circle testCircle1 = new Circle(); + Circle testCircle2 = new Circle(2, 4, 35); + Cylinder testCylinder1 = new Cylinder(); + Cylinder testCylinder2 = new Cylinder(3, 3, 8, 20); + + System.out.println("Area of Circle 1 = " + testCircle1.area()); + System.out.println("Area of Circle 2 = " + testCircle2.area()); + System.out.println("Surface Area of Cylinder 1 = " + testCylinder1.area()); + System.out.println("Surface Area of Cylinder 2 = " + testCylinder2.area()); + System.out.println("Volume of Cylinder 1 = " + testCylinder1.volume()); + System.out.println("Volume of Cylinder 2 = " + testCylinder2.volume()); + + System.out.println("Description of Circle 1 = " + testCircle1.toString()); + System.out.println("Description of Circle 2 = " + testCircle2.toString()); + System.out.println("Description of Cylinder 1 = " + testCylinder1.toString()); + System.out.println("Description of Cylinder 2 = " + testCylinder2.toString()); + } +} diff --git a/T3Q2-4/T3Q2-4.iml b/T3Q2-4/T3Q2-4.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/T3Q2-4/T3Q2-4.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q2-4/src/Circle.java b/T3Q2-4/src/Circle.java new file mode 100644 index 0000000..d7d6884 --- /dev/null +++ b/T3Q2-4/src/Circle.java @@ -0,0 +1,15 @@ +public class Circle { + protected Point center = new Point(); + protected double radius; + + public Circle() { radius = 1; } + public Circle(double radius) { this.radius = radius; } + public Circle(int x, int y, double radius) { center.setPoint(x, y); this.radius = radius; } + public Circle(int x, int y) { center.setPoint(x, y); radius = 1; } + + public double getRadius() { return radius; } + public void setRadius(double radius) { this.radius = radius; } + + public double area() { return Math.PI * radius * radius; } + public String toString() { return String.format("Circle of radius %.2f at point %s", radius, center.toString()); } +} diff --git a/T3Q2-4/src/Cylinder.java b/T3Q2-4/src/Cylinder.java new file mode 100644 index 0000000..85c66b5 --- /dev/null +++ b/T3Q2-4/src/Cylinder.java @@ -0,0 +1,17 @@ +public class Cylinder extends Circle { + private double height; + + public Cylinder() { height = 1; } + public Cylinder(double height) { this.height = height;} + public Cylinder(double radius, double height) { super(radius); this.height = height; } + public Cylinder(int x, int y, double radius) { super(x, y, radius); height = 1; } + public Cylinder(int x, int y, double radius, double height) { super(x, y, radius); this.height = height; } + + public double getHeight() { return height; } + public void setHeight(double height) { this.height = height; } + + public double area() { return 2 * (super.area() + Math.PI * super.getRadius() * height); } + public double volume() { return super.area() * height; }; + + public String toString() { return String.format("Cylinder of height %.2f, radius %.2f at point %s", height, getRadius(), center.toString() ); } +} diff --git a/T3Q2-4/src/Point.java b/T3Q2-4/src/Point.java new file mode 100644 index 0000000..780e3e8 --- /dev/null +++ b/T3Q2-4/src/Point.java @@ -0,0 +1,13 @@ +public class Point { + protected int x, y; + + public Point() { x = 0; y = 0; } + public Point(int x, int y) { this.x = x; this.y = y; } + + public void setPoint(int x, int y) { this.x = x; this.y = y; } + + public int getX() { return x; } + public int getY() { return y; } + + public String toString() { return String.format("[%d,%d]", x, y); } +} \ No newline at end of file diff --git a/T3Q2-4/src/Test.java b/T3Q2-4/src/Test.java new file mode 100644 index 0000000..f481306 --- /dev/null +++ b/T3Q2-4/src/Test.java @@ -0,0 +1,21 @@ +public class Test { + + public static void main(String[] args) { + Circle testCircle1 = new Circle(); + Circle testCircle2 = new Circle(2, 4, 35); + Cylinder testCylinder1 = new Cylinder(); + Cylinder testCylinder2 = new Cylinder(3, 3, 8, 20); + + System.out.printf("Area of Circle 1 = %.2f\n", testCircle1.area()); + System.out.printf("Area of Circle 2 = %.2f\n", testCircle2.area()); + System.out.printf("Surface Area of Cylinder 1 = %.2f\n", testCylinder1.area()); + System.out.printf("Surface Area of Cylinder 2 = %.2f\n", testCylinder2.area()); + System.out.printf("Volume of Cylinder 1 = %.2f\n", testCylinder1.volume()); + System.out.printf("Volume of Cylinder 2 = %.2f\n", testCylinder2.volume()); + + System.out.println("Description of Circle 1 = " + testCircle1); + System.out.println("Description of Circle 2 = " + testCircle2); + System.out.println("Description of Cylinder 1 = " + testCylinder1); + System.out.println("Description of Cylinder 2 = " + testCylinder2); + } +} From 0d87001c46d7bd28439f01dd964f920d96c0316e Mon Sep 17 00:00:00 2001 From: Ivsucram Date: Thu, 13 Mar 2025 19:27:01 +0800 Subject: [PATCH 2/3] Tutorial 5 --- T5Q1/src/ClassA.java | 5 +++ T5Q1/src/ClassB.java | 5 +++ T5Q1/src/ClassC.java | 5 +++ T5Q1/src/ClassD.java | 5 +++ T5Q1/src/ClassE.java | 5 +++ T5Q1/src/ClassF.java | 5 +++ T5Q1/src/ClassG.java | 5 +++ T5Q1/src/Main.java | 11 +++++ T5Q2-A-Sol1/src/ClassA.java | 5 +++ T5Q2-A-Sol1/src/ClassB.java | 5 +++ T5Q2-A-Sol1/src/ClassC.java | 3 ++ T5Q2-A-Sol1/src/ClassD.java | 5 +++ T5Q2-A-Sol1/src/ClassE.java | 8 ++++ T5Q2-A-Sol1/src/ClassF.java | 5 +++ T5Q2-A-Sol1/src/ClassG.java | 5 +++ T5Q2-A-Sol1/src/Main.java | 5 +++ T5Q2-A-Sol2/src/ClassA.java | 5 +++ T5Q2-A-Sol2/src/ClassB.java | 5 +++ T5Q2-A-Sol2/src/ClassC.java | 3 ++ T5Q2-A-Sol2/src/ClassD.java | 5 +++ T5Q2-A-Sol2/src/ClassE.java | 6 +++ T5Q2-A-Sol2/src/ClassF.java | 8 ++++ T5Q2-A-Sol2/src/ClassG.java | 8 ++++ T5Q2-A-Sol2/src/Main.java | 5 +++ T5Q2-A/src/ClassA.java | 5 +++ T5Q2-A/src/ClassB.java | 5 +++ T5Q2-A/src/ClassC.java | 3 ++ T5Q2-A/src/ClassD.java | 5 +++ T5Q2-A/src/ClassE.java | 5 +++ T5Q2-A/src/ClassF.java | 5 +++ T5Q2-A/src/ClassG.java | 5 +++ T5Q2-A/src/Main.java | 5 +++ T5Q2-B-FromSol1/src/ClassA.java | 5 +++ T5Q2-B-FromSol1/src/ClassB.java | 5 +++ T5Q2-B-FromSol1/src/ClassC.java | 3 ++ T5Q2-B-FromSol1/src/ClassD.java | 5 +++ T5Q2-B-FromSol1/src/ClassE.java | 8 ++++ T5Q2-B-FromSol1/src/ClassF.java | 5 +++ T5Q2-B-FromSol1/src/ClassG.java | 5 +++ T5Q2-B-FromSol1/src/Main.java | 15 +++++++ T5Q2-B-FromSol2/src/ClassA.java | 5 +++ T5Q2-B-FromSol2/src/ClassB.java | 5 +++ T5Q2-B-FromSol2/src/ClassC.java | 3 ++ T5Q2-B-FromSol2/src/ClassD.java | 5 +++ T5Q2-B-FromSol2/src/ClassE.java | 6 +++ T5Q2-B-FromSol2/src/ClassF.java | 8 ++++ T5Q2-B-FromSol2/src/ClassG.java | 8 ++++ T5Q2-B-FromSol2/src/Main.java | 15 +++++++ T5Q2-C/src/ClassA.java | 5 +++ T5Q2-C/src/ClassB.java | 5 +++ T5Q2-C/src/ClassC.java | 5 +++ T5Q2-C/src/ClassD.java | 5 +++ T5Q2-C/src/ClassE.java | 5 +++ T5Q2-C/src/ClassF.java | 5 +++ T5Q2-C/src/ClassG.java | 5 +++ T5Q2-C/src/Main.java | 32 ++++++++++++++ T5Q2-Marcus/src/ClassA.java | 9 ++++ T5Q2-Marcus/src/ClassB.java | 9 ++++ T5Q2-Marcus/src/ClassC.java | 9 ++++ T5Q2-Marcus/src/Main.java | 25 +++++++++++ T5Q3/src/Polygon.java | 73 +++++++++++++++++++++++++++++++ T5Q3/src/Rectangle.java | 10 +++++ T5Q3/src/TestPolygon.java | 77 +++++++++++++++++++++++++++++++++ T5Q3/src/Triangle.java | 10 +++++ 64 files changed, 565 insertions(+) create mode 100644 T5Q1/src/ClassA.java create mode 100644 T5Q1/src/ClassB.java create mode 100644 T5Q1/src/ClassC.java create mode 100644 T5Q1/src/ClassD.java create mode 100644 T5Q1/src/ClassE.java create mode 100644 T5Q1/src/ClassF.java create mode 100644 T5Q1/src/ClassG.java create mode 100644 T5Q1/src/Main.java create mode 100644 T5Q2-A-Sol1/src/ClassA.java create mode 100644 T5Q2-A-Sol1/src/ClassB.java create mode 100644 T5Q2-A-Sol1/src/ClassC.java create mode 100644 T5Q2-A-Sol1/src/ClassD.java create mode 100644 T5Q2-A-Sol1/src/ClassE.java create mode 100644 T5Q2-A-Sol1/src/ClassF.java create mode 100644 T5Q2-A-Sol1/src/ClassG.java create mode 100644 T5Q2-A-Sol1/src/Main.java create mode 100644 T5Q2-A-Sol2/src/ClassA.java create mode 100644 T5Q2-A-Sol2/src/ClassB.java create mode 100644 T5Q2-A-Sol2/src/ClassC.java create mode 100644 T5Q2-A-Sol2/src/ClassD.java create mode 100644 T5Q2-A-Sol2/src/ClassE.java create mode 100644 T5Q2-A-Sol2/src/ClassF.java create mode 100644 T5Q2-A-Sol2/src/ClassG.java create mode 100644 T5Q2-A-Sol2/src/Main.java create mode 100644 T5Q2-A/src/ClassA.java create mode 100644 T5Q2-A/src/ClassB.java create mode 100644 T5Q2-A/src/ClassC.java create mode 100644 T5Q2-A/src/ClassD.java create mode 100644 T5Q2-A/src/ClassE.java create mode 100644 T5Q2-A/src/ClassF.java create mode 100644 T5Q2-A/src/ClassG.java create mode 100644 T5Q2-A/src/Main.java create mode 100644 T5Q2-B-FromSol1/src/ClassA.java create mode 100644 T5Q2-B-FromSol1/src/ClassB.java create mode 100644 T5Q2-B-FromSol1/src/ClassC.java create mode 100644 T5Q2-B-FromSol1/src/ClassD.java create mode 100644 T5Q2-B-FromSol1/src/ClassE.java create mode 100644 T5Q2-B-FromSol1/src/ClassF.java create mode 100644 T5Q2-B-FromSol1/src/ClassG.java create mode 100644 T5Q2-B-FromSol1/src/Main.java create mode 100644 T5Q2-B-FromSol2/src/ClassA.java create mode 100644 T5Q2-B-FromSol2/src/ClassB.java create mode 100644 T5Q2-B-FromSol2/src/ClassC.java create mode 100644 T5Q2-B-FromSol2/src/ClassD.java create mode 100644 T5Q2-B-FromSol2/src/ClassE.java create mode 100644 T5Q2-B-FromSol2/src/ClassF.java create mode 100644 T5Q2-B-FromSol2/src/ClassG.java create mode 100644 T5Q2-B-FromSol2/src/Main.java create mode 100644 T5Q2-C/src/ClassA.java create mode 100644 T5Q2-C/src/ClassB.java create mode 100644 T5Q2-C/src/ClassC.java create mode 100644 T5Q2-C/src/ClassD.java create mode 100644 T5Q2-C/src/ClassE.java create mode 100644 T5Q2-C/src/ClassF.java create mode 100644 T5Q2-C/src/ClassG.java create mode 100644 T5Q2-C/src/Main.java create mode 100644 T5Q2-Marcus/src/ClassA.java create mode 100644 T5Q2-Marcus/src/ClassB.java create mode 100644 T5Q2-Marcus/src/ClassC.java create mode 100644 T5Q2-Marcus/src/Main.java create mode 100644 T5Q3/src/Polygon.java create mode 100644 T5Q3/src/Rectangle.java create mode 100644 T5Q3/src/TestPolygon.java create mode 100644 T5Q3/src/Triangle.java 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/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/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/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/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/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/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/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/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; + } +} From 0e672832bcbcf1ec3db50ba275d94137bf1c7967 Mon Sep 17 00:00:00 2001 From: Ivsucram Date: Thu, 13 Mar 2025 19:30:47 +0800 Subject: [PATCH 3/3] Tutorial 5 --- T5Q1/T5Q1.iml | 11 +++++++++++ T5Q2-A-Sol1/T5Q2-A-Sol1.iml | 11 +++++++++++ T5Q2-A-Sol2/T5Q2-A-Sol2.iml | 11 +++++++++++ T5Q2-A/T5Q2-A.iml | 11 +++++++++++ T5Q2-B-FromSol1/T5Q2-B-FromSol1.iml | 11 +++++++++++ T5Q2-B-FromSol2/T5Q2-B-FromSol2.iml | 11 +++++++++++ T5Q2-C/T5Q2-C.iml | 11 +++++++++++ T5Q2-Marcus/T5Q2-Marcus.iml | 11 +++++++++++ T5Q3/T5Q3.iml | 11 +++++++++++ 9 files changed, 99 insertions(+) create mode 100644 T5Q1/T5Q1.iml create mode 100644 T5Q2-A-Sol1/T5Q2-A-Sol1.iml create mode 100644 T5Q2-A-Sol2/T5Q2-A-Sol2.iml create mode 100644 T5Q2-A/T5Q2-A.iml create mode 100644 T5Q2-B-FromSol1/T5Q2-B-FromSol1.iml create mode 100644 T5Q2-B-FromSol2/T5Q2-B-FromSol2.iml create mode 100644 T5Q2-C/T5Q2-C.iml create mode 100644 T5Q2-Marcus/T5Q2-Marcus.iml create mode 100644 T5Q3/T5Q3.iml 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/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-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/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-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-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-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-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/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