Modern Java Types
Enum with Fields and Methods
Your Planet enum needs mass and radius for each planet. Enums can have fields, constructors, and methods - they're full classes. Each constant can carry data and provide behavior.
Enum with fields
Add data to each enum constant.
// Enum with fields and constructor
// Concept: enum fields - storing data in enum constants
enum CoffeeSize {
SMALL(8, 2.50),
MEDIUM(12, 3.00),
LARGE(16, 3.50),
EXTRA_LARGE(20, 4.00);
private final int ounces;
private final double price;
// Enum constructor (always private)
CoffeeSize(int ounces, double price) {
this.ounces = ounces;
this.price = price;
}
// Getter methods
public int getOunces() {
return ounces;
}
public double getPrice() {
return price;
}
}
public class EnumWithFields {
public static void main(String[] args) {
// Access enum constant data
CoffeeSize mySize = CoffeeSize.MEDIUM;
System.out.println("Size: " + mySize);
System.out.println("Ounces: " + mySize.getOunces());
System.out.println("Price: $" + mySize.getPrice());
// Print all sizes with details
System.out.println("\nAll coffee sizes:");
for (CoffeeSize size : CoffeeSize.values()) {
System.out.printf("%s: %doz - $%.2f%n",
size, size.getOunces(), size.getPrice());
}
// Calculate total for multiple coffees
CoffeeSize[] order = {
CoffeeSize.SMALL,
CoffeeSize.MEDIUM,
CoffeeSize.LARGE
};
double total = 0;
System.out.println("\nOrder:");
for (CoffeeSize size : order) {
System.out.printf(" %s: $%.2f%n", size, size.getPrice());
total += size.getPrice();
}
System.out.printf("Total: $%.2f%n", total);
}
}
// Enum with fields and constructor
// Concept: enum fields - storing data in enum constants
enum CoffeeSize {
SMALL(8, 2.50),
MEDIUM(12, 3.00),
LARGE(16, 3.50),
EXTRA_LARGE(20, 4.00);
private final int ounces;
private final double price;
// Enum constructor (always private)
CoffeeSize(int ounces, double price) {
this.ounces = ounces;
this.price = price;
}
// Getter methods
public int getOunces() {
return ounces;
}
public double getPrice() {
return price;
}
}
public class EnumWithFields {
public static void main(String[] args) {
// Access enum constant data
CoffeeSize mySize = CoffeeSize.SMALL;
System.out.println("Size: " + mySize);
System.out.println("Ounces: " + mySize.getOunces());
System.out.println("Price: $" + mySize.getPrice());
// Print all sizes with details
System.out.println("\nAll coffee sizes:");
for (CoffeeSize size : CoffeeSize.values()) {
System.out.printf("%s: %doz - $%.2f%n",
size, size.getOunces(), size.getPrice());
}
// Calculate total for multiple coffees
CoffeeSize[] order = {
CoffeeSize.SMALL,
CoffeeSize.MEDIUM,
CoffeeSize.LARGE
};
double total = 0;
System.out.println("\nOrder:");
for (CoffeeSize size : order) {
System.out.printf(" %s: $%.2f%n", size, size.getPrice());
total += size.getPrice();
}
System.out.printf("Total: $%.2f%n", total);
}
}
// Enum with fields and constructor
// Concept: enum fields - storing data in enum constants
enum CoffeeSize {
SMALL(8, 2.50),
MEDIUM(12, 3.00),
LARGE(16, 3.50),
EXTRA_LARGE(20, 4.00);
private final int ounces;
private final double price;
// Enum constructor (always private)
CoffeeSize(int ounces, double price) {
this.ounces = ounces;
this.price = price;
}
// Getter methods
public int getOunces() {
return ounces;
}
public double getPrice() {
return price;
}
}
public class EnumWithFields {
public static void main(String[] args) {
// Access enum constant data
CoffeeSize mySize = CoffeeSize.LARGE;
System.out.println("Size: " + mySize);
System.out.println("Ounces: " + mySize.getOunces());
System.out.println("Price: $" + mySize.getPrice());
// Print all sizes with details
System.out.println("\nAll coffee sizes:");
for (CoffeeSize size : CoffeeSize.values()) {
System.out.printf("%s: %doz - $%.2f%n",
size, size.getOunces(), size.getPrice());
}
// Calculate total for multiple coffees
CoffeeSize[] order = {
CoffeeSize.SMALL,
CoffeeSize.MEDIUM,
CoffeeSize.LARGE
};
double total = 0;
System.out.println("\nOrder:");
for (CoffeeSize size : order) {
System.out.printf(" %s: $%.2f%n", size, size.getPrice());
total += size.getPrice();
}
System.out.printf("Total: $%.2f%n", total);
}
}
public static void main(String[] args)
29public class EnumWithFields {30 public static void main(String[] args) {31 // Access enum constant data32 CoffeeSize mySize = CoffeeSize.MEDIUM;33 //@mySize=CoffeeSize.MEDIUM, CoffeeSize.SMALL, CoffeeSize.LARGEthis.ounces ← 8, this.price ← 2.5
pass 1 of 413// Enum constructor (always private)14CoffeeSize(int ounces8, double price2.5) {15 this.ounces→ 8 = ounces8;16 this.price→ 2.5 = price2.5;17}All 4 passes — pass 1 is the card above pass ouncespricethis.ouncesthis.price1 8 2.5 8 2.5 2 12 3.0 12 3.0 3 16 3.5 16 3.5 4 20 4.0 20 4.0 mySize ← MEDIUM
31// Access enum constant data32CoffeeSize mySize→ MEDIUM = CoffeeSize.MEDIUM;33//@mySize=CoffeeSize.MEDIUM, CoffeeSize.SMALL, CoffeeSize.LARGE3435System.out.println("Size: " + mySizeMEDIUM);36System.out.println("Ounces: " + mySize.getOunces());37System.out.println("Price: $" + mySize.getPrice());outputSize: MEDIUMpublic int getOunces()
pass 1 of 519// Getter methods20public int getOunces() {21 return ounces12;22}All 5 passes — pass 1 is the card above pass ounces1 12 2 8 3 12 4 16 5 20 System.out.println("Ounces: " + mySize.getOunces());
35System.out.println("Size: " + mySize);36System.out.println("Ounces: " + mySize.getOunces());37System.out.println("Price: $" + mySize.getPrice());outputOunces: 12public double getPrice()
pass 1 of 1124public double getPrice() {25 return price3.0;26}All 11 passes — pass 1 is the card above pass price1 3.0 2 2.5 3 3.0 4 3.5 5 4.0 6 2.5 7 2.5 8 3.0 9 3.0 10 3.5 11 3.5 System.out.println("Price: $" + mySize.getPrice());
36System.out.println("Ounces: " + mySize.getOunces());37System.out.println("Price: $" + mySize.getPrice());3839//@help h140// Enum constructor is implicitly private41// Called once per constant when enum is loaded42// Cannot create new enum instances with 'new'43//@end4445// Print all sizes with details46System.out.println("\nAll coffee sizes:");47for (CoffeeSize size : CoffeeSize.values()) {outputPrice: $3.0 All coffee sizes:for (CoffeeSize size : CoffeeSize.values())
pass 1 of 446System.out.println("\nAll coffee sizes:");47for (CoffeeSize sizeSMALL : CoffeeSize.values()) {48 System.out.printf("%s: %doz - $%.2f%n", 49 sizeSMALL, size.getOunces(), size.getPrice());50}All 4 passes — pass 1 is the card above pass size1 SMALL 2 MEDIUM 3 LARGE 4 EXTRA_LARGE size, size.getOunces(), size.getPrice());
47for (CoffeeSize size : CoffeeSize.values()) {48 System.out.printf("%s: %doz - $%.2f%n", 49 sizeSMALL, size.getOunces(), size.getPrice());50}size, size.getOunces(), size.getPrice());
47for (CoffeeSize size : CoffeeSize.values()) {48 System.out.printf("%s: %doz - $%.2f%n", 49 sizeMEDIUM, size.getOunces(), size.getPrice());50}size, size.getOunces(), size.getPrice());
47for (CoffeeSize size : CoffeeSize.values()) {48 System.out.printf("%s: %doz - $%.2f%n", 49 sizeLARGE, size.getOunces(), size.getPrice());50}size, size.getOunces(), size.getPrice());
47for (CoffeeSize size : CoffeeSize.values()) {48 System.out.printf("%s: %doz - $%.2f%n", 49 sizeEXTRA_LARGE, size.getOunces(), size.getPrice());50}total ← 0.0
52// Calculate total for multiple coffees53CoffeeSize[] order = {54 CoffeeSize.SMALL,55 CoffeeSize.MEDIUM,56 CoffeeSize.LARGE57};5859double total→ 0.0 = 0;60System.out.println("\nOrder:");61for (CoffeeSize size : order) {output Order:for (CoffeeSize size : order)
pass 1 of 360System.out.println("\nOrder:");61for (CoffeeSize sizeSMALL : order) {62 System.out.printf(" %s: $%.2f%n", sizeSMALL, size.getPrice());63 total += size.getPrice();All 3 passes — pass 1 is the card above pass size1 SMALL 2 MEDIUM 3 LARGE System.out.printf(" %s: $%.2f%n", size, size.getPrice());
61for (CoffeeSize size : order) {62 System.out.printf(" %s: $%.2f%n", sizeSMALL, size.getPrice());63 total0.0 += size.getPrice();64}total ← 2.5
62 System.out.printf(" %s: $%.2f%n", size, size.getPrice());63 total→ 2.5 += size.getPrice();64}System.out.printf(" %s: $%.2f%n", size, size.getPrice());
61for (CoffeeSize size : order) {62 System.out.printf(" %s: $%.2f%n", sizeMEDIUM, size.getPrice());63 total2.5 += size.getPrice();64}total ← 5.5
62 System.out.printf(" %s: $%.2f%n", size, size.getPrice());63 total→ 5.5 += size.getPrice();64}System.out.printf(" %s: $%.2f%n", size, size.getPrice());
61for (CoffeeSize size : order) {62 System.out.printf(" %s: $%.2f%n", sizeLARGE, size.getPrice());63 total5.5 += size.getPrice();64}total ← 9.0
62 System.out.printf(" %s: $%.2f%n", size, size.getPrice());63 total→ 9.0 += size.getPrice();64}System.out.printf("Total: $%.2f%n", total);
64}65System.out.printf("Total: $%.2f%n", total9.0);
public static void main(String[] args)
29public class EnumWithFields {30 public static void main(String[] args) {31 // Access enum constant data32 CoffeeSize mySize = CoffeeSize.SMALL;this.ounces ← 8, this.price ← 2.5
pass 1 of 413// Enum constructor (always private)14CoffeeSize(int ounces8, double price2.5) {15 this.ounces→ 8 = ounces8;16 this.price→ 2.5 = price2.5;17}All 4 passes — pass 1 is the card above pass ouncespricethis.ouncesthis.price1 8 2.5 8 2.5 2 12 3.0 12 3.0 3 16 3.5 16 3.5 4 20 4.0 20 4.0 mySize ← SMALL
31// Access enum constant data32CoffeeSize mySize→ SMALL = CoffeeSize.SMALL;3334System.out.println("Size: " + mySizeSMALL);35System.out.println("Ounces: " + mySize.getOunces());36System.out.println("Price: $" + mySize.getPrice());outputSize: SMALLpublic int getOunces()
pass 1 of 519// Getter methods20public int getOunces() {21 return ounces8;22}All 5 passes — pass 1 is the card above pass ounces1 8 2 8 3 12 4 16 5 20 System.out.println("Ounces: " + mySize.getOunces());
34System.out.println("Size: " + mySize);35System.out.println("Ounces: " + mySize.getOunces());36System.out.println("Price: $" + mySize.getPrice());outputOunces: 8public double getPrice()
pass 1 of 1124public double getPrice() {25 return price2.5;26}All 11 passes — pass 1 is the card above pass price1 2.5 2 2.5 3 3.0 4 3.5 5 4.0 6 2.5 7 2.5 8 3.0 9 3.0 10 3.5 11 3.5 System.out.println("Price: $" + mySize.getPrice());
35System.out.println("Ounces: " + mySize.getOunces());36System.out.println("Price: $" + mySize.getPrice());373839// Print all sizes with details40System.out.println("\nAll coffee sizes:");41for (CoffeeSize size : CoffeeSize.values()) {outputPrice: $2.5 All coffee sizes:for (CoffeeSize size : CoffeeSize.values())
pass 1 of 440System.out.println("\nAll coffee sizes:");41for (CoffeeSize sizeSMALL : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeSMALL, size.getOunces(), size.getPrice());44}All 4 passes — pass 1 is the card above pass size1 SMALL 2 MEDIUM 3 LARGE 4 EXTRA_LARGE size, size.getOunces(), size.getPrice());
41for (CoffeeSize size : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeSMALL, size.getOunces(), size.getPrice());44}size, size.getOunces(), size.getPrice());
41for (CoffeeSize size : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeMEDIUM, size.getOunces(), size.getPrice());44}size, size.getOunces(), size.getPrice());
41for (CoffeeSize size : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeLARGE, size.getOunces(), size.getPrice());44}size, size.getOunces(), size.getPrice());
41for (CoffeeSize size : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeEXTRA_LARGE, size.getOunces(), size.getPrice());44}total ← 0.0
46// Calculate total for multiple coffees47CoffeeSize[] order = {48 CoffeeSize.SMALL,49 CoffeeSize.MEDIUM,50 CoffeeSize.LARGE51};5253double total→ 0.0 = 0;54System.out.println("\nOrder:");55for (CoffeeSize size : order) {output Order:for (CoffeeSize size : order)
pass 1 of 354System.out.println("\nOrder:");55for (CoffeeSize sizeSMALL : order) {56 System.out.printf(" %s: $%.2f%n", sizeSMALL, size.getPrice());57 total += size.getPrice();All 3 passes — pass 1 is the card above pass size1 SMALL 2 MEDIUM 3 LARGE System.out.printf(" %s: $%.2f%n", size, size.getPrice());
55for (CoffeeSize size : order) {56 System.out.printf(" %s: $%.2f%n", sizeSMALL, size.getPrice());57 total0.0 += size.getPrice();58}total ← 2.5
56 System.out.printf(" %s: $%.2f%n", size, size.getPrice());57 total→ 2.5 += size.getPrice();58}System.out.printf(" %s: $%.2f%n", size, size.getPrice());
55for (CoffeeSize size : order) {56 System.out.printf(" %s: $%.2f%n", sizeMEDIUM, size.getPrice());57 total2.5 += size.getPrice();58}total ← 5.5
56 System.out.printf(" %s: $%.2f%n", size, size.getPrice());57 total→ 5.5 += size.getPrice();58}System.out.printf(" %s: $%.2f%n", size, size.getPrice());
55for (CoffeeSize size : order) {56 System.out.printf(" %s: $%.2f%n", sizeLARGE, size.getPrice());57 total5.5 += size.getPrice();58}total ← 9.0
56 System.out.printf(" %s: $%.2f%n", size, size.getPrice());57 total→ 9.0 += size.getPrice();58}System.out.printf("Total: $%.2f%n", total);
58}59System.out.printf("Total: $%.2f%n", total9.0);
public static void main(String[] args)
29public class EnumWithFields {30 public static void main(String[] args) {31 // Access enum constant data32 CoffeeSize mySize = CoffeeSize.LARGE;this.ounces ← 8, this.price ← 2.5
pass 1 of 413// Enum constructor (always private)14CoffeeSize(int ounces8, double price2.5) {15 this.ounces→ 8 = ounces8;16 this.price→ 2.5 = price2.5;17}All 4 passes — pass 1 is the card above pass ouncespricethis.ouncesthis.price1 8 2.5 8 2.5 2 12 3.0 12 3.0 3 16 3.5 16 3.5 4 20 4.0 20 4.0 mySize ← LARGE
31// Access enum constant data32CoffeeSize mySize→ LARGE = CoffeeSize.LARGE;3334System.out.println("Size: " + mySizeLARGE);35System.out.println("Ounces: " + mySize.getOunces());36System.out.println("Price: $" + mySize.getPrice());outputSize: LARGEpublic int getOunces()
pass 1 of 519// Getter methods20public int getOunces() {21 return ounces16;22}All 5 passes — pass 1 is the card above pass ounces1 16 2 8 3 12 4 16 5 20 System.out.println("Ounces: " + mySize.getOunces());
34System.out.println("Size: " + mySize);35System.out.println("Ounces: " + mySize.getOunces());36System.out.println("Price: $" + mySize.getPrice());outputOunces: 16public double getPrice()
pass 1 of 1124public double getPrice() {25 return price3.5;26}All 11 passes — pass 1 is the card above pass price1 3.5 2 2.5 3 3.0 4 3.5 5 4.0 6 2.5 7 2.5 8 3.0 9 3.0 10 3.5 11 3.5 System.out.println("Price: $" + mySize.getPrice());
35System.out.println("Ounces: " + mySize.getOunces());36System.out.println("Price: $" + mySize.getPrice());373839// Print all sizes with details40System.out.println("\nAll coffee sizes:");41for (CoffeeSize size : CoffeeSize.values()) {outputPrice: $3.5 All coffee sizes:for (CoffeeSize size : CoffeeSize.values())
pass 1 of 440System.out.println("\nAll coffee sizes:");41for (CoffeeSize sizeSMALL : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeSMALL, size.getOunces(), size.getPrice());44}All 4 passes — pass 1 is the card above pass size1 SMALL 2 MEDIUM 3 LARGE 4 EXTRA_LARGE size, size.getOunces(), size.getPrice());
41for (CoffeeSize size : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeSMALL, size.getOunces(), size.getPrice());44}size, size.getOunces(), size.getPrice());
41for (CoffeeSize size : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeMEDIUM, size.getOunces(), size.getPrice());44}size, size.getOunces(), size.getPrice());
41for (CoffeeSize size : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeLARGE, size.getOunces(), size.getPrice());44}size, size.getOunces(), size.getPrice());
41for (CoffeeSize size : CoffeeSize.values()) {42 System.out.printf("%s: %doz - $%.2f%n", 43 sizeEXTRA_LARGE, size.getOunces(), size.getPrice());44}total ← 0.0
46// Calculate total for multiple coffees47CoffeeSize[] order = {48 CoffeeSize.SMALL,49 CoffeeSize.MEDIUM,50 CoffeeSize.LARGE51};5253double total→ 0.0 = 0;54System.out.println("\nOrder:");55for (CoffeeSize size : order) {output Order:for (CoffeeSize size : order)
pass 1 of 354System.out.println("\nOrder:");55for (CoffeeSize sizeSMALL : order) {56 System.out.printf(" %s: $%.2f%n", sizeSMALL, size.getPrice());57 total += size.getPrice();All 3 passes — pass 1 is the card above pass size1 SMALL 2 MEDIUM 3 LARGE System.out.printf(" %s: $%.2f%n", size, size.getPrice());
55for (CoffeeSize size : order) {56 System.out.printf(" %s: $%.2f%n", sizeSMALL, size.getPrice());57 total0.0 += size.getPrice();58}total ← 2.5
56 System.out.printf(" %s: $%.2f%n", size, size.getPrice());57 total→ 2.5 += size.getPrice();58}System.out.printf(" %s: $%.2f%n", size, size.getPrice());
55for (CoffeeSize size : order) {56 System.out.printf(" %s: $%.2f%n", sizeMEDIUM, size.getPrice());57 total2.5 += size.getPrice();58}total ← 5.5
56 System.out.printf(" %s: $%.2f%n", size, size.getPrice());57 total→ 5.5 += size.getPrice();58}System.out.printf(" %s: $%.2f%n", size, size.getPrice());
55for (CoffeeSize size : order) {56 System.out.printf(" %s: $%.2f%n", sizeLARGE, size.getPrice());57 total5.5 += size.getPrice();58}total ← 9.0
56 System.out.printf(" %s: $%.2f%n", size, size.getPrice());57 total→ 9.0 += size.getPrice();58}System.out.printf("Total: $%.2f%n", total);
58}59System.out.printf("Total: $%.2f%n", total9.0);
Constructor assigns values. Each constant has its own data.
Enum methods
Add behavior to enums.
// Enum with methods
// Concept: enum methods - behavior in enum constants
enum Temperature {
FREEZING(32),
COLD(50),
MILD(65),
WARM(75),
HOT(90);
private final int fahrenheit;
Temperature(int fahrenheit) {
this.fahrenheit = fahrenheit;
}
// Convert to Celsius
public double toCelsius() {
return (fahrenheit - 32) * 5.0 / 9.0;
}
// Get description
public String getDescription() {
return switch (this) {
case FREEZING -> "Water freezes";
case COLD -> "Wear a jacket";
case MILD -> "Pleasant weather";
case WARM -> "T-shirt weather";
case HOT -> "Stay hydrated";
};
}
// Check if comfortable
public boolean isComfortable() {
return fahrenheit >= 65 && fahrenheit <= 75;
}
public int getFahrenheit() {
return fahrenheit;
}
}
public class EnumMethods {
public static void main(String[] args) {
// Use enum methods
Temperature current = Temperature.MILD;
System.out.println("Temperature: " + current);
System.out.println("Fahrenheit: " + current.getFahrenheit());
System.out.println("Celsius: " + current.toCelsius());
System.out.println("Description: " + current.getDescription());
System.out.println("Comfortable? " + current.isComfortable());
// Print all temperatures with conversions
System.out.println("\nAll temperatures:");
for (Temperature t : Temperature.values()) {
System.out.printf("%s: %d°F = %.1f°C - %s%n",
t,
t.getFahrenheit(),
t.toCelsius(),
t.isComfortable() ? "✓" : "✗");
}
// Find comfortable temperatures
System.out.println("\nComfortable temperatures:");
for (Temperature t : Temperature.values()) {
if (t.isComfortable()) {
System.out.println(" " + t + ": " + t.getDescription());
}
}
// Compare temperatures
Temperature t1 = Temperature.COLD;
Temperature t2 = Temperature.HOT;
System.out.println("\nComparison:");
System.out.printf("%s (%d°F) vs %s (%d°F)%n",
t1, t1.getFahrenheit(),
t2, t2.getFahrenheit());
if (t1.getFahrenheit() < t2.getFahrenheit()) {
System.out.println(t1 + " is cooler");
}
}
}
// Enum with methods
// Concept: enum methods - behavior in enum constants
enum Temperature {
FREEZING(32),
COLD(50),
MILD(65),
WARM(75),
HOT(90);
private final int fahrenheit;
Temperature(int fahrenheit) {
this.fahrenheit = fahrenheit;
}
// Convert to Celsius
public double toCelsius() {
return (fahrenheit - 32) * 5.0 / 9.0;
}
// Get description
public String getDescription() {
return switch (this) {
case FREEZING -> "Water freezes";
case COLD -> "Wear a jacket";
case MILD -> "Pleasant weather";
case WARM -> "T-shirt weather";
case HOT -> "Stay hydrated";
};
}
// Check if comfortable
public boolean isComfortable() {
return fahrenheit >= 65 && fahrenheit <= 75;
}
public int getFahrenheit() {
return fahrenheit;
}
}
public class EnumMethods {
public static void main(String[] args) {
// Use enum methods
Temperature current = Temperature.COLD;
System.out.println("Temperature: " + current);
System.out.println("Fahrenheit: " + current.getFahrenheit());
System.out.println("Celsius: " + current.toCelsius());
System.out.println("Description: " + current.getDescription());
System.out.println("Comfortable? " + current.isComfortable());
// Print all temperatures with conversions
System.out.println("\nAll temperatures:");
for (Temperature t : Temperature.values()) {
System.out.printf("%s: %d°F = %.1f°C - %s%n",
t,
t.getFahrenheit(),
t.toCelsius(),
t.isComfortable() ? "✓" : "✗");
}
// Find comfortable temperatures
System.out.println("\nComfortable temperatures:");
for (Temperature t : Temperature.values()) {
if (t.isComfortable()) {
System.out.println(" " + t + ": " + t.getDescription());
}
}
// Compare temperatures
Temperature t1 = Temperature.COLD;
Temperature t2 = Temperature.HOT;
System.out.println("\nComparison:");
System.out.printf("%s (%d°F) vs %s (%d°F)%n",
t1, t1.getFahrenheit(),
t2, t2.getFahrenheit());
if (t1.getFahrenheit() < t2.getFahrenheit()) {
System.out.println(t1 + " is cooler");
}
}
}
// Enum with methods
// Concept: enum methods - behavior in enum constants
enum Temperature {
FREEZING(32),
COLD(50),
MILD(65),
WARM(75),
HOT(90);
private final int fahrenheit;
Temperature(int fahrenheit) {
this.fahrenheit = fahrenheit;
}
// Convert to Celsius
public double toCelsius() {
return (fahrenheit - 32) * 5.0 / 9.0;
}
// Get description
public String getDescription() {
return switch (this) {
case FREEZING -> "Water freezes";
case COLD -> "Wear a jacket";
case MILD -> "Pleasant weather";
case WARM -> "T-shirt weather";
case HOT -> "Stay hydrated";
};
}
// Check if comfortable
public boolean isComfortable() {
return fahrenheit >= 65 && fahrenheit <= 75;
}
public int getFahrenheit() {
return fahrenheit;
}
}
public class EnumMethods {
public static void main(String[] args) {
// Use enum methods
Temperature current = Temperature.HOT;
System.out.println("Temperature: " + current);
System.out.println("Fahrenheit: " + current.getFahrenheit());
System.out.println("Celsius: " + current.toCelsius());
System.out.println("Description: " + current.getDescription());
System.out.println("Comfortable? " + current.isComfortable());
// Print all temperatures with conversions
System.out.println("\nAll temperatures:");
for (Temperature t : Temperature.values()) {
System.out.printf("%s: %d°F = %.1f°C - %s%n",
t,
t.getFahrenheit(),
t.toCelsius(),
t.isComfortable() ? "✓" : "✗");
}
// Find comfortable temperatures
System.out.println("\nComfortable temperatures:");
for (Temperature t : Temperature.values()) {
if (t.isComfortable()) {
System.out.println(" " + t + ": " + t.getDescription());
}
}
// Compare temperatures
Temperature t1 = Temperature.COLD;
Temperature t2 = Temperature.HOT;
System.out.println("\nComparison:");
System.out.printf("%s (%d°F) vs %s (%d°F)%n",
t1, t1.getFahrenheit(),
t2, t2.getFahrenheit());
if (t1.getFahrenheit() < t2.getFahrenheit()) {
System.out.println(t1 + " is cooler");
}
}
}
public static void main(String[] args)
43public class EnumMethods {44 public static void main(String[] args) {45 // Use enum methods46 Temperature current = Temperature.MILD;47 //@current=Temperature.MILD, Temperature.COLD, Temperature.HOTthis.fahrenheit ← 32
pass 1 of 513Temperature(int fahrenheit32) {14 this.fahrenheit→ 32 = fahrenheit32;15}All 5 passes — pass 1 is the card above pass fahrenheitthis.fahrenheit1 32 32 2 50 50 3 65 65 4 75 75 5 90 90 current ← MILD
45// Use enum methods46Temperature current→ MILD = Temperature.MILD;47//@current=Temperature.MILD, Temperature.COLD, Temperature.HOT4849System.out.println("Temperature: " + currentMILD);50System.out.println("Fahrenheit: " + current.getFahrenheit());51System.out.println("Celsius: " + current.toCelsius());outputTemperature: MILDpublic int getFahrenheit()
pass 1 of 1038public int getFahrenheit() {39 return fahrenheit65;40}All 10 passes — pass 1 is the card above pass fahrenheitt11 65 — 2 32 — 3 50 — 4 65 — 5 75 — 6 90 — 7 50 — 8 90 — 9 50 — 10 90 COLD System.out.println("Fahrenheit: " + current.getFahrenheit());
49System.out.println("Temperature: " + current);50System.out.println("Fahrenheit: " + current.getFahrenheit());51System.out.println("Celsius: " + current.toCelsius());52System.out.println("Description: " + current.getDescription());outputFahrenheit: 65public double toCelsius()
pass 1 of 617// Convert to Celsius18public double toCelsius() {19 return (fahrenheit65 - 32) * 5.0 / 9.0;20}All 6 passes — pass 1 is the card above pass fahrenheit1 65 2 32 3 50 4 65 5 75 6 90 System.out.println("Celsius: " + current.toCelsius());
50System.out.println("Fahrenheit: " + current.getFahrenheit());51System.out.println("Celsius: " + current.toCelsius());52System.out.println("Description: " + current.getDescription());53System.out.println("Comfortable? " + current.isComfortable());outputCelsius: 18.333333333333332System.out.println("Description: " + current.getDescription());
51System.out.println("Celsius: " + current.toCelsius());52System.out.println("Description: " + current.getDescription());53System.out.println("Comfortable? " + current.isComfortable());outputDescription: Pleasant weatherpublic boolean isComfortable()
pass 1 of 1133// Check if comfortable34public boolean isComfortable() {35 return fahrenheit65 >= 65 && fahrenheit <= 75;36}All 11 passes — pass 1 is the card above pass fahrenheitt1 65 — 2 32 — 3 50 — 4 65 — 5 75 — 6 90 — 7 32 — 8 50 — 9 65 MILD 10 75 WARM 11 90 — System.out.println("Comfortable? " + current.isComfortable());
52System.out.println("Description: " + current.getDescription());53System.out.println("Comfortable? " + current.isComfortable());5455//@help h156// Enum methods can access the enum's fields57// Can use 'this' to refer to current constant58// Methods work just like class methods59//@end6061// Print all temperatures with conversions62System.out.println("\nAll temperatures:");63for (Temperature t : Temperature.values()) {outputComfortable? true All temperatures:for (Temperature t : Temperature.values())
pass 1 of 562System.out.println("\nAll temperatures:");63for (Temperature tFREEZING : Temperature.values()) {64 System.out.printf("%s: %d°F = %.1f°C - %s%n",65 tFREEZING, 66 t.getFahrenheit(),67 t.toCelsius(),68 t.isComfortable() ? "✓" : "✗");69}All 5 passes — pass 1 is the card above pass t1 FREEZING 2 COLD 3 MILD 4 WARM 5 HOT t,
63for (Temperature t : Temperature.values()) {64 System.out.printf("%s: %d°F = %.1f°C - %s%n",65 tFREEZING, 66 t.getFahrenheit(),67 t.toCelsius(),68 t.isComfortable() ? "✓" : "✗");69}t,
63for (Temperature t : Temperature.values()) {64 System.out.printf("%s: %d°F = %.1f°C - %s%n",65 tCOLD, 66 t.getFahrenheit(),67 t.toCelsius(),68 t.isComfortable() ? "✓" : "✗");69}t,
63for (Temperature t : Temperature.values()) {64 System.out.printf("%s: %d°F = %.1f°C - %s%n",65 tMILD, 66 t.getFahrenheit(),67 t.toCelsius(),68 t.isComfortable() ? "✓" : "✗");69}t,
63for (Temperature t : Temperature.values()) {64 System.out.printf("%s: %d°F = %.1f°C - %s%n",65 tWARM, 66 t.getFahrenheit(),67 t.toCelsius(),68 t.isComfortable() ? "✓" : "✗");69}t,
63for (Temperature t : Temperature.values()) {64 System.out.printf("%s: %d°F = %.1f°C - %s%n",65 tHOT, 66 t.getFahrenheit(),67 t.toCelsius(),68 t.isComfortable() ? "✓" : "✗");69}System.out.println(" Comfortable temperatures:");
71// Find comfortable temperatures72System.out.println("\nComfortable temperatures:");73for (Temperature t : Temperature.values()) {output Comfortable temperatures:for (Temperature t : Temperature.values())
pass 1 of 572System.out.println("\nComfortable temperatures:");73for (Temperature tFREEZING : Temperature.values()) {74 if (t.isComfortable()) {All 5 passes — pass 1 is the card above pass t1 FREEZING 2 COLD 3 MILD 4 WARM 5 HOT if (t.isComfortable())
pass 1 of 273for (Temperature t : Temperature.values()) {74 if (t.isComfortable()) {75 System.out.println(" " + tMILD + ": " + t.getDescription());76 }System.out.println(" " + t + ": " + t.getDescription());
74if (t.isComfortable()) {75 System.out.println(" " + tMILD + ": " + t.getDescription());76}output MILD: Pleasant weatherif (t.isComfortable())
pass 2 of 273for (Temperature t : Temperature.values()) {74 if (t.isComfortable()) {75 System.out.println(" " + tWARM + ": " + t.getDescription());76 }System.out.println(" " + t + ": " + t.getDescription());
74if (t.isComfortable()) {75 System.out.println(" " + tWARM + ": " + t.getDescription());76}output WARM: T-shirt weathert1 ← COLD, t2 ← HOT
79// Compare temperatures80Temperature t1→ COLD = Temperature.COLD;81Temperature t2→ HOT = Temperature.HOT;8283System.out.println("\nComparison:");84System.out.printf("%s (%d°F) vs %s (%d°F)%n",85 t1COLD, t1.getFahrenheit(),86 t2HOT, t2.getFahrenheit());output Comparison:t1, t1.getFahrenheit(),
83System.out.println("\nComparison:");84System.out.printf("%s (%d°F) vs %s (%d°F)%n",85 t1COLD, t1.getFahrenheit(),86 t2HOT, t2.getFahrenheit());if (t1.getFahrenheit() < t2.getFahrenheit())
88if (t1.getFahrenheit() < t2.getFahrenheit()) {89 System.out.println(t1COLD + " is cooler");90}outputCOLD is cooler
public static void main(String[] args)
43public class EnumMethods {44 public static void main(String[] args) {45 // Use enum methods46 Temperature current = Temperature.COLD;this.fahrenheit ← 32
pass 1 of 513Temperature(int fahrenheit32) {14 this.fahrenheit→ 32 = fahrenheit32;15}All 5 passes — pass 1 is the card above pass fahrenheitthis.fahrenheit1 32 32 2 50 50 3 65 65 4 75 75 5 90 90 current ← COLD
45// Use enum methods46Temperature current→ COLD = Temperature.COLD;4748System.out.println("Temperature: " + currentCOLD);49System.out.println("Fahrenheit: " + current.getFahrenheit());50System.out.println("Celsius: " + current.toCelsius());outputTemperature: COLDpublic int getFahrenheit()
pass 1 of 1038public int getFahrenheit() {39 return fahrenheit50;40}All 10 passes — pass 1 is the card above pass fahrenheitt11 50 — 2 32 — 3 50 — 4 65 — 5 75 — 6 90 — 7 50 — 8 90 — 9 50 — 10 90 COLD System.out.println("Fahrenheit: " + current.getFahrenheit());
48System.out.println("Temperature: " + current);49System.out.println("Fahrenheit: " + current.getFahrenheit());50System.out.println("Celsius: " + current.toCelsius());51System.out.println("Description: " + current.getDescription());outputFahrenheit: 50public double toCelsius()
pass 1 of 617// Convert to Celsius18public double toCelsius() {19 return (fahrenheit50 - 32) * 5.0 / 9.0;20}All 6 passes — pass 1 is the card above pass fahrenheit1 50 2 32 3 50 4 65 5 75 6 90 System.out.println("Celsius: " + current.toCelsius());
49System.out.println("Fahrenheit: " + current.getFahrenheit());50System.out.println("Celsius: " + current.toCelsius());51System.out.println("Description: " + current.getDescription());52System.out.println("Comfortable? " + current.isComfortable());outputCelsius: 10.0System.out.println("Description: " + current.getDescription());
50System.out.println("Celsius: " + current.toCelsius());51System.out.println("Description: " + current.getDescription());52System.out.println("Comfortable? " + current.isComfortable());outputDescription: Wear a jacketpublic boolean isComfortable()
pass 1 of 1133// Check if comfortable34public boolean isComfortable() {35 return fahrenheit50 >= 65 && fahrenheit <= 75;36}All 11 passes — pass 1 is the card above pass fahrenheitt1 50 — 2 32 — 3 50 — 4 65 — 5 75 — 6 90 — 7 32 — 8 50 — 9 65 MILD 10 75 WARM 11 90 — System.out.println("Comfortable? " + current.isComfortable());
51System.out.println("Description: " + current.getDescription());52System.out.println("Comfortable? " + current.isComfortable());535455// Print all temperatures with conversions56System.out.println("\nAll temperatures:");57for (Temperature t : Temperature.values()) {outputComfortable? false All temperatures:for (Temperature t : Temperature.values())
pass 1 of 556System.out.println("\nAll temperatures:");57for (Temperature tFREEZING : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tFREEZING, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}All 5 passes — pass 1 is the card above pass t1 FREEZING 2 COLD 3 MILD 4 WARM 5 HOT t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tFREEZING, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tCOLD, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tMILD, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tWARM, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tHOT, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}System.out.println(" Comfortable temperatures:");
65// Find comfortable temperatures66System.out.println("\nComfortable temperatures:");67for (Temperature t : Temperature.values()) {output Comfortable temperatures:for (Temperature t : Temperature.values())
pass 1 of 566System.out.println("\nComfortable temperatures:");67for (Temperature tFREEZING : Temperature.values()) {68 if (t.isComfortable()) {All 5 passes — pass 1 is the card above pass t1 FREEZING 2 COLD 3 MILD 4 WARM 5 HOT if (t.isComfortable())
pass 1 of 267for (Temperature t : Temperature.values()) {68 if (t.isComfortable()) {69 System.out.println(" " + tMILD + ": " + t.getDescription());70 }System.out.println(" " + t + ": " + t.getDescription());
68if (t.isComfortable()) {69 System.out.println(" " + tMILD + ": " + t.getDescription());70}output MILD: Pleasant weatherif (t.isComfortable())
pass 2 of 267for (Temperature t : Temperature.values()) {68 if (t.isComfortable()) {69 System.out.println(" " + tWARM + ": " + t.getDescription());70 }System.out.println(" " + t + ": " + t.getDescription());
68if (t.isComfortable()) {69 System.out.println(" " + tWARM + ": " + t.getDescription());70}output WARM: T-shirt weathert1 ← COLD, t2 ← HOT
73// Compare temperatures74Temperature t1→ COLD = Temperature.COLD;75Temperature t2→ HOT = Temperature.HOT;7677System.out.println("\nComparison:");78System.out.printf("%s (%d°F) vs %s (%d°F)%n",79 t1COLD, t1.getFahrenheit(),80 t2HOT, t2.getFahrenheit());output Comparison:t1, t1.getFahrenheit(),
77System.out.println("\nComparison:");78System.out.printf("%s (%d°F) vs %s (%d°F)%n",79 t1COLD, t1.getFahrenheit(),80 t2HOT, t2.getFahrenheit());if (t1.getFahrenheit() < t2.getFahrenheit())
82if (t1.getFahrenheit() < t2.getFahrenheit()) {83 System.out.println(t1COLD + " is cooler");84}outputCOLD is cooler
public static void main(String[] args)
43public class EnumMethods {44 public static void main(String[] args) {45 // Use enum methods46 Temperature current = Temperature.HOT;this.fahrenheit ← 32
pass 1 of 513Temperature(int fahrenheit32) {14 this.fahrenheit→ 32 = fahrenheit32;15}All 5 passes — pass 1 is the card above pass fahrenheitthis.fahrenheit1 32 32 2 50 50 3 65 65 4 75 75 5 90 90 current ← HOT
45// Use enum methods46Temperature current→ HOT = Temperature.HOT;4748System.out.println("Temperature: " + currentHOT);49System.out.println("Fahrenheit: " + current.getFahrenheit());50System.out.println("Celsius: " + current.toCelsius());outputTemperature: HOTpublic int getFahrenheit()
pass 1 of 1038public int getFahrenheit() {39 return fahrenheit90;40}All 10 passes — pass 1 is the card above pass fahrenheitt11 90 — 2 32 — 3 50 — 4 65 — 5 75 — 6 90 — 7 50 — 8 90 — 9 50 — 10 90 COLD System.out.println("Fahrenheit: " + current.getFahrenheit());
48System.out.println("Temperature: " + current);49System.out.println("Fahrenheit: " + current.getFahrenheit());50System.out.println("Celsius: " + current.toCelsius());51System.out.println("Description: " + current.getDescription());outputFahrenheit: 90public double toCelsius()
pass 1 of 617// Convert to Celsius18public double toCelsius() {19 return (fahrenheit90 - 32) * 5.0 / 9.0;20}All 6 passes — pass 1 is the card above pass fahrenheit1 90 2 32 3 50 4 65 5 75 6 90 System.out.println("Celsius: " + current.toCelsius());
49System.out.println("Fahrenheit: " + current.getFahrenheit());50System.out.println("Celsius: " + current.toCelsius());51System.out.println("Description: " + current.getDescription());52System.out.println("Comfortable? " + current.isComfortable());outputCelsius: 32.22222222222222System.out.println("Description: " + current.getDescription());
50System.out.println("Celsius: " + current.toCelsius());51System.out.println("Description: " + current.getDescription());52System.out.println("Comfortable? " + current.isComfortable());outputDescription: Stay hydratedpublic boolean isComfortable()
pass 1 of 1133// Check if comfortable34public boolean isComfortable() {35 return fahrenheit90 >= 65 && fahrenheit <= 75;36}All 11 passes — pass 1 is the card above pass fahrenheitt1 90 — 2 32 — 3 50 — 4 65 — 5 75 — 6 90 — 7 32 — 8 50 — 9 65 MILD 10 75 WARM 11 90 — System.out.println("Comfortable? " + current.isComfortable());
51System.out.println("Description: " + current.getDescription());52System.out.println("Comfortable? " + current.isComfortable());535455// Print all temperatures with conversions56System.out.println("\nAll temperatures:");57for (Temperature t : Temperature.values()) {outputComfortable? false All temperatures:for (Temperature t : Temperature.values())
pass 1 of 556System.out.println("\nAll temperatures:");57for (Temperature tFREEZING : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tFREEZING, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}All 5 passes — pass 1 is the card above pass t1 FREEZING 2 COLD 3 MILD 4 WARM 5 HOT t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tFREEZING, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tCOLD, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tMILD, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tWARM, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}t,
57for (Temperature t : Temperature.values()) {58 System.out.printf("%s: %d°F = %.1f°C - %s%n",59 tHOT, 60 t.getFahrenheit(),61 t.toCelsius(),62 t.isComfortable() ? "✓" : "✗");63}System.out.println(" Comfortable temperatures:");
65// Find comfortable temperatures66System.out.println("\nComfortable temperatures:");67for (Temperature t : Temperature.values()) {output Comfortable temperatures:for (Temperature t : Temperature.values())
pass 1 of 566System.out.println("\nComfortable temperatures:");67for (Temperature tFREEZING : Temperature.values()) {68 if (t.isComfortable()) {All 5 passes — pass 1 is the card above pass t1 FREEZING 2 COLD 3 MILD 4 WARM 5 HOT if (t.isComfortable())
pass 1 of 267for (Temperature t : Temperature.values()) {68 if (t.isComfortable()) {69 System.out.println(" " + tMILD + ": " + t.getDescription());70 }System.out.println(" " + t + ": " + t.getDescription());
68if (t.isComfortable()) {69 System.out.println(" " + tMILD + ": " + t.getDescription());70}output MILD: Pleasant weatherif (t.isComfortable())
pass 2 of 267for (Temperature t : Temperature.values()) {68 if (t.isComfortable()) {69 System.out.println(" " + tWARM + ": " + t.getDescription());70 }System.out.println(" " + t + ": " + t.getDescription());
68if (t.isComfortable()) {69 System.out.println(" " + tWARM + ": " + t.getDescription());70}output WARM: T-shirt weathert1 ← COLD, t2 ← HOT
73// Compare temperatures74Temperature t1→ COLD = Temperature.COLD;75Temperature t2→ HOT = Temperature.HOT;7677System.out.println("\nComparison:");78System.out.printf("%s (%d°F) vs %s (%d°F)%n",79 t1COLD, t1.getFahrenheit(),80 t2HOT, t2.getFahrenheit());output Comparison:t1, t1.getFahrenheit(),
77System.out.println("\nComparison:");78System.out.printf("%s (%d°F) vs %s (%d°F)%n",79 t1COLD, t1.getFahrenheit(),80 t2HOT, t2.getFahrenheit());if (t1.getFahrenheit() < t2.getFahrenheit())
82if (t1.getFahrenheit() < t2.getFahrenheit()) {83 System.out.println(t1COLD + " is cooler");84}outputCOLD is cooler
Regular methods work with enum's fields. All constants share the method.
Complex enum
Full-featured enum with multiple fields and methods.
// Enum with multiple fields and complex logic
// Concept: complex enum - multiple fields and calculations
enum ShippingMethod {
STANDARD(5.99, 5, "5-7 business days"),
EXPRESS(12.99, 2, "2-3 business days"),
OVERNIGHT(24.99, 1, "Next business day"),
INTERNATIONAL(35.00, 10, "7-14 business days");
private final double baseCost;
private final int daysMin;
private final String description;
ShippingMethod(double baseCost, int daysMin, String description) {
this.baseCost = baseCost;
this.daysMin = daysMin;
this.description = description;
}
// Calculate cost with weight
public double calculateCost(double weightPounds) {
if (this == INTERNATIONAL) {
return baseCost + (weightPounds * 2.50);
} else if (this == OVERNIGHT) {
return baseCost + (weightPounds * 1.50);
} else {
return baseCost + (weightPounds * 0.50);
}
}
// Check if available for weight
public boolean isAvailableFor(double weightPounds) {
if (this == OVERNIGHT && weightPounds > 50) {
return false; // Overnight has weight limit
}
return true;
}
public double getBaseCost() { return baseCost; }
public String getDescription() { return description; }
public int getDaysMin() { return daysMin; }
}
public class ComplexEnum {
public static void main(String[] args) {
// Calculate shipping for different weights
double packageWeight = 10.0;
System.out.println("Package weight: " + packageWeight + " lbs\n");
System.out.println("Shipping options:");
for (ShippingMethod method : ShippingMethod.values()) {
if (method.isAvailableFor(packageWeight)) {
double cost = method.calculateCost(packageWeight);
System.out.printf("%s: $%.2f (%s)%n",
method, cost, method.getDescription());
} else {
System.out.printf("%s: Not available for this weight%n", method);
}
}
// Find cheapest option
double weight = 5.0;
ShippingMethod cheapest = ShippingMethod.STANDARD;
double lowestCost = cheapest.calculateCost(weight);
for (ShippingMethod method : ShippingMethod.values()) {
if (method.isAvailableFor(weight)) {
double cost = method.calculateCost(weight);
if (cost < lowestCost) {
cheapest = method;
lowestCost = cost;
}
}
}
System.out.printf("\nFor %.1f lbs, cheapest: %s ($%.2f)%n",
weight, cheapest, lowestCost);
// Find fastest under budget
double budget = 15.00;
ShippingMethod fastest = null;
for (ShippingMethod method : ShippingMethod.values()) {
double cost = method.calculateCost(weight);
if (cost <= budget) {
if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {
fastest = method;
}
}
}
if (fastest != null) {
System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",
budget, fastest,
(double)fastest.getDaysMin(),
fastest.calculateCost(weight));
}
}
}
// Enum with multiple fields and complex logic
// Concept: complex enum - multiple fields and calculations
enum ShippingMethod {
STANDARD(5.99, 5, "5-7 business days"),
EXPRESS(12.99, 2, "2-3 business days"),
OVERNIGHT(24.99, 1, "Next business day"),
INTERNATIONAL(35.00, 10, "7-14 business days");
private final double baseCost;
private final int daysMin;
private final String description;
ShippingMethod(double baseCost, int daysMin, String description) {
this.baseCost = baseCost;
this.daysMin = daysMin;
this.description = description;
}
// Calculate cost with weight
public double calculateCost(double weightPounds) {
if (this == INTERNATIONAL) {
return baseCost + (weightPounds * 2.50);
} else if (this == OVERNIGHT) {
return baseCost + (weightPounds * 1.50);
} else {
return baseCost + (weightPounds * 0.50);
}
}
// Check if available for weight
public boolean isAvailableFor(double weightPounds) {
if (this == OVERNIGHT && weightPounds > 50) {
return false; // Overnight has weight limit
}
return true;
}
public double getBaseCost() { return baseCost; }
public String getDescription() { return description; }
public int getDaysMin() { return daysMin; }
}
public class ComplexEnum {
public static void main(String[] args) {
// Calculate shipping for different weights
double packageWeight = 20.0;
System.out.println("Package weight: " + packageWeight + " lbs\n");
System.out.println("Shipping options:");
for (ShippingMethod method : ShippingMethod.values()) {
if (method.isAvailableFor(packageWeight)) {
double cost = method.calculateCost(packageWeight);
System.out.printf("%s: $%.2f (%s)%n",
method, cost, method.getDescription());
} else {
System.out.printf("%s: Not available for this weight%n", method);
}
}
// Find cheapest option
double weight = 5.0;
ShippingMethod cheapest = ShippingMethod.STANDARD;
double lowestCost = cheapest.calculateCost(weight);
for (ShippingMethod method : ShippingMethod.values()) {
if (method.isAvailableFor(weight)) {
double cost = method.calculateCost(weight);
if (cost < lowestCost) {
cheapest = method;
lowestCost = cost;
}
}
}
System.out.printf("\nFor %.1f lbs, cheapest: %s ($%.2f)%n",
weight, cheapest, lowestCost);
// Find fastest under budget
double budget = 15.00;
ShippingMethod fastest = null;
for (ShippingMethod method : ShippingMethod.values()) {
double cost = method.calculateCost(weight);
if (cost <= budget) {
if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {
fastest = method;
}
}
}
if (fastest != null) {
System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",
budget, fastest,
(double)fastest.getDaysMin(),
fastest.calculateCost(weight));
}
}
}
// Enum with multiple fields and complex logic
// Concept: complex enum - multiple fields and calculations
enum ShippingMethod {
STANDARD(5.99, 5, "5-7 business days"),
EXPRESS(12.99, 2, "2-3 business days"),
OVERNIGHT(24.99, 1, "Next business day"),
INTERNATIONAL(35.00, 10, "7-14 business days");
private final double baseCost;
private final int daysMin;
private final String description;
ShippingMethod(double baseCost, int daysMin, String description) {
this.baseCost = baseCost;
this.daysMin = daysMin;
this.description = description;
}
// Calculate cost with weight
public double calculateCost(double weightPounds) {
if (this == INTERNATIONAL) {
return baseCost + (weightPounds * 2.50);
} else if (this == OVERNIGHT) {
return baseCost + (weightPounds * 1.50);
} else {
return baseCost + (weightPounds * 0.50);
}
}
// Check if available for weight
public boolean isAvailableFor(double weightPounds) {
if (this == OVERNIGHT && weightPounds > 50) {
return false; // Overnight has weight limit
}
return true;
}
public double getBaseCost() { return baseCost; }
public String getDescription() { return description; }
public int getDaysMin() { return daysMin; }
}
public class ComplexEnum {
public static void main(String[] args) {
// Calculate shipping for different weights
double packageWeight = 60.0;
System.out.println("Package weight: " + packageWeight + " lbs\n");
System.out.println("Shipping options:");
for (ShippingMethod method : ShippingMethod.values()) {
if (method.isAvailableFor(packageWeight)) {
double cost = method.calculateCost(packageWeight);
System.out.printf("%s: $%.2f (%s)%n",
method, cost, method.getDescription());
} else {
System.out.printf("%s: Not available for this weight%n", method);
}
}
// Find cheapest option
double weight = 5.0;
ShippingMethod cheapest = ShippingMethod.STANDARD;
double lowestCost = cheapest.calculateCost(weight);
for (ShippingMethod method : ShippingMethod.values()) {
if (method.isAvailableFor(weight)) {
double cost = method.calculateCost(weight);
if (cost < lowestCost) {
cheapest = method;
lowestCost = cost;
}
}
}
System.out.printf("\nFor %.1f lbs, cheapest: %s ($%.2f)%n",
weight, cheapest, lowestCost);
// Find fastest under budget
double budget = 15.00;
ShippingMethod fastest = null;
for (ShippingMethod method : ShippingMethod.values()) {
double cost = method.calculateCost(weight);
if (cost <= budget) {
if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {
fastest = method;
}
}
}
if (fastest != null) {
System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",
budget, fastest,
(double)fastest.getDaysMin(),
fastest.calculateCost(weight));
}
}
}
// Enum with multiple fields and complex logic
// Concept: complex enum - multiple fields and calculations
enum ShippingMethod {
STANDARD(5.99, 5, "5-7 business days"),
EXPRESS(12.99, 2, "2-3 business days"),
OVERNIGHT(24.99, 1, "Next business day"),
INTERNATIONAL(35.00, 10, "7-14 business days");
private final double baseCost;
private final int daysMin;
private final String description;
ShippingMethod(double baseCost, int daysMin, String description) {
this.baseCost = baseCost;
this.daysMin = daysMin;
this.description = description;
}
// Calculate cost with weight
public double calculateCost(double weightPounds) {
if (this == INTERNATIONAL) {
return baseCost + (weightPounds * 2.50);
} else if (this == OVERNIGHT) {
return baseCost + (weightPounds * 1.50);
} else {
return baseCost + (weightPounds * 0.50);
}
}
// Check if available for weight
public boolean isAvailableFor(double weightPounds) {
if (this == OVERNIGHT && weightPounds > 50) {
return false; // Overnight has weight limit
}
return true;
}
public double getBaseCost() { return baseCost; }
public String getDescription() { return description; }
public int getDaysMin() { return daysMin; }
}
public class ComplexEnum {
public static void main(String[] args) {
// Calculate shipping for different weights
double packageWeight = 10.0;
System.out.println("Package weight: " + packageWeight + " lbs\n");
System.out.println("Shipping options:");
for (ShippingMethod method : ShippingMethod.values()) {
if (method.isAvailableFor(packageWeight)) {
double cost = method.calculateCost(packageWeight);
System.out.printf("%s: $%.2f (%s)%n",
method, cost, method.getDescription());
} else {
System.out.printf("%s: Not available for this weight%n", method);
}
}
// Find cheapest option
double weight = 15.0;
ShippingMethod cheapest = ShippingMethod.STANDARD;
double lowestCost = cheapest.calculateCost(weight);
for (ShippingMethod method : ShippingMethod.values()) {
if (method.isAvailableFor(weight)) {
double cost = method.calculateCost(weight);
if (cost < lowestCost) {
cheapest = method;
lowestCost = cost;
}
}
}
System.out.printf("\nFor %.1f lbs, cheapest: %s ($%.2f)%n",
weight, cheapest, lowestCost);
// Find fastest under budget
double budget = 15.00;
ShippingMethod fastest = null;
for (ShippingMethod method : ShippingMethod.values()) {
double cost = method.calculateCost(weight);
if (cost <= budget) {
if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {
fastest = method;
}
}
}
if (fastest != null) {
System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",
budget, fastest,
(double)fastest.getDaysMin(),
fastest.calculateCost(weight));
}
}
}
packageWeight ← 10.0
44public class ComplexEnum {45 public static void main(String[] args) {46 // Calculate shipping for different weights47 double packageWeight→ 10.0 = 10.0;48 //@packageWeight=10.0, 20.0, 60.049 50 System.out.println("Package weight: " + packageWeight10.0 + " lbs\n");51 System.out.println("Shipping options:");outputPackage weight: 10.0 lbs Shipping options:this.baseCost ← 5.99, this.daysMin ← 5, this.description ← 5-7 business days
pass 1 of 414ShippingMethod(double baseCost5.99, int daysMin5, String description5-7 business days) {15 this.baseCost→ 5.99 = baseCost5.99;16 this.daysMin→ 5 = daysMin5;17 this.description→ 5-7 business days = description5-7 business days;18}All 4 passes — pass 1 is the card above pass baseCostdaysMindescriptionthis.baseCostthis.daysMinthis.description1 5.99 5 5-7 business days 5.99 5 5-7 business days 2 12.99 2 2-3 business days 12.99 2 2-3 business days 3 24.99 1 Next business day 24.99 1 Next business day 4 35.0 10 7-14 business days 35.0 10 7-14 business days for (ShippingMethod method : ShippingMethod.values())
pass 1 of 453for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {54 if (method.isAvailableFor(packageWeight)) {All 4 passes — pass 1 is the card above pass method1 STANDARD 2 EXPRESS 3 OVERNIGHT 4 INTERNATIONAL public boolean isAvailableFor(double weightPounds)
pass 1 of 831// Check if available for weight32public boolean isAvailableFor(double weightPounds10.0) {33 if (this == OVERNIGHT && weightPounds > 50) {34 return false; // Overnight has weight limit35 }36 return true;37}All 8 passes — pass 1 is the card above pass weightPounds1 10.0 2 10.0 3 10.0 4 10.0 5 5.0 6 5.0 7 5.0 8 5.0 if (method.isAvailableFor(packageWeight))
pass 1 of 453for (ShippingMethod method : ShippingMethod.values()) {54 if (method.isAvailableFor(packageWeight10.0)) {55 double cost = method.calculateCost(packageWeight10.0);56 System.out.printf("%s: $%.2f (%s)%n",public double calculateCost(double weightPounds)
pass 1 of 1420// Calculate cost with weight21public double calculateCost(double weightPounds10.0) {22 if (this == INTERNATIONAL) {14 passes — pass 1 is the card above pass weightPounds1 10.0 2 10.0 3 10.0 4 10.0 5 5.0 6 5.0 7 5.0 8 5.0 9 5.0 ⋯ 3 more passes ⋯ 13 5.0 14 5.0 else
pass 1 of 825 return baseCost + (weightPounds * 1.50);26} else {27 return baseCost5.99 + (weightPounds10.0 * 0.50);28}All 8 passes — pass 1 is the card above pass baseCostweightPounds1 5.99 10.0 2 12.99 10.0 3 5.99 5.0 4 5.99 5.0 5 12.99 5.0 6 5.99 5.0 7 12.99 5.0 8 5.99 5.0 cost ← 10.99
54if (method.isAvailableFor(packageWeight)) {55 double cost→ 10.99 = method.calculateCost(packageWeight10.0);56 System.out.printf("%s: $%.2f (%s)%n",57 methodSTANDARD, cost10.99, method.getDescription());58} else {public String getDescription()
pass 1 of 439public double getBaseCost() { return baseCost; }40public String getDescription() { return description5-7 business days; }41public int getDaysMin() { return daysMin; }All 4 passes — pass 1 is the card above pass description1 5-7 business days 2 2-3 business days 3 Next business day 4 7-14 business days method, cost, method.getDescription());
55 double cost = method.calculateCost(packageWeight);56 System.out.printf("%s: $%.2f (%s)%n",57 methodSTANDARD, cost10.99, method.getDescription());58} else {cost ← 17.990000000000002
54if (method.isAvailableFor(packageWeight)) {55 double cost→ 17.990000000000002 = method.calculateCost(packageWeight10.0);56 System.out.printf("%s: $%.2f (%s)%n",57 methodEXPRESS, cost17.990000000000002, method.getDescription());58} else {method, cost, method.getDescription());
55 double cost = method.calculateCost(packageWeight);56 System.out.printf("%s: $%.2f (%s)%n",57 methodEXPRESS, cost17.990000000000002, method.getDescription());58} else {if (this == OVERNIGHT)
pass 1 of 323 return baseCost + (weightPounds * 2.50);24} else if (this == OVERNIGHT) {25 return baseCost24.99 + (weightPounds10.0 * 1.50);26} else {All 3 passes — pass 1 is the card above pass weightPounds1 10.0 2 5.0 3 5.0 cost ← 39.989999999999995
54if (method.isAvailableFor(packageWeight)) {55 double cost→ 39.989999999999995 = method.calculateCost(packageWeight10.0);56 System.out.printf("%s: $%.2f (%s)%n",57 methodOVERNIGHT, cost39.989999999999995, method.getDescription());58} else {method, cost, method.getDescription());
55 double cost = method.calculateCost(packageWeight);56 System.out.printf("%s: $%.2f (%s)%n",57 methodOVERNIGHT, cost39.989999999999995, method.getDescription());58} else {if (this == INTERNATIONAL)
pass 1 of 321public double calculateCost(double weightPounds) {22 if (this == INTERNATIONAL) {23 return baseCost35.0 + (weightPounds10.0 * 2.50);24 } else if (this == OVERNIGHT) {All 3 passes — pass 1 is the card above pass weightPounds1 10.0 2 5.0 3 5.0 cost ← 60.0
54if (method.isAvailableFor(packageWeight)) {55 double cost→ 60.0 = method.calculateCost(packageWeight10.0);56 System.out.printf("%s: $%.2f (%s)%n",57 methodINTERNATIONAL, cost60.0, method.getDescription());58} else {method, cost, method.getDescription());
55 double cost = method.calculateCost(packageWeight);56 System.out.printf("%s: $%.2f (%s)%n",57 methodINTERNATIONAL, cost60.0, method.getDescription());58} else {weight ← 5.0, cheapest ← STANDARD
69// Find cheapest option70double weight→ 5.0 = 5.0;71//@weight=5.0, 15.072ShippingMethod cheapest→ STANDARD = ShippingMethod.STANDARD;73double lowestCost = cheapest.calculateCost(weight5.0);lowestCost ← 8.49
72ShippingMethod cheapest = ShippingMethod.STANDARD;73double lowestCost→ 8.49 = cheapest.calculateCost(weight5.0);for (ShippingMethod method : ShippingMethod.values())
pass 1 of 475for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {76 if (method.isAvailableFor(weight)) {All 4 passes — pass 1 is the card above pass method1 STANDARD 2 EXPRESS 3 OVERNIGHT 4 INTERNATIONAL if (method.isAvailableFor(weight))
pass 1 of 475for (ShippingMethod method : ShippingMethod.values()) {76 if (method.isAvailableFor(weight5.0)) {77 double cost = method.calculateCost(weight5.0);78 if (cost < lowestCost) {cost ← 8.49
76if (method.isAvailableFor(weight)) {77 double cost→ 8.49 = method.calculateCost(weight5.0);78 if (cost < lowestCost) {cost ← 15.49
76if (method.isAvailableFor(weight)) {77 double cost→ 15.49 = method.calculateCost(weight5.0);78 if (cost < lowestCost) {cost ← 32.489999999999995
76if (method.isAvailableFor(weight)) {77 double cost→ 32.489999999999995 = method.calculateCost(weight5.0);78 if (cost < lowestCost) {cost ← 47.5
76if (method.isAvailableFor(weight)) {77 double cost→ 47.5 = method.calculateCost(weight5.0);78 if (cost < lowestCost) {budget ← 15.0, fastest ← null
85System.out.printf("\nFor %.1f lbs, cheapest: %s ($%.2f)%n",86 weight5.0, cheapestSTANDARD, lowestCost8.49);8788// Find fastest under budget89double budget→ 15.0 = 15.00;90ShippingMethod fastest→ null = null;for (ShippingMethod method : ShippingMethod.values())
pass 1 of 492for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {93 double cost = method.calculateCost(weight5.0);94 if (cost <= budget) {All 4 passes — pass 1 is the card above pass method1 STANDARD 2 EXPRESS 3 OVERNIGHT 4 INTERNATIONAL cost ← 8.49
92for (ShippingMethod method : ShippingMethod.values()) {93 double cost→ 8.49 = method.calculateCost(weight5.0);94 if (cost <= budget) {if (cost <= budget)
93double cost = method.calculateCost(weight);94if (cost8.49 <= budget15.0) {95 if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {fastest ← STANDARD
94if (cost <= budget) {95 if (fastestnull == null || method.getDaysMin() < fastest.getDaysMin()) {96 fastest→ STANDARD = methodSTANDARD;97 }cost ← 15.49
92for (ShippingMethod method : ShippingMethod.values()) {93 double cost→ 15.49 = method.calculateCost(weight5.0);94 if (cost <= budget) {cost ← 32.489999999999995
92for (ShippingMethod method : ShippingMethod.values()) {93 double cost→ 32.489999999999995 = method.calculateCost(weight5.0);94 if (cost <= budget) {cost ← 47.5
92for (ShippingMethod method : ShippingMethod.values()) {93 double cost→ 47.5 = method.calculateCost(weight5.0);94 if (cost <= budget) {if (fastest != null)
101if (fastestSTANDARD != null) {102 System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",103 budget15.0, fastestSTANDARD, 104 (double)fastest.getDaysMin(),105 fastest.calculateCost(weight5.0));106}public int getDaysMin()
40 public String getDescription() { return description; }41 public int getDaysMin() { return daysMin5; }42}budget, fastest,
101if (fastest != null) {102 System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",103 budget15.0, fastestSTANDARD, 104 (double)fastest.getDaysMin(),105 fastest.calculateCost(weight5.0));106}
packageWeight ← 20.0
44public class ComplexEnum {45 public static void main(String[] args) {46 // Calculate shipping for different weights47 double packageWeight→ 20.0 = 20.0;48 49 System.out.println("Package weight: " + packageWeight20.0 + " lbs\n");50 System.out.println("Shipping options:");outputPackage weight: 20.0 lbs Shipping options:this.baseCost ← 5.99, this.daysMin ← 5, this.description ← 5-7 business days
pass 1 of 414ShippingMethod(double baseCost5.99, int daysMin5, String description5-7 business days) {15 this.baseCost→ 5.99 = baseCost5.99;16 this.daysMin→ 5 = daysMin5;17 this.description→ 5-7 business days = description5-7 business days;18}All 4 passes — pass 1 is the card above pass baseCostdaysMindescriptionthis.baseCostthis.daysMinthis.description1 5.99 5 5-7 business days 5.99 5 5-7 business days 2 12.99 2 2-3 business days 12.99 2 2-3 business days 3 24.99 1 Next business day 24.99 1 Next business day 4 35.0 10 7-14 business days 35.0 10 7-14 business days for (ShippingMethod method : ShippingMethod.values())
pass 1 of 452for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {53 if (method.isAvailableFor(packageWeight)) {All 4 passes — pass 1 is the card above pass method1 STANDARD 2 EXPRESS 3 OVERNIGHT 4 INTERNATIONAL public boolean isAvailableFor(double weightPounds)
pass 1 of 831// Check if available for weight32public boolean isAvailableFor(double weightPounds20.0) {33 if (this == OVERNIGHT && weightPounds > 50) {34 return false; // Overnight has weight limit35 }36 return true;37}All 8 passes — pass 1 is the card above pass weightPounds1 20.0 2 20.0 3 20.0 4 20.0 5 5.0 6 5.0 7 5.0 8 5.0 if (method.isAvailableFor(packageWeight))
pass 1 of 452for (ShippingMethod method : ShippingMethod.values()) {53 if (method.isAvailableFor(packageWeight20.0)) {54 double cost = method.calculateCost(packageWeight20.0);55 System.out.printf("%s: $%.2f (%s)%n",public double calculateCost(double weightPounds)
pass 1 of 1420// Calculate cost with weight21public double calculateCost(double weightPounds20.0) {22 if (this == INTERNATIONAL) {14 passes — pass 1 is the card above pass weightPounds1 20.0 2 20.0 3 20.0 4 20.0 5 5.0 6 5.0 7 5.0 8 5.0 9 5.0 ⋯ 3 more passes ⋯ 13 5.0 14 5.0 else
pass 1 of 825 return baseCost + (weightPounds * 1.50);26} else {27 return baseCost5.99 + (weightPounds20.0 * 0.50);28}All 8 passes — pass 1 is the card above pass baseCostweightPounds1 5.99 20.0 2 12.99 20.0 3 5.99 5.0 4 5.99 5.0 5 12.99 5.0 6 5.99 5.0 7 12.99 5.0 8 5.99 5.0 cost ← 15.99
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 15.99 = method.calculateCost(packageWeight20.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodSTANDARD, cost15.99, method.getDescription());57} else {public String getDescription()
pass 1 of 439public double getBaseCost() { return baseCost; }40public String getDescription() { return description5-7 business days; }41public int getDaysMin() { return daysMin; }All 4 passes — pass 1 is the card above pass description1 5-7 business days 2 2-3 business days 3 Next business day 4 7-14 business days method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodSTANDARD, cost15.99, method.getDescription());57} else {cost ← 22.990000000000002
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 22.990000000000002 = method.calculateCost(packageWeight20.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodEXPRESS, cost22.990000000000002, method.getDescription());57} else {method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodEXPRESS, cost22.990000000000002, method.getDescription());57} else {if (this == OVERNIGHT)
pass 1 of 323 return baseCost + (weightPounds * 2.50);24} else if (this == OVERNIGHT) {25 return baseCost24.99 + (weightPounds20.0 * 1.50);26} else {All 3 passes — pass 1 is the card above pass weightPounds1 20.0 2 5.0 3 5.0 cost ← 54.989999999999995
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 54.989999999999995 = method.calculateCost(packageWeight20.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodOVERNIGHT, cost54.989999999999995, method.getDescription());57} else {method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodOVERNIGHT, cost54.989999999999995, method.getDescription());57} else {if (this == INTERNATIONAL)
pass 1 of 321public double calculateCost(double weightPounds) {22 if (this == INTERNATIONAL) {23 return baseCost35.0 + (weightPounds20.0 * 2.50);24 } else if (this == OVERNIGHT) {All 3 passes — pass 1 is the card above pass weightPounds1 20.0 2 5.0 3 5.0 cost ← 85.0
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 85.0 = method.calculateCost(packageWeight20.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodINTERNATIONAL, cost85.0, method.getDescription());57} else {method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodINTERNATIONAL, cost85.0, method.getDescription());57} else {weight ← 5.0, cheapest ← STANDARD
63// Find cheapest option64double weight→ 5.0 = 5.0;65ShippingMethod cheapest→ STANDARD = ShippingMethod.STANDARD;66double lowestCost = cheapest.calculateCost(weight5.0);lowestCost ← 8.49
65ShippingMethod cheapest = ShippingMethod.STANDARD;66double lowestCost→ 8.49 = cheapest.calculateCost(weight5.0);for (ShippingMethod method : ShippingMethod.values())
pass 1 of 468for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {69 if (method.isAvailableFor(weight)) {All 4 passes — pass 1 is the card above pass method1 STANDARD 2 EXPRESS 3 OVERNIGHT 4 INTERNATIONAL if (method.isAvailableFor(weight))
pass 1 of 468for (ShippingMethod method : ShippingMethod.values()) {69 if (method.isAvailableFor(weight5.0)) {70 double cost = method.calculateCost(weight5.0);71 if (cost < lowestCost) {cost ← 8.49
69if (method.isAvailableFor(weight)) {70 double cost→ 8.49 = method.calculateCost(weight5.0);71 if (cost < lowestCost) {cost ← 15.49
69if (method.isAvailableFor(weight)) {70 double cost→ 15.49 = method.calculateCost(weight5.0);71 if (cost < lowestCost) {cost ← 32.489999999999995
69if (method.isAvailableFor(weight)) {70 double cost→ 32.489999999999995 = method.calculateCost(weight5.0);71 if (cost < lowestCost) {cost ← 47.5
69if (method.isAvailableFor(weight)) {70 double cost→ 47.5 = method.calculateCost(weight5.0);71 if (cost < lowestCost) {budget ← 15.0, fastest ← null
78System.out.printf("\nFor %.1f lbs, cheapest: %s ($%.2f)%n",79 weight5.0, cheapestSTANDARD, lowestCost8.49);8081// Find fastest under budget82double budget→ 15.0 = 15.00;83ShippingMethod fastest→ null = null;for (ShippingMethod method : ShippingMethod.values())
pass 1 of 485for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {86 double cost = method.calculateCost(weight5.0);87 if (cost <= budget) {All 4 passes — pass 1 is the card above pass method1 STANDARD 2 EXPRESS 3 OVERNIGHT 4 INTERNATIONAL cost ← 8.49
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 8.49 = method.calculateCost(weight5.0);87 if (cost <= budget) {if (cost <= budget)
86double cost = method.calculateCost(weight);87if (cost8.49 <= budget15.0) {88 if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {fastest ← STANDARD
87if (cost <= budget) {88 if (fastestnull == null || method.getDaysMin() < fastest.getDaysMin()) {89 fastest→ STANDARD = methodSTANDARD;90 }cost ← 15.49
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 15.49 = method.calculateCost(weight5.0);87 if (cost <= budget) {cost ← 32.489999999999995
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 32.489999999999995 = method.calculateCost(weight5.0);87 if (cost <= budget) {cost ← 47.5
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 47.5 = method.calculateCost(weight5.0);87 if (cost <= budget) {if (fastest != null)
94if (fastestSTANDARD != null) {95 System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",96 budget15.0, fastestSTANDARD, 97 (double)fastest.getDaysMin(),98 fastest.calculateCost(weight5.0));99}public int getDaysMin()
40 public String getDescription() { return description; }41 public int getDaysMin() { return daysMin5; }42}budget, fastest,
94if (fastest != null) {95 System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",96 budget15.0, fastestSTANDARD, 97 (double)fastest.getDaysMin(),98 fastest.calculateCost(weight5.0));99}
packageWeight ← 60.0
44public class ComplexEnum {45 public static void main(String[] args) {46 // Calculate shipping for different weights47 double packageWeight→ 60.0 = 60.0;48 49 System.out.println("Package weight: " + packageWeight60.0 + " lbs\n");50 System.out.println("Shipping options:");outputPackage weight: 60.0 lbs Shipping options:this.baseCost ← 5.99, this.daysMin ← 5, this.description ← 5-7 business days
pass 1 of 414ShippingMethod(double baseCost5.99, int daysMin5, String description5-7 business days) {15 this.baseCost→ 5.99 = baseCost5.99;16 this.daysMin→ 5 = daysMin5;17 this.description→ 5-7 business days = description5-7 business days;18}All 4 passes — pass 1 is the card above pass baseCostdaysMindescriptionthis.baseCostthis.daysMinthis.description1 5.99 5 5-7 business days 5.99 5 5-7 business days 2 12.99 2 2-3 business days 12.99 2 2-3 business days 3 24.99 1 Next business day 24.99 1 Next business day 4 35.0 10 7-14 business days 35.0 10 7-14 business days for (ShippingMethod method : ShippingMethod.values())
pass 1 of 452for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {53 if (method.isAvailableFor(packageWeight)) {All 4 passes — pass 1 is the card above pass methodweightPounds1 STANDARD — 2 EXPRESS — 3 OVERNIGHT 60.0 4 INTERNATIONAL — public boolean isAvailableFor(double weightPounds)
pass 1 of 831// Check if available for weight32public boolean isAvailableFor(double weightPounds60.0) {33 if (this == OVERNIGHT && weightPounds > 50) {34 return false; // Overnight has weight limit35 }36 return true;37}All 8 passes — pass 1 is the card above pass weightPoundsmethodbaseCost1 60.0 — — 2 60.0 — — 3 60.0 OVERNIGHT — 4 60.0 — — 5 5.0 — — 6 5.0 — — 7 5.0 — 24.99 8 5.0 — — if (method.isAvailableFor(packageWeight))
pass 1 of 352for (ShippingMethod method : ShippingMethod.values()) {53 if (method.isAvailableFor(packageWeight60.0)) {54 double cost = method.calculateCost(packageWeight60.0);55 System.out.printf("%s: $%.2f (%s)%n",public double calculateCost(double weightPounds)
pass 1 of 1320// Calculate cost with weight21public double calculateCost(double weightPounds60.0) {22 if (this == INTERNATIONAL) {13 passes — pass 1 is the card above pass weightPoundsbaseCost1 60.0 — 2 60.0 — 3 60.0 — 4 5.0 — 5 5.0 — 6 5.0 — 7 5.0 24.99 8 5.0 — 9 5.0 — ⋯ 2 more passes ⋯ 12 5.0 — 13 5.0 — else
pass 1 of 825 return baseCost + (weightPounds * 1.50);26} else {27 return baseCost5.99 + (weightPounds60.0 * 0.50);28}All 8 passes — pass 1 is the card above pass baseCostweightPounds1 5.99 60.0 2 12.99 60.0 3 5.99 5.0 4 5.99 5.0 5 12.99 5.0 6 5.99 5.0 7 12.99 5.0 8 5.99 5.0 cost ← 35.99
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 35.99 = method.calculateCost(packageWeight60.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodSTANDARD, cost35.99, method.getDescription());57} else {public String getDescription()
pass 1 of 339public double getBaseCost() { return baseCost; }40public String getDescription() { return description5-7 business days; }41public int getDaysMin() { return daysMin; }All 3 passes — pass 1 is the card above pass description1 5-7 business days 2 2-3 business days 3 7-14 business days method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodSTANDARD, cost35.99, method.getDescription());57} else {cost ← 42.99
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 42.99 = method.calculateCost(packageWeight60.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodEXPRESS, cost42.99, method.getDescription());57} else {method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodEXPRESS, cost42.99, method.getDescription());57} else {if (this == OVERNIGHT && weightPounds > 50)
32public boolean isAvailableFor(double weightPounds) {33 if (this == OVERNIGHT && weightPounds60.0 > 50) {34 return false; // Overnight has weight limit35 }else
56 method, cost, method.getDescription());57} else {58 System.out.printf("%s: Not available for this weight%n", methodOVERNIGHT);59}if (this == INTERNATIONAL)
pass 1 of 321public double calculateCost(double weightPounds) {22 if (this == INTERNATIONAL) {23 return baseCost35.0 + (weightPounds60.0 * 2.50);24 } else if (this == OVERNIGHT) {All 3 passes — pass 1 is the card above pass weightPounds1 60.0 2 5.0 3 5.0 cost ← 185.0
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 185.0 = method.calculateCost(packageWeight60.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodINTERNATIONAL, cost185.0, method.getDescription());57} else {method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodINTERNATIONAL, cost185.0, method.getDescription());57} else {weight ← 5.0, cheapest ← STANDARD
63// Find cheapest option64double weight→ 5.0 = 5.0;65ShippingMethod cheapest→ STANDARD = ShippingMethod.STANDARD;66double lowestCost = cheapest.calculateCost(weight5.0);lowestCost ← 8.49
65ShippingMethod cheapest = ShippingMethod.STANDARD;66double lowestCost→ 8.49 = cheapest.calculateCost(weight5.0);for (ShippingMethod method : ShippingMethod.values())
pass 1 of 468for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {69 if (method.isAvailableFor(weight)) {All 4 passes — pass 1 is the card above pass methodbaseCostweightPounds1 STANDARD — — 2 EXPRESS — — 3 OVERNIGHT 24.99 5.0 4 INTERNATIONAL — — if (method.isAvailableFor(weight))
pass 1 of 468for (ShippingMethod method : ShippingMethod.values()) {69 if (method.isAvailableFor(weight5.0)) {70 double cost = method.calculateCost(weight5.0);71 if (cost < lowestCost) {All 4 passes — pass 1 is the card above pass baseCostweightPounds1 — — 2 — — 3 24.99 5.0 4 — — cost ← 8.49
69if (method.isAvailableFor(weight)) {70 double cost→ 8.49 = method.calculateCost(weight5.0);71 if (cost < lowestCost) {cost ← 15.49
69if (method.isAvailableFor(weight)) {70 double cost→ 15.49 = method.calculateCost(weight5.0);71 if (cost < lowestCost) {if (this == OVERNIGHT)
pass 1 of 223 return baseCost + (weightPounds * 2.50);24} else if (this == OVERNIGHT) {25 return baseCost24.99 + (weightPounds5.0 * 1.50);26} else {cost ← 32.489999999999995
69if (method.isAvailableFor(weight)) {70 double cost→ 32.489999999999995 = method.calculateCost(weight5.0);71 if (cost < lowestCost) {cost ← 47.5
69if (method.isAvailableFor(weight)) {70 double cost→ 47.5 = method.calculateCost(weight5.0);71 if (cost < lowestCost) {budget ← 15.0, fastest ← null
78System.out.printf("\nFor %.1f lbs, cheapest: %s ($%.2f)%n",79 weight5.0, cheapestSTANDARD, lowestCost8.49);8081// Find fastest under budget82double budget→ 15.0 = 15.00;83ShippingMethod fastest→ null = null;for (ShippingMethod method : ShippingMethod.values())
pass 1 of 485for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {86 double cost = method.calculateCost(weight5.0);87 if (cost <= budget) {All 4 passes — pass 1 is the card above pass methodbaseCostweightPounds1 STANDARD — — 2 EXPRESS — — 3 OVERNIGHT 24.99 5.0 4 INTERNATIONAL — — cost ← 8.49
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 8.49 = method.calculateCost(weight5.0);87 if (cost <= budget) {if (cost <= budget)
86double cost = method.calculateCost(weight);87if (cost8.49 <= budget15.0) {88 if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {fastest ← STANDARD
87if (cost <= budget) {88 if (fastestnull == null || method.getDaysMin() < fastest.getDaysMin()) {89 fastest→ STANDARD = methodSTANDARD;90 }cost ← 15.49
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 15.49 = method.calculateCost(weight5.0);87 if (cost <= budget) {if (this == OVERNIGHT)
pass 2 of 223 return baseCost + (weightPounds * 2.50);24} else if (this == OVERNIGHT) {25 return baseCost24.99 + (weightPounds5.0 * 1.50);26} else {cost ← 32.489999999999995
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 32.489999999999995 = method.calculateCost(weight5.0);87 if (cost <= budget) {cost ← 47.5
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 47.5 = method.calculateCost(weight5.0);87 if (cost <= budget) {if (fastest != null)
94if (fastestSTANDARD != null) {95 System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",96 budget15.0, fastestSTANDARD, 97 (double)fastest.getDaysMin(),98 fastest.calculateCost(weight5.0));99}public int getDaysMin()
40 public String getDescription() { return description; }41 public int getDaysMin() { return daysMin5; }42}budget, fastest,
94if (fastest != null) {95 System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",96 budget15.0, fastestSTANDARD, 97 (double)fastest.getDaysMin(),98 fastest.calculateCost(weight5.0));99}
packageWeight ← 10.0
44public class ComplexEnum {45 public static void main(String[] args) {46 // Calculate shipping for different weights47 double packageWeight→ 10.0 = 10.0;48 49 System.out.println("Package weight: " + packageWeight10.0 + " lbs\n");50 System.out.println("Shipping options:");outputPackage weight: 10.0 lbs Shipping options:this.baseCost ← 5.99, this.daysMin ← 5, this.description ← 5-7 business days
pass 1 of 414ShippingMethod(double baseCost5.99, int daysMin5, String description5-7 business days) {15 this.baseCost→ 5.99 = baseCost5.99;16 this.daysMin→ 5 = daysMin5;17 this.description→ 5-7 business days = description5-7 business days;18}All 4 passes — pass 1 is the card above pass baseCostdaysMindescriptionthis.baseCostthis.daysMinthis.description1 5.99 5 5-7 business days 5.99 5 5-7 business days 2 12.99 2 2-3 business days 12.99 2 2-3 business days 3 24.99 1 Next business day 24.99 1 Next business day 4 35.0 10 7-14 business days 35.0 10 7-14 business days for (ShippingMethod method : ShippingMethod.values())
pass 1 of 452for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {53 if (method.isAvailableFor(packageWeight)) {All 4 passes — pass 1 is the card above pass method1 STANDARD 2 EXPRESS 3 OVERNIGHT 4 INTERNATIONAL public boolean isAvailableFor(double weightPounds)
pass 1 of 831// Check if available for weight32public boolean isAvailableFor(double weightPounds10.0) {33 if (this == OVERNIGHT && weightPounds > 50) {34 return false; // Overnight has weight limit35 }36 return true;37}All 8 passes — pass 1 is the card above pass weightPounds1 10.0 2 10.0 3 10.0 4 10.0 5 15.0 6 15.0 7 15.0 8 15.0 if (method.isAvailableFor(packageWeight))
pass 1 of 452for (ShippingMethod method : ShippingMethod.values()) {53 if (method.isAvailableFor(packageWeight10.0)) {54 double cost = method.calculateCost(packageWeight10.0);55 System.out.printf("%s: $%.2f (%s)%n",public double calculateCost(double weightPounds)
pass 1 of 1420// Calculate cost with weight21public double calculateCost(double weightPounds10.0) {22 if (this == INTERNATIONAL) {14 passes — pass 1 is the card above pass weightPounds1 10.0 2 10.0 3 10.0 4 10.0 5 15.0 6 15.0 7 15.0 8 15.0 9 15.0 ⋯ 3 more passes ⋯ 13 15.0 14 15.0 else
pass 1 of 825 return baseCost + (weightPounds * 1.50);26} else {27 return baseCost5.99 + (weightPounds10.0 * 0.50);28}All 8 passes — pass 1 is the card above pass baseCostweightPounds1 5.99 10.0 2 12.99 10.0 3 5.99 15.0 4 5.99 15.0 5 12.99 15.0 6 5.99 15.0 7 12.99 15.0 8 5.99 15.0 cost ← 10.99
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 10.99 = method.calculateCost(packageWeight10.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodSTANDARD, cost10.99, method.getDescription());57} else {public String getDescription()
pass 1 of 439public double getBaseCost() { return baseCost; }40public String getDescription() { return description5-7 business days; }41public int getDaysMin() { return daysMin; }All 4 passes — pass 1 is the card above pass description1 5-7 business days 2 2-3 business days 3 Next business day 4 7-14 business days method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodSTANDARD, cost10.99, method.getDescription());57} else {cost ← 17.990000000000002
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 17.990000000000002 = method.calculateCost(packageWeight10.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodEXPRESS, cost17.990000000000002, method.getDescription());57} else {method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodEXPRESS, cost17.990000000000002, method.getDescription());57} else {if (this == OVERNIGHT)
pass 1 of 323 return baseCost + (weightPounds * 2.50);24} else if (this == OVERNIGHT) {25 return baseCost24.99 + (weightPounds10.0 * 1.50);26} else {All 3 passes — pass 1 is the card above pass weightPounds1 10.0 2 15.0 3 15.0 cost ← 39.989999999999995
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 39.989999999999995 = method.calculateCost(packageWeight10.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodOVERNIGHT, cost39.989999999999995, method.getDescription());57} else {method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodOVERNIGHT, cost39.989999999999995, method.getDescription());57} else {if (this == INTERNATIONAL)
pass 1 of 321public double calculateCost(double weightPounds) {22 if (this == INTERNATIONAL) {23 return baseCost35.0 + (weightPounds10.0 * 2.50);24 } else if (this == OVERNIGHT) {All 3 passes — pass 1 is the card above pass weightPounds1 10.0 2 15.0 3 15.0 cost ← 60.0
53if (method.isAvailableFor(packageWeight)) {54 double cost→ 60.0 = method.calculateCost(packageWeight10.0);55 System.out.printf("%s: $%.2f (%s)%n",56 methodINTERNATIONAL, cost60.0, method.getDescription());57} else {method, cost, method.getDescription());
54 double cost = method.calculateCost(packageWeight);55 System.out.printf("%s: $%.2f (%s)%n",56 methodINTERNATIONAL, cost60.0, method.getDescription());57} else {weight ← 15.0, cheapest ← STANDARD
63// Find cheapest option64double weight→ 15.0 = 15.0;65ShippingMethod cheapest→ STANDARD = ShippingMethod.STANDARD;66double lowestCost = cheapest.calculateCost(weight15.0);lowestCost ← 13.49
65ShippingMethod cheapest = ShippingMethod.STANDARD;66double lowestCost→ 13.49 = cheapest.calculateCost(weight15.0);for (ShippingMethod method : ShippingMethod.values())
pass 1 of 468for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {69 if (method.isAvailableFor(weight)) {All 4 passes — pass 1 is the card above pass method1 STANDARD 2 EXPRESS 3 OVERNIGHT 4 INTERNATIONAL if (method.isAvailableFor(weight))
pass 1 of 468for (ShippingMethod method : ShippingMethod.values()) {69 if (method.isAvailableFor(weight15.0)) {70 double cost = method.calculateCost(weight15.0);71 if (cost < lowestCost) {cost ← 13.49
69if (method.isAvailableFor(weight)) {70 double cost→ 13.49 = method.calculateCost(weight15.0);71 if (cost < lowestCost) {cost ← 20.490000000000002
69if (method.isAvailableFor(weight)) {70 double cost→ 20.490000000000002 = method.calculateCost(weight15.0);71 if (cost < lowestCost) {cost ← 47.489999999999995
69if (method.isAvailableFor(weight)) {70 double cost→ 47.489999999999995 = method.calculateCost(weight15.0);71 if (cost < lowestCost) {cost ← 72.5
69if (method.isAvailableFor(weight)) {70 double cost→ 72.5 = method.calculateCost(weight15.0);71 if (cost < lowestCost) {budget ← 15.0, fastest ← null
78System.out.printf("\nFor %.1f lbs, cheapest: %s ($%.2f)%n",79 weight15.0, cheapestSTANDARD, lowestCost13.49);8081// Find fastest under budget82double budget→ 15.0 = 15.00;83ShippingMethod fastest→ null = null;for (ShippingMethod method : ShippingMethod.values())
pass 1 of 485for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {86 double cost = method.calculateCost(weight15.0);87 if (cost <= budget) {All 4 passes — pass 1 is the card above pass method1 STANDARD 2 EXPRESS 3 OVERNIGHT 4 INTERNATIONAL cost ← 13.49
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 13.49 = method.calculateCost(weight15.0);87 if (cost <= budget) {if (cost <= budget)
86double cost = method.calculateCost(weight);87if (cost13.49 <= budget15.0) {88 if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {fastest ← STANDARD
87if (cost <= budget) {88 if (fastestnull == null || method.getDaysMin() < fastest.getDaysMin()) {89 fastest→ STANDARD = methodSTANDARD;90 }cost ← 20.490000000000002
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 20.490000000000002 = method.calculateCost(weight15.0);87 if (cost <= budget) {cost ← 47.489999999999995
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 47.489999999999995 = method.calculateCost(weight15.0);87 if (cost <= budget) {cost ← 72.5
85for (ShippingMethod method : ShippingMethod.values()) {86 double cost→ 72.5 = method.calculateCost(weight15.0);87 if (cost <= budget) {if (fastest != null)
94if (fastestSTANDARD != null) {95 System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",96 budget15.0, fastestSTANDARD, 97 (double)fastest.getDaysMin(),98 fastest.calculateCost(weight15.0));99}public int getDaysMin()
40 public String getDescription() { return description; }41 public int getDaysMin() { return daysMin5; }42}budget, fastest,
94if (fastest != null) {95 System.out.printf("Budget $%.2f: %s (%.0f days, $%.2f)%n",96 budget15.0, fastestSTANDARD, 97 (double)fastest.getDaysMin(),98 fastest.calculateCost(weight15.0));99}
Combine fields, constructors, methods - enum as a full data type.
Abstract methods per constant
Each constant provides its own implementation.
// Enum with abstract methods (per-constant behavior)
// Concept: abstract method in enum
// Concept: per-constant behavior
enum Operation {
PLUS("+") {
@Override
public double apply(double x, double y) {
return x + y;
}
},
MINUS("-") {
@Override
public double apply(double x, double y) {
return x - y;
}
},
MULTIPLY("*") {
@Override
public double apply(double x, double y) {
return x * y;
}
},
DIVIDE("/") {
@Override
public double apply(double x, double y) {
return x / y;
}
};
private final String symbol;
Operation(String symbol) {
this.symbol = symbol;
}
// Abstract method - each constant must implement
public abstract double apply(double x, double y);
public String getSymbol() {
return symbol;
}
}
public class AbstractMethod {
public static void main(String[] args) {
// Use operations
double a = 10;
double b = 3;
System.out.println("Calculate: a=" + a + ", b=" + b + "\n");
for (Operation op : Operation.values()) {
double result = op.apply(a, b);
System.out.printf("%s %s %s = %.2f%n",
a, op.getSymbol(), b, result);
}
// Build calculator
Operation operator = Operation.MULTIPLY;
double x = 7;
double y = 6;
double answer = operator.apply(x, y);
System.out.printf("\n%.0f %s %.0f = %.0f%n",
x, operator.getSymbol(), y, answer);
// Evaluate expressions
class Expression {
double left;
Operation op;
double right;
Expression(double left, Operation op, double right) {
this.left = left;
this.op = op;
this.right = right;
}
double evaluate() {
return op.apply(left, right);
}
public String toString() {
return String.format("%.0f %s %.0f = %.2f",
left, op.getSymbol(), right, evaluate());
}
}
Expression[] expressions = {
new Expression(100, Operation.PLUS, 50),
new Expression(100, Operation.MINUS, 50),
new Expression(100, Operation.MULTIPLY, 50),
new Expression(100, Operation.DIVIDE, 50)
};
System.out.println("\nExpressions:");
for (Expression expr : expressions) {
System.out.println(" " + expr);
}
}
}
// Enum with abstract methods (per-constant behavior)
// Concept: abstract method in enum
// Concept: per-constant behavior
enum Operation {
PLUS("+") {
@Override
public double apply(double x, double y) {
return x + y;
}
},
MINUS("-") {
@Override
public double apply(double x, double y) {
return x - y;
}
},
MULTIPLY("*") {
@Override
public double apply(double x, double y) {
return x * y;
}
},
DIVIDE("/") {
@Override
public double apply(double x, double y) {
return x / y;
}
};
private final String symbol;
Operation(String symbol) {
this.symbol = symbol;
}
// Abstract method - each constant must implement
public abstract double apply(double x, double y);
public String getSymbol() {
return symbol;
}
}
public class AbstractMethod {
public static void main(String[] args) {
// Use operations
double a = 10;
double b = 3;
System.out.println("Calculate: a=" + a + ", b=" + b + "\n");
for (Operation op : Operation.values()) {
double result = op.apply(a, b);
System.out.printf("%s %s %s = %.2f%n",
a, op.getSymbol(), b, result);
}
// Build calculator
Operation operator = Operation.PLUS;
double x = 7;
double y = 6;
double answer = operator.apply(x, y);
System.out.printf("\n%.0f %s %.0f = %.0f%n",
x, operator.getSymbol(), y, answer);
// Evaluate expressions
class Expression {
double left;
Operation op;
double right;
Expression(double left, Operation op, double right) {
this.left = left;
this.op = op;
this.right = right;
}
double evaluate() {
return op.apply(left, right);
}
public String toString() {
return String.format("%.0f %s %.0f = %.2f",
left, op.getSymbol(), right, evaluate());
}
}
Expression[] expressions = {
new Expression(100, Operation.PLUS, 50),
new Expression(100, Operation.MINUS, 50),
new Expression(100, Operation.MULTIPLY, 50),
new Expression(100, Operation.DIVIDE, 50)
};
System.out.println("\nExpressions:");
for (Expression expr : expressions) {
System.out.println(" " + expr);
}
}
}
// Enum with abstract methods (per-constant behavior)
// Concept: abstract method in enum
// Concept: per-constant behavior
enum Operation {
PLUS("+") {
@Override
public double apply(double x, double y) {
return x + y;
}
},
MINUS("-") {
@Override
public double apply(double x, double y) {
return x - y;
}
},
MULTIPLY("*") {
@Override
public double apply(double x, double y) {
return x * y;
}
},
DIVIDE("/") {
@Override
public double apply(double x, double y) {
return x / y;
}
};
private final String symbol;
Operation(String symbol) {
this.symbol = symbol;
}
// Abstract method - each constant must implement
public abstract double apply(double x, double y);
public String getSymbol() {
return symbol;
}
}
public class AbstractMethod {
public static void main(String[] args) {
// Use operations
double a = 10;
double b = 3;
System.out.println("Calculate: a=" + a + ", b=" + b + "\n");
for (Operation op : Operation.values()) {
double result = op.apply(a, b);
System.out.printf("%s %s %s = %.2f%n",
a, op.getSymbol(), b, result);
}
// Build calculator
Operation operator = Operation.DIVIDE;
double x = 7;
double y = 6;
double answer = operator.apply(x, y);
System.out.printf("\n%.0f %s %.0f = %.0f%n",
x, operator.getSymbol(), y, answer);
// Evaluate expressions
class Expression {
double left;
Operation op;
double right;
Expression(double left, Operation op, double right) {
this.left = left;
this.op = op;
this.right = right;
}
double evaluate() {
return op.apply(left, right);
}
public String toString() {
return String.format("%.0f %s %.0f = %.2f",
left, op.getSymbol(), right, evaluate());
}
}
Expression[] expressions = {
new Expression(100, Operation.PLUS, 50),
new Expression(100, Operation.MINUS, 50),
new Expression(100, Operation.MULTIPLY, 50),
new Expression(100, Operation.DIVIDE, 50)
};
System.out.println("\nExpressions:");
for (Expression expr : expressions) {
System.out.println(" " + expr);
}
}
}
a ← 10.0, b ← 3.0
45public class AbstractMethod {46 public static void main(String[] args) {47 // Use operations48 double a→ 10.0 = 10;49 double b→ 3.0 = 3;50 51 System.out.println("Calculate: a=" + a10.0 + ", b=" + b3.0 + "\n");outputCalculate: a=10.0, b=3.0this.symbol ← +
pass 1 of 433Operation(String symbol+) {34 this.symbol→ + = symbol+;35}All 4 passes — pass 1 is the card above pass symbolthis.symbol1 + + 2 - - 3 * * 4 / / for (Operation op : Operation.values())
pass 1 of 453for (Operation opPLUS : Operation.values()) {54 double result = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",All 4 passes — pass 1 is the card above pass op1 PLUS 2 MINUS 3 MULTIPLY 4 DIVIDE @Override public double apply(double x, double y)
pass 1 of 86PLUS("+") {7 @Override8 public double apply(double x10.0, double y3.0) {9 return x10.0 + y3.0;10 }All 8 passes — pass 1 is the card above pass xy1 10.0 3.0 2 100.0 50.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 result ← 13.0
53for (Operation op : Operation.values()) {54 double result→ 13.0 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result13.0);57}public String getSymbol()
pass 1 of 3340public String getSymbol() {41 return symbol+;42}33 passes — pass 1 is the card above pass symbol1 + 2 - 3 * 4 / 5 * 6 + 7 + 8 + 9 + ⋯ 22 more passes ⋯ 32 / 33 / a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result13.0);57}@Override public double apply(double x, double y)
pass 1 of 812MINUS("-") {13 @Override14 public double apply(double x10.0, double y3.0) {15 return x10.0 - y3.0;16 }All 8 passes — pass 1 is the card above pass xy1 10.0 3.0 2 100.0 50.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 result ← 7.0
53for (Operation op : Operation.values()) {54 double result→ 7.0 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result7.0);57}a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result7.0);57}@Override public double apply(double x, double y)
pass 1 of 918MULTIPLY("*") {19 @Override20 public double apply(double x10.0, double y3.0) {21 return x10.0 * y3.0;22 }All 9 passes — pass 1 is the card above pass xy1 10.0 3.0 2 7.0 6.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 9 100.0 50.0 result ← 30.0
53for (Operation op : Operation.values()) {54 double result→ 30.0 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result30.0);57}a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result30.0);57}@Override public double apply(double x, double y)
pass 1 of 824DIVIDE("/") {25 @Override26 public double apply(double x10.0, double y3.0) {27 return x10.0 / y3.0;28 }All 8 passes — pass 1 is the card above pass xy1 10.0 3.0 2 100.0 50.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 result ← 3.3333333333333335
53for (Operation op : Operation.values()) {54 double result→ 3.3333333333333335 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result3.3333333333333335);57}a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result3.3333333333333335);57}operator ← MULTIPLY, x ← 7.0, y ← 6.0
65// Build calculator66Operation operator→ MULTIPLY = Operation.MULTIPLY;67//@operator=Operation.MULTIPLY, Operation.PLUS, Operation.DIVIDE68double x→ 7.0 = 7;69double y→ 6.0 = 6;7071double answer = operator.apply(x7.0, y6.0);72System.out.printf("\n%.0f %s %.0f = %.0f%n",answer ← 42.0
71double answer→ 42.0 = operator.apply(x7.0, y6.0);72System.out.printf("\n%.0f %s %.0f = %.0f%n",73 x7.0, operator.getSymbol(), y6.0, answer42.0);x, operator.getSymbol(), y, answer);
71double answer = operator.apply(x, y);72System.out.printf("\n%.0f %s %.0f = %.0f%n",73 x7.0, operator.getSymbol(), y6.0, answer42.0);7475// Evaluate expressions76class Expression {77 double left;78 Operation op;79 double right;80 81 Expression(double left, Operation op, double right) {82 this.left = left;83 this.op = op;84 this.right = right;85 }86 87 double evaluate() {88 return op.apply(left, right);89 }90 91 public String toString() {92 return String.format("%.0f %s %.0f = %.2f",93 left, op.getSymbol(), right, evaluate());94 }95}9697Expression[] expressions = {98 new Expression(100, Operation.PLUS, 50),99 new Expression(100, Operation.MINUS, 50),100 new Expression(100, Operation.MULTIPLY, 50),101 new Expression(100, Operation.DIVIDE, 50)102};this.left ← 100.0, this.op ← PLUS, this.right ← 50.0
pass 1 of 481Expression(double left100.0, Operation opPLUS, double right50.0) {82 this.left→ 100.0 = left100.0;83 this.op→ PLUS = opPLUS;84 this.right→ 50.0 = right50.0;85}All 4 passes — pass 1 is the card above pass opthis.leftthis.opthis.right1 PLUS 100.0 PLUS 50.0 2 MINUS 100.0 MINUS 50.0 3 MULTIPLY 100.0 MULTIPLY 50.0 4 DIVIDE 100.0 DIVIDE 50.0 Expression[] expressions =
97Expression[] expressions = {98 new Expression(100, Operation.PLUS, 50),99 new Expression(100, Operation.MINUS, 50),100 new Expression(100, Operation.MULTIPLY, 50),101 new Expression(100, Operation.DIVIDE, 50)102};103104System.out.println("\nExpressions:");105for (Expression expr : expressions) {output Expressions:double evaluate()
pass 1 of 2887double evaluate() {88 return op.apply(left100.0, right50.0);89}for (Expression expr : expressions)
pass 1 of 4104System.out.println("\nExpressions:");105for (Expression expr100 + 50 = 150.00 : expressions) {106 System.out.println(" " + expr);All 4 passes — pass 1 is the card above pass expr1 100 + 50 = 150.00 2 100 - 50 = 50.00 3 100 * 50 = 5000.00 4 100 / 50 = 2.00 System.out.println(" " + expr);
105for (Expression expr : expressions) {106 System.out.println(" " + expr100 + 50 = 150.00);107}System.out.println(" " + expr);
105for (Expression expr : expressions) {106 System.out.println(" " + expr100 + 50 = 150.00);107}output 100 + 50 = 150.00System.out.println(" " + expr);
105for (Expression expr : expressions) {106 System.out.println(" " + expr100 - 50 = 50.00);107}System.out.println(" " + expr);
105for (Expression expr : expressions) {106 System.out.println(" " + expr100 - 50 = 50.00);107}output 100 - 50 = 50.00System.out.println(" " + expr);
105for (Expression expr : expressions) {106 System.out.println(" " + expr100 * 50 = 5000.00);107}System.out.println(" " + expr);
105for (Expression expr : expressions) {106 System.out.println(" " + expr100 * 50 = 5000.00);107}output 100 * 50 = 5000.00System.out.println(" " + expr);
105for (Expression expr : expressions) {106 System.out.println(" " + expr100 / 50 = 2.00);107}System.out.println(" " + expr);
105for (Expression expr : expressions) {106 System.out.println(" " + expr100 / 50 = 2.00);107}output 100 / 50 = 2.00
a ← 10.0, b ← 3.0
45public class AbstractMethod {46 public static void main(String[] args) {47 // Use operations48 double a→ 10.0 = 10;49 double b→ 3.0 = 3;50 51 System.out.println("Calculate: a=" + a10.0 + ", b=" + b3.0 + "\n");outputCalculate: a=10.0, b=3.0this.symbol ← +
pass 1 of 433Operation(String symbol+) {34 this.symbol→ + = symbol+;35}All 4 passes — pass 1 is the card above pass symbolthis.symbol1 + + 2 - - 3 * * 4 / / for (Operation op : Operation.values())
pass 1 of 453for (Operation opPLUS : Operation.values()) {54 double result = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",All 4 passes — pass 1 is the card above pass op1 PLUS 2 MINUS 3 MULTIPLY 4 DIVIDE @Override public double apply(double x, double y)
pass 1 of 96PLUS("+") {7 @Override8 public double apply(double x10.0, double y3.0) {9 return x10.0 + y3.0;10 }All 9 passes — pass 1 is the card above pass xy1 10.0 3.0 2 7.0 6.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 9 100.0 50.0 result ← 13.0
53for (Operation op : Operation.values()) {54 double result→ 13.0 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result13.0);57}public String getSymbol()
pass 1 of 3340public String getSymbol() {41 return symbol+;42}33 passes — pass 1 is the card above pass symbol1 + 2 - 3 * 4 / 5 + 6 + 7 + 8 + 9 + ⋯ 22 more passes ⋯ 32 / 33 / a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result13.0);57}@Override public double apply(double x, double y)
pass 1 of 812MINUS("-") {13 @Override14 public double apply(double x10.0, double y3.0) {15 return x10.0 - y3.0;16 }All 8 passes — pass 1 is the card above pass xy1 10.0 3.0 2 100.0 50.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 result ← 7.0
53for (Operation op : Operation.values()) {54 double result→ 7.0 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result7.0);57}a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result7.0);57}@Override public double apply(double x, double y)
pass 1 of 818MULTIPLY("*") {19 @Override20 public double apply(double x10.0, double y3.0) {21 return x10.0 * y3.0;22 }All 8 passes — pass 1 is the card above pass xy1 10.0 3.0 2 100.0 50.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 result ← 30.0
53for (Operation op : Operation.values()) {54 double result→ 30.0 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result30.0);57}a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result30.0);57}@Override public double apply(double x, double y)
pass 1 of 824DIVIDE("/") {25 @Override26 public double apply(double x10.0, double y3.0) {27 return x10.0 / y3.0;28 }All 8 passes — pass 1 is the card above pass xy1 10.0 3.0 2 100.0 50.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 result ← 3.3333333333333335
53for (Operation op : Operation.values()) {54 double result→ 3.3333333333333335 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result3.3333333333333335);57}a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result3.3333333333333335);57}operator ← PLUS, x ← 7.0, y ← 6.0
60// Build calculator61Operation operator→ PLUS = Operation.PLUS;62double x→ 7.0 = 7;63double y→ 6.0 = 6;6465double answer = operator.apply(x7.0, y6.0);66System.out.printf("\n%.0f %s %.0f = %.0f%n",answer ← 13.0
65double answer→ 13.0 = operator.apply(x7.0, y6.0);66System.out.printf("\n%.0f %s %.0f = %.0f%n",67 x7.0, operator.getSymbol(), y6.0, answer13.0);x, operator.getSymbol(), y, answer);
65double answer = operator.apply(x, y);66System.out.printf("\n%.0f %s %.0f = %.0f%n",67 x7.0, operator.getSymbol(), y6.0, answer13.0);6869// Evaluate expressions70class Expression {71 double left;72 Operation op;73 double right;74 75 Expression(double left, Operation op, double right) {76 this.left = left;77 this.op = op;78 this.right = right;79 }80 81 double evaluate() {82 return op.apply(left, right);83 }84 85 public String toString() {86 return String.format("%.0f %s %.0f = %.2f",87 left, op.getSymbol(), right, evaluate());88 }89}9091Expression[] expressions = {92 new Expression(100, Operation.PLUS, 50),93 new Expression(100, Operation.MINUS, 50),94 new Expression(100, Operation.MULTIPLY, 50),95 new Expression(100, Operation.DIVIDE, 50)96};this.left ← 100.0, this.op ← PLUS, this.right ← 50.0
pass 1 of 475Expression(double left100.0, Operation opPLUS, double right50.0) {76 this.left→ 100.0 = left100.0;77 this.op→ PLUS = opPLUS;78 this.right→ 50.0 = right50.0;79}All 4 passes — pass 1 is the card above pass opthis.leftthis.opthis.right1 PLUS 100.0 PLUS 50.0 2 MINUS 100.0 MINUS 50.0 3 MULTIPLY 100.0 MULTIPLY 50.0 4 DIVIDE 100.0 DIVIDE 50.0 Expression[] expressions =
91Expression[] expressions = {92 new Expression(100, Operation.PLUS, 50),93 new Expression(100, Operation.MINUS, 50),94 new Expression(100, Operation.MULTIPLY, 50),95 new Expression(100, Operation.DIVIDE, 50)96};9798System.out.println("\nExpressions:");99for (Expression expr : expressions) {output Expressions:double evaluate()
pass 1 of 2881double evaluate() {82 return op.apply(left100.0, right50.0);83}for (Expression expr : expressions)
pass 1 of 498System.out.println("\nExpressions:");99for (Expression expr100 + 50 = 150.00 : expressions) {100 System.out.println(" " + expr);All 4 passes — pass 1 is the card above pass expr1 100 + 50 = 150.00 2 100 - 50 = 50.00 3 100 * 50 = 5000.00 4 100 / 50 = 2.00 System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 + 50 = 150.00);101}System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 + 50 = 150.00);101}output 100 + 50 = 150.00System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 - 50 = 50.00);101}System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 - 50 = 50.00);101}output 100 - 50 = 50.00System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 * 50 = 5000.00);101}System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 * 50 = 5000.00);101}output 100 * 50 = 5000.00System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 / 50 = 2.00);101}System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 / 50 = 2.00);101}output 100 / 50 = 2.00
a ← 10.0, b ← 3.0
45public class AbstractMethod {46 public static void main(String[] args) {47 // Use operations48 double a→ 10.0 = 10;49 double b→ 3.0 = 3;50 51 System.out.println("Calculate: a=" + a10.0 + ", b=" + b3.0 + "\n");outputCalculate: a=10.0, b=3.0this.symbol ← +
pass 1 of 433Operation(String symbol+) {34 this.symbol→ + = symbol+;35}All 4 passes — pass 1 is the card above pass symbolthis.symbol1 + + 2 - - 3 * * 4 / / for (Operation op : Operation.values())
pass 1 of 453for (Operation opPLUS : Operation.values()) {54 double result = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",All 4 passes — pass 1 is the card above pass op1 PLUS 2 MINUS 3 MULTIPLY 4 DIVIDE @Override public double apply(double x, double y)
pass 1 of 86PLUS("+") {7 @Override8 public double apply(double x10.0, double y3.0) {9 return x10.0 + y3.0;10 }All 8 passes — pass 1 is the card above pass xy1 10.0 3.0 2 100.0 50.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 result ← 13.0
53for (Operation op : Operation.values()) {54 double result→ 13.0 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result13.0);57}public String getSymbol()
pass 1 of 3340public String getSymbol() {41 return symbol+;42}33 passes — pass 1 is the card above pass symbol1 + 2 - 3 * 4 / 5 / 6 + 7 + 8 + 9 + ⋯ 22 more passes ⋯ 32 / 33 / a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result13.0);57}@Override public double apply(double x, double y)
pass 1 of 812MINUS("-") {13 @Override14 public double apply(double x10.0, double y3.0) {15 return x10.0 - y3.0;16 }All 8 passes — pass 1 is the card above pass xy1 10.0 3.0 2 100.0 50.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 result ← 7.0
53for (Operation op : Operation.values()) {54 double result→ 7.0 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result7.0);57}a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result7.0);57}@Override public double apply(double x, double y)
pass 1 of 818MULTIPLY("*") {19 @Override20 public double apply(double x10.0, double y3.0) {21 return x10.0 * y3.0;22 }All 8 passes — pass 1 is the card above pass xy1 10.0 3.0 2 100.0 50.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 result ← 30.0
53for (Operation op : Operation.values()) {54 double result→ 30.0 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result30.0);57}a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result30.0);57}@Override public double apply(double x, double y)
pass 1 of 924DIVIDE("/") {25 @Override26 public double apply(double x10.0, double y3.0) {27 return x10.0 / y3.0;28 }All 9 passes — pass 1 is the card above pass xy1 10.0 3.0 2 7.0 6.0 3 100.0 50.0 4 100.0 50.0 5 100.0 50.0 6 100.0 50.0 7 100.0 50.0 8 100.0 50.0 9 100.0 50.0 result ← 3.3333333333333335
53for (Operation op : Operation.values()) {54 double result→ 3.3333333333333335 = op.apply(a10.0, b3.0);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result3.3333333333333335);57}a, op.getSymbol(), b, result);
54 double result = op.apply(a, b);55 System.out.printf("%s %s %s = %.2f%n",56 a10.0, op.getSymbol(), b3.0, result3.3333333333333335);57}operator ← DIVIDE, x ← 7.0, y ← 6.0
60// Build calculator61Operation operator→ DIVIDE = Operation.DIVIDE;62double x→ 7.0 = 7;63double y→ 6.0 = 6;6465double answer = operator.apply(x7.0, y6.0);66System.out.printf("\n%.0f %s %.0f = %.0f%n",answer ← 1.1666666666666667
65double answer→ 1.1666666666666667 = operator.apply(x7.0, y6.0);66System.out.printf("\n%.0f %s %.0f = %.0f%n",67 x7.0, operator.getSymbol(), y6.0, answer1.1666666666666667);x, operator.getSymbol(), y, answer);
65double answer = operator.apply(x, y);66System.out.printf("\n%.0f %s %.0f = %.0f%n",67 x7.0, operator.getSymbol(), y6.0, answer1.1666666666666667);6869// Evaluate expressions70class Expression {71 double left;72 Operation op;73 double right;74 75 Expression(double left, Operation op, double right) {76 this.left = left;77 this.op = op;78 this.right = right;79 }80 81 double evaluate() {82 return op.apply(left, right);83 }84 85 public String toString() {86 return String.format("%.0f %s %.0f = %.2f",87 left, op.getSymbol(), right, evaluate());88 }89}9091Expression[] expressions = {92 new Expression(100, Operation.PLUS, 50),93 new Expression(100, Operation.MINUS, 50),94 new Expression(100, Operation.MULTIPLY, 50),95 new Expression(100, Operation.DIVIDE, 50)96};this.left ← 100.0, this.op ← PLUS, this.right ← 50.0
pass 1 of 475Expression(double left100.0, Operation opPLUS, double right50.0) {76 this.left→ 100.0 = left100.0;77 this.op→ PLUS = opPLUS;78 this.right→ 50.0 = right50.0;79}All 4 passes — pass 1 is the card above pass opthis.leftthis.opthis.right1 PLUS 100.0 PLUS 50.0 2 MINUS 100.0 MINUS 50.0 3 MULTIPLY 100.0 MULTIPLY 50.0 4 DIVIDE 100.0 DIVIDE 50.0 Expression[] expressions =
91Expression[] expressions = {92 new Expression(100, Operation.PLUS, 50),93 new Expression(100, Operation.MINUS, 50),94 new Expression(100, Operation.MULTIPLY, 50),95 new Expression(100, Operation.DIVIDE, 50)96};9798System.out.println("\nExpressions:");99for (Expression expr : expressions) {output Expressions:double evaluate()
pass 1 of 2881double evaluate() {82 return op.apply(left100.0, right50.0);83}for (Expression expr : expressions)
pass 1 of 498System.out.println("\nExpressions:");99for (Expression expr100 + 50 = 150.00 : expressions) {100 System.out.println(" " + expr);All 4 passes — pass 1 is the card above pass expr1 100 + 50 = 150.00 2 100 - 50 = 50.00 3 100 * 50 = 5000.00 4 100 / 50 = 2.00 System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 + 50 = 150.00);101}System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 + 50 = 150.00);101}output 100 + 50 = 150.00System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 - 50 = 50.00);101}System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 - 50 = 50.00);101}output 100 - 50 = 50.00System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 * 50 = 5000.00);101}System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 * 50 = 5000.00);101}output 100 * 50 = 5000.00System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 / 50 = 2.00);101}System.out.println(" " + expr);
99for (Expression expr : expressions) {100 System.out.println(" " + expr100 / 50 = 2.00);101}output 100 / 50 = 2.00
abstract method in enum - each constant implements differently.
Enum implementing interface
Enums can implement interfaces.
// Enum implementing interface
// Concept: enum implements interface
// Concept: polymorphism with enum
interface Describable {
String getDescription();
String getDetails();
}
// Enum implementing interface
enum HttpStatus implements Describable {
OK(200, "Success"),
CREATED(201, "Resource created"),
BAD_REQUEST(400, "Invalid request"),
NOT_FOUND(404, "Resource not found"),
SERVER_ERROR(500, "Internal error");
private final int code;
private final String message;
HttpStatus(int code, String message) {
this.code = code;
this.message = message;
}
// Implement interface methods
@Override
public String getDescription() {
return code + " " + name();
}
@Override
public String getDetails() {
return message;
}
// Enum-specific methods
public int getCode() {
return code;
}
public boolean isSuccess() {
return code >= 200 && code < 300;
}
public boolean isError() {
return code >= 400;
}
}
public class ImplementingInterface {
public static void main(String[] args) {
// Use enum as interface type
Describable status = HttpStatus.OK;
System.out.println("Description: " + status.getDescription());
System.out.println("Details: " + status.getDetails());
// Check status types
HttpStatus[] responses = {
HttpStatus.OK,
HttpStatus.NOT_FOUND,
HttpStatus.SERVER_ERROR,
HttpStatus.CREATED
};
System.out.println("\nAnalyzing responses:");
for (HttpStatus resp : responses) {
String type = resp.isSuccess() ? "SUCCESS" :
resp.isError() ? "ERROR" : "INFO";
System.out.printf("%d %s: %s [%s]%n",
resp.getCode(),
resp.name(),
resp.getDetails(),
type);
}
// Polymorphic processing
System.out.println("\nPolymorphic usage:");
printDescribable(HttpStatus.BAD_REQUEST);
// Filter by criteria
System.out.println("\nError statuses:");
for (HttpStatus s : HttpStatus.values()) {
if (s.isError()) {
System.out.println(" " + s.getDescription());
}
}
System.out.println("\nSuccess statuses:");
for (HttpStatus s : HttpStatus.values()) {
if (s.isSuccess()) {
System.out.println(" " + s.getDescription());
}
}
// Find by code
int searchCode = 404;
HttpStatus found = null;
for (HttpStatus s : HttpStatus.values()) {
if (s.getCode() == searchCode) {
found = s;
break;
}
}
if (found != null) {
System.out.println("\nCode " + searchCode + ": " + found.getDetails());
}
}
static void printDescribable(Describable d) {
System.out.println(" " + d.getDescription());
System.out.println(" " + d.getDetails());
}
}
// Enum implementing interface
// Concept: enum implements interface
// Concept: polymorphism with enum
interface Describable {
String getDescription();
String getDetails();
}
// Enum implementing interface
enum HttpStatus implements Describable {
OK(200, "Success"),
CREATED(201, "Resource created"),
BAD_REQUEST(400, "Invalid request"),
NOT_FOUND(404, "Resource not found"),
SERVER_ERROR(500, "Internal error");
private final int code;
private final String message;
HttpStatus(int code, String message) {
this.code = code;
this.message = message;
}
// Implement interface methods
@Override
public String getDescription() {
return code + " " + name();
}
@Override
public String getDetails() {
return message;
}
// Enum-specific methods
public int getCode() {
return code;
}
public boolean isSuccess() {
return code >= 200 && code < 300;
}
public boolean isError() {
return code >= 400;
}
}
public class ImplementingInterface {
public static void main(String[] args) {
// Use enum as interface type
Describable status = HttpStatus.OK;
System.out.println("Description: " + status.getDescription());
System.out.println("Details: " + status.getDetails());
// Check status types
HttpStatus[] responses = {
HttpStatus.OK,
HttpStatus.NOT_FOUND,
HttpStatus.SERVER_ERROR,
HttpStatus.CREATED
};
System.out.println("\nAnalyzing responses:");
for (HttpStatus resp : responses) {
String type = resp.isSuccess() ? "SUCCESS" :
resp.isError() ? "ERROR" : "INFO";
System.out.printf("%d %s: %s [%s]%n",
resp.getCode(),
resp.name(),
resp.getDetails(),
type);
}
// Polymorphic processing
System.out.println("\nPolymorphic usage:");
printDescribable(HttpStatus.BAD_REQUEST);
// Filter by criteria
System.out.println("\nError statuses:");
for (HttpStatus s : HttpStatus.values()) {
if (s.isError()) {
System.out.println(" " + s.getDescription());
}
}
System.out.println("\nSuccess statuses:");
for (HttpStatus s : HttpStatus.values()) {
if (s.isSuccess()) {
System.out.println(" " + s.getDescription());
}
}
// Find by code
int searchCode = 200;
HttpStatus found = null;
for (HttpStatus s : HttpStatus.values()) {
if (s.getCode() == searchCode) {
found = s;
break;
}
}
if (found != null) {
System.out.println("\nCode " + searchCode + ": " + found.getDetails());
}
}
static void printDescribable(Describable d) {
System.out.println(" " + d.getDescription());
System.out.println(" " + d.getDetails());
}
}
// Enum implementing interface
// Concept: enum implements interface
// Concept: polymorphism with enum
interface Describable {
String getDescription();
String getDetails();
}
// Enum implementing interface
enum HttpStatus implements Describable {
OK(200, "Success"),
CREATED(201, "Resource created"),
BAD_REQUEST(400, "Invalid request"),
NOT_FOUND(404, "Resource not found"),
SERVER_ERROR(500, "Internal error");
private final int code;
private final String message;
HttpStatus(int code, String message) {
this.code = code;
this.message = message;
}
// Implement interface methods
@Override
public String getDescription() {
return code + " " + name();
}
@Override
public String getDetails() {
return message;
}
// Enum-specific methods
public int getCode() {
return code;
}
public boolean isSuccess() {
return code >= 200 && code < 300;
}
public boolean isError() {
return code >= 400;
}
}
public class ImplementingInterface {
public static void main(String[] args) {
// Use enum as interface type
Describable status = HttpStatus.OK;
System.out.println("Description: " + status.getDescription());
System.out.println("Details: " + status.getDetails());
// Check status types
HttpStatus[] responses = {
HttpStatus.OK,
HttpStatus.NOT_FOUND,
HttpStatus.SERVER_ERROR,
HttpStatus.CREATED
};
System.out.println("\nAnalyzing responses:");
for (HttpStatus resp : responses) {
String type = resp.isSuccess() ? "SUCCESS" :
resp.isError() ? "ERROR" : "INFO";
System.out.printf("%d %s: %s [%s]%n",
resp.getCode(),
resp.name(),
resp.getDetails(),
type);
}
// Polymorphic processing
System.out.println("\nPolymorphic usage:");
printDescribable(HttpStatus.BAD_REQUEST);
// Filter by criteria
System.out.println("\nError statuses:");
for (HttpStatus s : HttpStatus.values()) {
if (s.isError()) {
System.out.println(" " + s.getDescription());
}
}
System.out.println("\nSuccess statuses:");
for (HttpStatus s : HttpStatus.values()) {
if (s.isSuccess()) {
System.out.println(" " + s.getDescription());
}
}
// Find by code
int searchCode = 500;
HttpStatus found = null;
for (HttpStatus s : HttpStatus.values()) {
if (s.getCode() == searchCode) {
found = s;
break;
}
}
if (found != null) {
System.out.println("\nCode " + searchCode + ": " + found.getDetails());
}
}
static void printDescribable(Describable d) {
System.out.println(" " + d.getDescription());
System.out.println(" " + d.getDetails());
}
}
public static void main(String[] args)
51public class ImplementingInterface {52 public static void main(String[] args) {53 // Use enum as interface type54 Describable status = HttpStatus.OK;this.code ← 200, this.message ← Success
pass 1 of 521HttpStatus(int code200, String messageSuccess) {22 this.code→ 200 = code200;23 this.message→ Success = messageSuccess;24}All 5 passes — pass 1 is the card above pass codemessagethis.codethis.message1 200 Success 200 Success 2 201 Resource created 201 Resource created 3 400 Invalid request 400 Invalid request 4 404 Resource not found 404 Resource not found 5 500 Internal error 500 Internal error status ← OK
53// Use enum as interface type54Describable status→ OK = HttpStatus.OK;5556System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());@Override public String getDescription()
pass 1 of 726// Implement interface methods27@Override28public String getDescription() {29 return code200 + " " + name();30}All 7 passes — pass 1 is the card above pass code1 200 2 400 3 400 4 404 5 500 6 200 7 201 System.out.println("Description: " + status.getDescription());
56System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());outputDescription: 200 OK@Override public String getDetails()
pass 1 of 732@Override33public String getDetails() {34 return messageSuccess;35}All 7 passes — pass 1 is the card above pass message1 Success 2 Success 3 Resource not found 4 Internal error 5 Resource created 6 Invalid request 7 Resource not found HttpStatus[] responses =
56System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());5859//@help h160// Enum can implement interfaces like any class61// Useful for polymorphic collections62// All enum constants must provide the implementation63//@end6465// Check status types66HttpStatus[] responses = {67 HttpStatus.OK,68 HttpStatus.NOT_FOUND,69 HttpStatus.SERVER_ERROR,70 HttpStatus.CREATED71};7273System.out.println("\nAnalyzing responses:");74for (HttpStatus resp : responses) {outputDetails: Success Analyzing responses:for (HttpStatus resp : responses)
pass 1 of 473System.out.println("\nAnalyzing responses:");74for (HttpStatus respOK : responses) {75 String type = resp.isSuccess() ? "SUCCESS" :76 resp.isError() ? "ERROR" : "INFO";77 System.out.printf("%d %s: %s [%s]%n",All 4 passes — pass 1 is the card above pass resp1 OK 2 NOT_FOUND 3 SERVER_ERROR 4 CREATED public boolean isSuccess()
pass 1 of 942public boolean isSuccess() {43 return code200 >= 200 && code < 300;44}All 9 passes — pass 1 is the card above pass code1 200 2 404 3 500 4 201 5 200 6 201 7 400 8 404 9 500 type ← SUCCESS
74for (HttpStatus resp : responses) {75 String type→ SUCCESS = resp.isSuccess() ? "SUCCESS" :76 resp.isError() ? "ERROR" : "INFO";77 System.out.printf("%d %s: %s [%s]%n",78 resp.getCode(),79 resp.name(),80 resp.getDetails(),81 typeSUCCESS);82}public int getCode()
pass 1 of 837// Enum-specific methods38public int getCode() {39 return code200;40}All 8 passes — pass 1 is the card above pass codesearchCodesfound1 200 — — — 2 404 — — — 3 500 — — — 4 201 — — — 5 200 — — — 6 201 — — — 7 400 — — — 8 404 404 NOT_FOUND NOT_FOUND type);
76 resp.isError() ? "ERROR" : "INFO";77 System.out.printf("%d %s: %s [%s]%n",78 resp.getCode(),79 resp.name(),80 resp.getDetails(),81 typeSUCCESS);82}public boolean isError()
pass 1 of 746public boolean isError() {47 return code404 >= 400;48}All 7 passes — pass 1 is the card above pass code1 404 2 500 3 200 4 201 5 400 6 404 7 500 type ← ERROR
74for (HttpStatus resp : responses) {75 String type→ ERROR = resp.isSuccess() ? "SUCCESS" :76 resp.isError() ? "ERROR" : "INFO";77 System.out.printf("%d %s: %s [%s]%n",78 resp.getCode(),79 resp.name(),80 resp.getDetails(),81 typeERROR);82}type);
76 resp.isError() ? "ERROR" : "INFO";77 System.out.printf("%d %s: %s [%s]%n",78 resp.getCode(),79 resp.name(),80 resp.getDetails(),81 typeERROR);82}type ← ERROR
74for (HttpStatus resp : responses) {75 String type→ ERROR = resp.isSuccess() ? "SUCCESS" :76 resp.isError() ? "ERROR" : "INFO";77 System.out.printf("%d %s: %s [%s]%n",78 resp.getCode(),79 resp.name(),80 resp.getDetails(),81 typeERROR);82}type);
76 resp.isError() ? "ERROR" : "INFO";77 System.out.printf("%d %s: %s [%s]%n",78 resp.getCode(),79 resp.name(),80 resp.getDetails(),81 typeERROR);82}type ← SUCCESS
74for (HttpStatus resp : responses) {75 String type→ SUCCESS = resp.isSuccess() ? "SUCCESS" :76 resp.isError() ? "ERROR" : "INFO";77 System.out.printf("%d %s: %s [%s]%n",78 resp.getCode(),79 resp.name(),80 resp.getDetails(),81 typeSUCCESS);82}type);
76 resp.isError() ? "ERROR" : "INFO";77 System.out.printf("%d %s: %s [%s]%n",78 resp.getCode(),79 resp.name(),80 resp.getDetails(),81 typeSUCCESS);82}System.out.println(" Polymorphic usage:");
84// Polymorphic processing85System.out.println("\nPolymorphic usage:");86printDescribable(HttpStatus.BAD_REQUEST);output Polymorphic usage:static void printDescribable(Describable d)
121static void printDescribable(Describable dBAD_REQUEST) {122 System.out.println(" " + d.getDescription());123 System.out.println(" " + d.getDetails());System.out.println(" " + d.getDescription());
121static void printDescribable(Describable d) {122 System.out.println(" " + d.getDescription());123 System.out.println(" " + d.getDetails());124}output 400 BAD_REQUESTSystem.out.println(" " + d.getDetails());
85 System.out.println("\nPolymorphic usage:");86 printDescribable(HttpStatus.BAD_REQUEST);87 88 // Filter by criteria89 System.out.println("\nError statuses:");90 for (HttpStatus s : HttpStatus.values()) {91 if (s.isError()) {92 System.out.println(" " + s.getDescription());93 }94 }95 96 System.out.println("\nSuccess statuses:");97 for (HttpStatus s : HttpStatus.values()) {98 if (s.isSuccess()) {99 System.out.println(" " + s.getDescription());100 }101 }102 103 // Find by code104 int searchCode = 404;105 //@searchCode=404, 200, 500106 HttpStatus found = null;107 108 for (HttpStatus s : HttpStatus.values()) {109 if (s.getCode() == searchCode) {110 found = s;111 break;112 }113 }114 115 if (found != null) {116 System.out.println("\nCode " + searchCode + ": " + found.getDetails());117 }118 119}120121static void printDescribable(Describable d) {122 System.out.println(" " + d.getDescription());123 System.out.println(" " + d.getDetails());124}output Invalid request Error statuses:for (HttpStatus s : HttpStatus.values())
pass 1 of 589System.out.println("\nError statuses:");90for (HttpStatus sOK : HttpStatus.values()) {91 if (s.isError()) {All 5 passes — pass 1 is the card above pass s1 OK 2 CREATED 3 BAD_REQUEST 4 NOT_FOUND 5 SERVER_ERROR System.out.println(" " + s.getDescription());
91if (s.isError()) {92 System.out.println(" " + s.getDescription());93}output 400 BAD_REQUESTSystem.out.println(" " + s.getDescription());
91if (s.isError()) {92 System.out.println(" " + s.getDescription());93}output 404 NOT_FOUNDSystem.out.println(" " + s.getDescription());
91if (s.isError()) {92 System.out.println(" " + s.getDescription());93}output 500 SERVER_ERRORSystem.out.println(" Success statuses:");
96System.out.println("\nSuccess statuses:");97for (HttpStatus s : HttpStatus.values()) {output Success statuses:for (HttpStatus s : HttpStatus.values())
pass 1 of 596System.out.println("\nSuccess statuses:");97for (HttpStatus sOK : HttpStatus.values()) {98 if (s.isSuccess()) {All 5 passes — pass 1 is the card above pass s1 OK 2 CREATED 3 BAD_REQUEST 4 NOT_FOUND 5 SERVER_ERROR System.out.println(" " + s.getDescription());
98if (s.isSuccess()) {99 System.out.println(" " + s.getDescription());100}output 200 OKSystem.out.println(" " + s.getDescription());
98if (s.isSuccess()) {99 System.out.println(" " + s.getDescription());100}output 201 CREATEDsearchCode ← 404, found ← null
103// Find by code104int searchCode→ 404 = 404;105//@searchCode=404, 200, 500106HttpStatus found→ null = null;for (HttpStatus s : HttpStatus.values())
pass 1 of 4108for (HttpStatus sOK : HttpStatus.values()) {109 if (s.getCode() == searchCode) {All 4 passes — pass 1 is the card above pass ssearchCodefound1 OK — — 2 CREATED — — 3 BAD_REQUEST — — 4 NOT_FOUND 404 NOT_FOUND found ← NOT_FOUND
108for (HttpStatus s : HttpStatus.values()) {109 if (s.getCode() == searchCode404) {110 found→ NOT_FOUND = sNOT_FOUND;111 break;112 }if (found != null)
115if (foundNOT_FOUND != null) {116 System.out.println("\nCode " + searchCode404 + ": " + found.getDetails());117}System.out.println(" Code " + searchCode + ": " + found.getDetails());
115if (found != null) {116 System.out.println("\nCode " + searchCode404 + ": " + found.getDetails());117}output Code 404: Resource not found
public static void main(String[] args)
51public class ImplementingInterface {52 public static void main(String[] args) {53 // Use enum as interface type54 Describable status = HttpStatus.OK;this.code ← 200, this.message ← Success
pass 1 of 521HttpStatus(int code200, String messageSuccess) {22 this.code→ 200 = code200;23 this.message→ Success = messageSuccess;24}All 5 passes — pass 1 is the card above pass codemessagethis.codethis.message1 200 Success 200 Success 2 201 Resource created 201 Resource created 3 400 Invalid request 400 Invalid request 4 404 Resource not found 404 Resource not found 5 500 Internal error 500 Internal error status ← OK
53// Use enum as interface type54Describable status→ OK = HttpStatus.OK;5556System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());@Override public String getDescription()
pass 1 of 726// Implement interface methods27@Override28public String getDescription() {29 return code200 + " " + name();30}All 7 passes — pass 1 is the card above pass code1 200 2 400 3 400 4 404 5 500 6 200 7 201 System.out.println("Description: " + status.getDescription());
56System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());outputDescription: 200 OK@Override public String getDetails()
pass 1 of 732@Override33public String getDetails() {34 return messageSuccess;35}All 7 passes — pass 1 is the card above pass message1 Success 2 Success 3 Resource not found 4 Internal error 5 Resource created 6 Invalid request 7 Success HttpStatus[] responses =
56System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());585960// Check status types61HttpStatus[] responses = {62 HttpStatus.OK,63 HttpStatus.NOT_FOUND,64 HttpStatus.SERVER_ERROR,65 HttpStatus.CREATED66};6768System.out.println("\nAnalyzing responses:");69for (HttpStatus resp : responses) {outputDetails: Success Analyzing responses:for (HttpStatus resp : responses)
pass 1 of 468System.out.println("\nAnalyzing responses:");69for (HttpStatus respOK : responses) {70 String type = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",All 4 passes — pass 1 is the card above pass resp1 OK 2 NOT_FOUND 3 SERVER_ERROR 4 CREATED public boolean isSuccess()
pass 1 of 942public boolean isSuccess() {43 return code200 >= 200 && code < 300;44}All 9 passes — pass 1 is the card above pass code1 200 2 404 3 500 4 201 5 200 6 201 7 400 8 404 9 500 type ← SUCCESS
69for (HttpStatus resp : responses) {70 String type→ SUCCESS = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeSUCCESS);77}public int getCode()
pass 1 of 537// Enum-specific methods38public int getCode() {39 return code200;40}All 5 passes — pass 1 is the card above pass codesearchCodesfound1 200 — — — 2 404 — — — 3 500 — — — 4 201 — — — 5 200 200 OK OK type);
71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeSUCCESS);77}public boolean isError()
pass 1 of 746public boolean isError() {47 return code404 >= 400;48}All 7 passes — pass 1 is the card above pass code1 404 2 500 3 200 4 201 5 400 6 404 7 500 type ← ERROR
69for (HttpStatus resp : responses) {70 String type→ ERROR = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeERROR);77}type);
71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeERROR);77}type ← ERROR
69for (HttpStatus resp : responses) {70 String type→ ERROR = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeERROR);77}type);
71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeERROR);77}type ← SUCCESS
69for (HttpStatus resp : responses) {70 String type→ SUCCESS = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeSUCCESS);77}type);
71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeSUCCESS);77}System.out.println(" Polymorphic usage:");
79// Polymorphic processing80System.out.println("\nPolymorphic usage:");81printDescribable(HttpStatus.BAD_REQUEST);output Polymorphic usage:static void printDescribable(Describable d)
115static void printDescribable(Describable dBAD_REQUEST) {116 System.out.println(" " + d.getDescription());117 System.out.println(" " + d.getDetails());System.out.println(" " + d.getDescription());
115static void printDescribable(Describable d) {116 System.out.println(" " + d.getDescription());117 System.out.println(" " + d.getDetails());118}output 400 BAD_REQUESTSystem.out.println(" " + d.getDetails());
80 System.out.println("\nPolymorphic usage:");81 printDescribable(HttpStatus.BAD_REQUEST);82 83 // Filter by criteria84 System.out.println("\nError statuses:");85 for (HttpStatus s : HttpStatus.values()) {86 if (s.isError()) {87 System.out.println(" " + s.getDescription());88 }89 }90 91 System.out.println("\nSuccess statuses:");92 for (HttpStatus s : HttpStatus.values()) {93 if (s.isSuccess()) {94 System.out.println(" " + s.getDescription());95 }96 }97 98 // Find by code99 int searchCode = 200;100 HttpStatus found = null;101 102 for (HttpStatus s : HttpStatus.values()) {103 if (s.getCode() == searchCode) {104 found = s;105 break;106 }107 }108 109 if (found != null) {110 System.out.println("\nCode " + searchCode + ": " + found.getDetails());111 }112 113}114115static void printDescribable(Describable d) {116 System.out.println(" " + d.getDescription());117 System.out.println(" " + d.getDetails());118}output Invalid request Error statuses:for (HttpStatus s : HttpStatus.values())
pass 1 of 584System.out.println("\nError statuses:");85for (HttpStatus sOK : HttpStatus.values()) {86 if (s.isError()) {All 5 passes — pass 1 is the card above pass s1 OK 2 CREATED 3 BAD_REQUEST 4 NOT_FOUND 5 SERVER_ERROR System.out.println(" " + s.getDescription());
86if (s.isError()) {87 System.out.println(" " + s.getDescription());88}output 400 BAD_REQUESTSystem.out.println(" " + s.getDescription());
86if (s.isError()) {87 System.out.println(" " + s.getDescription());88}output 404 NOT_FOUNDSystem.out.println(" " + s.getDescription());
86if (s.isError()) {87 System.out.println(" " + s.getDescription());88}output 500 SERVER_ERRORSystem.out.println(" Success statuses:");
91System.out.println("\nSuccess statuses:");92for (HttpStatus s : HttpStatus.values()) {output Success statuses:for (HttpStatus s : HttpStatus.values())
pass 1 of 591System.out.println("\nSuccess statuses:");92for (HttpStatus sOK : HttpStatus.values()) {93 if (s.isSuccess()) {All 5 passes — pass 1 is the card above pass s1 OK 2 CREATED 3 BAD_REQUEST 4 NOT_FOUND 5 SERVER_ERROR System.out.println(" " + s.getDescription());
93if (s.isSuccess()) {94 System.out.println(" " + s.getDescription());95}output 200 OKSystem.out.println(" " + s.getDescription());
93if (s.isSuccess()) {94 System.out.println(" " + s.getDescription());95}output 201 CREATEDsearchCode ← 200, found ← null
98// Find by code99int searchCode→ 200 = 200;100HttpStatus found→ null = null;for (HttpStatus s : HttpStatus.values())
102for (HttpStatus sOK : HttpStatus.values()) {103 if (s.getCode() == searchCode) {found ← OK
102for (HttpStatus s : HttpStatus.values()) {103 if (s.getCode() == searchCode200) {104 found→ OK = sOK;105 break;106 }if (found != null)
109if (foundOK != null) {110 System.out.println("\nCode " + searchCode200 + ": " + found.getDetails());111}System.out.println(" Code " + searchCode + ": " + found.getDetails());
109if (found != null) {110 System.out.println("\nCode " + searchCode200 + ": " + found.getDetails());111}output Code 200: Success
public static void main(String[] args)
51public class ImplementingInterface {52 public static void main(String[] args) {53 // Use enum as interface type54 Describable status = HttpStatus.OK;this.code ← 200, this.message ← Success
pass 1 of 521HttpStatus(int code200, String messageSuccess) {22 this.code→ 200 = code200;23 this.message→ Success = messageSuccess;24}All 5 passes — pass 1 is the card above pass codemessagethis.codethis.message1 200 Success 200 Success 2 201 Resource created 201 Resource created 3 400 Invalid request 400 Invalid request 4 404 Resource not found 404 Resource not found 5 500 Internal error 500 Internal error status ← OK
53// Use enum as interface type54Describable status→ OK = HttpStatus.OK;5556System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());@Override public String getDescription()
pass 1 of 726// Implement interface methods27@Override28public String getDescription() {29 return code200 + " " + name();30}All 7 passes — pass 1 is the card above pass code1 200 2 400 3 400 4 404 5 500 6 200 7 201 System.out.println("Description: " + status.getDescription());
56System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());outputDescription: 200 OK@Override public String getDetails()
pass 1 of 732@Override33public String getDetails() {34 return messageSuccess;35}All 7 passes — pass 1 is the card above pass message1 Success 2 Success 3 Resource not found 4 Internal error 5 Resource created 6 Invalid request 7 Internal error HttpStatus[] responses =
56System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());585960// Check status types61HttpStatus[] responses = {62 HttpStatus.OK,63 HttpStatus.NOT_FOUND,64 HttpStatus.SERVER_ERROR,65 HttpStatus.CREATED66};6768System.out.println("\nAnalyzing responses:");69for (HttpStatus resp : responses) {outputDetails: Success Analyzing responses:for (HttpStatus resp : responses)
pass 1 of 468System.out.println("\nAnalyzing responses:");69for (HttpStatus respOK : responses) {70 String type = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",All 4 passes — pass 1 is the card above pass resp1 OK 2 NOT_FOUND 3 SERVER_ERROR 4 CREATED public boolean isSuccess()
pass 1 of 942public boolean isSuccess() {43 return code200 >= 200 && code < 300;44}All 9 passes — pass 1 is the card above pass code1 200 2 404 3 500 4 201 5 200 6 201 7 400 8 404 9 500 type ← SUCCESS
69for (HttpStatus resp : responses) {70 String type→ SUCCESS = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeSUCCESS);77}public int getCode()
pass 1 of 937// Enum-specific methods38public int getCode() {39 return code200;40}All 9 passes — pass 1 is the card above pass codesearchCodesfound1 200 — — — 2 404 — — — 3 500 — — — 4 201 — — — 5 200 — — — 6 201 — — — 7 400 — — — 8 404 — — — 9 500 500 SERVER_ERROR SERVER_ERROR type);
71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeSUCCESS);77}public boolean isError()
pass 1 of 746public boolean isError() {47 return code404 >= 400;48}All 7 passes — pass 1 is the card above pass code1 404 2 500 3 200 4 201 5 400 6 404 7 500 type ← ERROR
69for (HttpStatus resp : responses) {70 String type→ ERROR = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeERROR);77}type);
71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeERROR);77}type ← ERROR
69for (HttpStatus resp : responses) {70 String type→ ERROR = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeERROR);77}type);
71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeERROR);77}type ← SUCCESS
69for (HttpStatus resp : responses) {70 String type→ SUCCESS = resp.isSuccess() ? "SUCCESS" :71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeSUCCESS);77}type);
71 resp.isError() ? "ERROR" : "INFO";72 System.out.printf("%d %s: %s [%s]%n",73 resp.getCode(),74 resp.name(),75 resp.getDetails(),76 typeSUCCESS);77}System.out.println(" Polymorphic usage:");
79// Polymorphic processing80System.out.println("\nPolymorphic usage:");81printDescribable(HttpStatus.BAD_REQUEST);output Polymorphic usage:static void printDescribable(Describable d)
115static void printDescribable(Describable dBAD_REQUEST) {116 System.out.println(" " + d.getDescription());117 System.out.println(" " + d.getDetails());System.out.println(" " + d.getDescription());
115static void printDescribable(Describable d) {116 System.out.println(" " + d.getDescription());117 System.out.println(" " + d.getDetails());118}output 400 BAD_REQUESTSystem.out.println(" " + d.getDetails());
80 System.out.println("\nPolymorphic usage:");81 printDescribable(HttpStatus.BAD_REQUEST);82 83 // Filter by criteria84 System.out.println("\nError statuses:");85 for (HttpStatus s : HttpStatus.values()) {86 if (s.isError()) {87 System.out.println(" " + s.getDescription());88 }89 }90 91 System.out.println("\nSuccess statuses:");92 for (HttpStatus s : HttpStatus.values()) {93 if (s.isSuccess()) {94 System.out.println(" " + s.getDescription());95 }96 }97 98 // Find by code99 int searchCode = 500;100 HttpStatus found = null;101 102 for (HttpStatus s : HttpStatus.values()) {103 if (s.getCode() == searchCode) {104 found = s;105 break;106 }107 }108 109 if (found != null) {110 System.out.println("\nCode " + searchCode + ": " + found.getDetails());111 }112 113}114115static void printDescribable(Describable d) {116 System.out.println(" " + d.getDescription());117 System.out.println(" " + d.getDetails());118}output Invalid request Error statuses:for (HttpStatus s : HttpStatus.values())
pass 1 of 584System.out.println("\nError statuses:");85for (HttpStatus sOK : HttpStatus.values()) {86 if (s.isError()) {All 5 passes — pass 1 is the card above pass s1 OK 2 CREATED 3 BAD_REQUEST 4 NOT_FOUND 5 SERVER_ERROR System.out.println(" " + s.getDescription());
86if (s.isError()) {87 System.out.println(" " + s.getDescription());88}output 400 BAD_REQUESTSystem.out.println(" " + s.getDescription());
86if (s.isError()) {87 System.out.println(" " + s.getDescription());88}output 404 NOT_FOUNDSystem.out.println(" " + s.getDescription());
86if (s.isError()) {87 System.out.println(" " + s.getDescription());88}output 500 SERVER_ERRORSystem.out.println(" Success statuses:");
91System.out.println("\nSuccess statuses:");92for (HttpStatus s : HttpStatus.values()) {output Success statuses:for (HttpStatus s : HttpStatus.values())
pass 1 of 591System.out.println("\nSuccess statuses:");92for (HttpStatus sOK : HttpStatus.values()) {93 if (s.isSuccess()) {All 5 passes — pass 1 is the card above pass s1 OK 2 CREATED 3 BAD_REQUEST 4 NOT_FOUND 5 SERVER_ERROR System.out.println(" " + s.getDescription());
93if (s.isSuccess()) {94 System.out.println(" " + s.getDescription());95}output 200 OKSystem.out.println(" " + s.getDescription());
93if (s.isSuccess()) {94 System.out.println(" " + s.getDescription());95}output 201 CREATEDsearchCode ← 500, found ← null
98// Find by code99int searchCode→ 500 = 500;100HttpStatus found→ null = null;for (HttpStatus s : HttpStatus.values())
pass 1 of 5102for (HttpStatus sOK : HttpStatus.values()) {103 if (s.getCode() == searchCode) {All 5 passes — pass 1 is the card above pass ssearchCodefound1 OK — — 2 CREATED — — 3 BAD_REQUEST — — 4 NOT_FOUND — — 5 SERVER_ERROR 500 SERVER_ERROR found ← SERVER_ERROR
102for (HttpStatus s : HttpStatus.values()) {103 if (s.getCode() == searchCode500) {104 found→ SERVER_ERROR = sSERVER_ERROR;105 break;106 }if (found != null)
109if (foundSERVER_ERROR != null) {110 System.out.println("\nCode " + searchCode500 + ": " + found.getDetails());111}System.out.println(" Code " + searchCode + ": " + found.getDetails());
109if (found != null) {110 System.out.println("\nCode " + searchCode500 + ": " + found.getDetails());111}output Code 500: Internal error
enum Name implements Interface - enum provides interface methods.
Exercise: Practical.java
Build an operation enum (ADD, SUBTRACT, MULTIPLY, DIVIDE)