Tutorial 3
This commit is contained in:
parent
a7b6bab263
commit
5a81a04720
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
public class VendingMachineApp {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
VendingMachine vendingMachine = new VendingMachine();
|
||||
vendingMachine.start();
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
public class VendingMachineApp {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
VendingMachine vendingMachine = new VendingMachine();
|
||||
vendingMachine.start();
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="openjdk-22" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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 "Circle of radius " + radius + " at point [" + x + "," + y + "]"; }
|
||||
}
|
|
@ -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 + "]"; }
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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()); }
|
||||
}
|
|
@ -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()); }
|
||||
}
|
|
@ -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); }
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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()); }
|
||||
}
|
|
@ -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() ); }
|
||||
}
|
|
@ -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(); }
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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()); }
|
||||
}
|
|
@ -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() ); }
|
||||
}
|
|
@ -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); }
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue