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.

mySize
EnumWithFields.java
Replay: real traced execution (multi-file project)
// 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);

    }
}
  1. 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.LARGE
  2. this.ounces ← 8, this.price ← 2.5

    pass 1 of 4
    13// 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
    passouncespricethis.ouncesthis.price
    182.582.5
    2123.0123.0
    3163.5163.5
    4204.0204.0
  3. 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: MEDIUM
  4. public int getOunces()

    pass 1 of 5
    19// Getter methods20public int getOunces() {21    return ounces12;22}
    All 5 passes — pass 1 is the card above
    passounces
    112
    28
    312
    416
    520
  5. System.out.println("Ounces: " + mySize.getOunces());

    35System.out.println("Size: " + mySize);36System.out.println("Ounces: " + mySize.getOunces());37System.out.println("Price: $" + mySize.getPrice());
    outputOunces: 12
  6. public double getPrice()

    pass 1 of 11
    24public double getPrice() {25    return price3.0;26}
    All 11 passes — pass 1 is the card above
    passprice
    13.0
    22.5
    33.0
    43.5
    54.0
    62.5
    72.5
    83.0
    93.0
    103.5
    113.5
  7. 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:
  8. for (CoffeeSize size : CoffeeSize.values())

    pass 1 of 4
    46System.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
    passsize
    1SMALL
    2MEDIUM
    3LARGE
    4EXTRA_LARGE
  9. 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}
  10. 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}
  11. 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}
  12. 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}
  13. 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:
  14. for (CoffeeSize size : order)

    pass 1 of 3
    60System.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
    passsize
    1SMALL
    2MEDIUM
    3LARGE
  15. 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}
  16. total ← 2.5

    62    System.out.printf("  %s: $%.2f%n", size, size.getPrice());63    total→ 2.5 += size.getPrice();64}
  17. 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}
  18. total ← 5.5

    62    System.out.printf("  %s: $%.2f%n", size, size.getPrice());63    total→ 5.5 += size.getPrice();64}
  19. 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}
  20. total ← 9.0

    62    System.out.printf("  %s: $%.2f%n", size, size.getPrice());63    total→ 9.0 += size.getPrice();64}
  21. System.out.printf("Total: $%.2f%n", total);

    64}65System.out.printf("Total: $%.2f%n", total9.0);
  1. 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;
  2. this.ounces ← 8, this.price ← 2.5

    pass 1 of 4
    13// 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
    passouncespricethis.ouncesthis.price
    182.582.5
    2123.0123.0
    3163.5163.5
    4204.0204.0
  3. 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: SMALL
  4. public int getOunces()

    pass 1 of 5
    19// Getter methods20public int getOunces() {21    return ounces8;22}
    All 5 passes — pass 1 is the card above
    passounces
    18
    28
    312
    416
    520
  5. System.out.println("Ounces: " + mySize.getOunces());

    34System.out.println("Size: " + mySize);35System.out.println("Ounces: " + mySize.getOunces());36System.out.println("Price: $" + mySize.getPrice());
    outputOunces: 8
  6. public double getPrice()

    pass 1 of 11
    24public double getPrice() {25    return price2.5;26}
    All 11 passes — pass 1 is the card above
    passprice
    12.5
    22.5
    33.0
    43.5
    54.0
    62.5
    72.5
    83.0
    93.0
    103.5
    113.5
  7. 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:
  8. for (CoffeeSize size : CoffeeSize.values())

    pass 1 of 4
    40System.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
    passsize
    1SMALL
    2MEDIUM
    3LARGE
    4EXTRA_LARGE
  9. 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}
  10. 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}
  11. 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}
  12. 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}
  13. 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:
  14. for (CoffeeSize size : order)

    pass 1 of 3
    54System.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
    passsize
    1SMALL
    2MEDIUM
    3LARGE
  15. 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}
  16. total ← 2.5

    56    System.out.printf("  %s: $%.2f%n", size, size.getPrice());57    total→ 2.5 += size.getPrice();58}
  17. 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}
  18. total ← 5.5

    56    System.out.printf("  %s: $%.2f%n", size, size.getPrice());57    total→ 5.5 += size.getPrice();58}
  19. 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}
  20. total ← 9.0

    56    System.out.printf("  %s: $%.2f%n", size, size.getPrice());57    total→ 9.0 += size.getPrice();58}
  21. System.out.printf("Total: $%.2f%n", total);

    58}59System.out.printf("Total: $%.2f%n", total9.0);
  1. 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;
  2. this.ounces ← 8, this.price ← 2.5

    pass 1 of 4
    13// 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
    passouncespricethis.ouncesthis.price
    182.582.5
    2123.0123.0
    3163.5163.5
    4204.0204.0
  3. 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: LARGE
  4. public int getOunces()

    pass 1 of 5
    19// Getter methods20public int getOunces() {21    return ounces16;22}
    All 5 passes — pass 1 is the card above
    passounces
    116
    28
    312
    416
    520
  5. System.out.println("Ounces: " + mySize.getOunces());

    34System.out.println("Size: " + mySize);35System.out.println("Ounces: " + mySize.getOunces());36System.out.println("Price: $" + mySize.getPrice());
    outputOunces: 16
  6. public double getPrice()

    pass 1 of 11
    24public double getPrice() {25    return price3.5;26}
    All 11 passes — pass 1 is the card above
    passprice
    13.5
    22.5
    33.0
    43.5
    54.0
    62.5
    72.5
    83.0
    93.0
    103.5
    113.5
  7. 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:
  8. for (CoffeeSize size : CoffeeSize.values())

    pass 1 of 4
    40System.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
    passsize
    1SMALL
    2MEDIUM
    3LARGE
    4EXTRA_LARGE
  9. 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}
  10. 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}
  11. 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}
  12. 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}
  13. 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:
  14. for (CoffeeSize size : order)

    pass 1 of 3
    54System.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
    passsize
    1SMALL
    2MEDIUM
    3LARGE
  15. 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}
  16. total ← 2.5

    56    System.out.printf("  %s: $%.2f%n", size, size.getPrice());57    total→ 2.5 += size.getPrice();58}
  17. 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}
  18. total ← 5.5

    56    System.out.printf("  %s: $%.2f%n", size, size.getPrice());57    total→ 5.5 += size.getPrice();58}
  19. 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}
  20. total ← 9.0

    56    System.out.printf("  %s: $%.2f%n", size, size.getPrice());57    total→ 9.0 += size.getPrice();58}
  21. 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 fields Enums can have private final fields. Each constant stores its own values.

Enum methods

Add behavior to enums.

current
EnumMethods.java
Replay: real traced execution (multi-file project)
// 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");
        }

    }
}
  1. 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.HOT
  2. this.fahrenheit ← 32

    pass 1 of 5
    13Temperature(int fahrenheit32) {14    this.fahrenheit→ 32 = fahrenheit32;15}
    All 5 passes — pass 1 is the card above
    passfahrenheitthis.fahrenheit
    13232
    25050
    36565
    47575
    59090
  3. 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: MILD
  4. public int getFahrenheit()

    pass 1 of 10
    38public int getFahrenheit() {39    return fahrenheit65;40}
    All 10 passes — pass 1 is the card above
    passfahrenheitt1
    165
    232
    350
    465
    575
    690
    750
    890
    950
    1090COLD
  5. 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: 65
  6. public double toCelsius()

    pass 1 of 6
    17// Convert to Celsius18public double toCelsius() {19    return (fahrenheit65 - 32) * 5.0 / 9.0;20}
    All 6 passes — pass 1 is the card above
    passfahrenheit
    165
    232
    350
    465
    575
    690
  7. 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.333333333333332
  8. System.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 weather
  9. public boolean isComfortable()

    pass 1 of 11
    33// Check if comfortable34public boolean isComfortable() {35    return fahrenheit65 >= 65 && fahrenheit <= 75;36}
    All 11 passes — pass 1 is the card above
    passfahrenheitt
    165
    232
    350
    465
    575
    690
    732
    850
    965MILD
    1075WARM
    1190
  10. 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:
  11. for (Temperature t : Temperature.values())

    pass 1 of 5
    62System.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
    passt
    1FREEZING
    2COLD
    3MILD
    4WARM
    5HOT
  12. 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}
  13. 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}
  14. 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}
  15. 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}
  16. 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}
  17. System.out.println(" Comfortable temperatures:");

    71// Find comfortable temperatures72System.out.println("\nComfortable temperatures:");73for (Temperature t : Temperature.values()) {
    output
    Comfortable temperatures:
  18. for (Temperature t : Temperature.values())

    pass 1 of 5
    72System.out.println("\nComfortable temperatures:");73for (Temperature tFREEZING : Temperature.values()) {74    if (t.isComfortable()) {
    All 5 passes — pass 1 is the card above
    passt
    1FREEZING
    2COLD
    3MILD
    4WARM
    5HOT
  19. if (t.isComfortable())

    pass 1 of 2
    73for (Temperature t : Temperature.values()) {74    if (t.isComfortable()) {75        System.out.println("  " + tMILD + ": " + t.getDescription());76    }
  20. System.out.println(" " + t + ": " + t.getDescription());

    74if (t.isComfortable()) {75    System.out.println("  " + tMILD + ": " + t.getDescription());76}
    output  MILD: Pleasant weather
  21. if (t.isComfortable())

    pass 2 of 2
    73for (Temperature t : Temperature.values()) {74    if (t.isComfortable()) {75        System.out.println("  " + tWARM + ": " + t.getDescription());76    }
  22. System.out.println(" " + t + ": " + t.getDescription());

    74if (t.isComfortable()) {75    System.out.println("  " + tWARM + ": " + t.getDescription());76}
    output  WARM: T-shirt weather
  23. t1 ← 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:
  24. 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());
  25. if (t1.getFahrenheit() < t2.getFahrenheit())

    88if (t1.getFahrenheit() < t2.getFahrenheit()) {89    System.out.println(t1COLD + " is cooler");90}
    outputCOLD is cooler
  1. public static void main(String[] args)

    43public class EnumMethods {44    public static void main(String[] args) {45        // Use enum methods46        Temperature current = Temperature.COLD;
  2. this.fahrenheit ← 32

    pass 1 of 5
    13Temperature(int fahrenheit32) {14    this.fahrenheit→ 32 = fahrenheit32;15}
    All 5 passes — pass 1 is the card above
    passfahrenheitthis.fahrenheit
    13232
    25050
    36565
    47575
    59090
  3. 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: COLD
  4. public int getFahrenheit()

    pass 1 of 10
    38public int getFahrenheit() {39    return fahrenheit50;40}
    All 10 passes — pass 1 is the card above
    passfahrenheitt1
    150
    232
    350
    465
    575
    690
    750
    890
    950
    1090COLD
  5. 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: 50
  6. public double toCelsius()

    pass 1 of 6
    17// Convert to Celsius18public double toCelsius() {19    return (fahrenheit50 - 32) * 5.0 / 9.0;20}
    All 6 passes — pass 1 is the card above
    passfahrenheit
    150
    232
    350
    465
    575
    690
  7. 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.0
  8. System.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 jacket
  9. public boolean isComfortable()

    pass 1 of 11
    33// Check if comfortable34public boolean isComfortable() {35    return fahrenheit50 >= 65 && fahrenheit <= 75;36}
    All 11 passes — pass 1 is the card above
    passfahrenheitt
    150
    232
    350
    465
    575
    690
    732
    850
    965MILD
    1075WARM
    1190
  10. 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:
  11. for (Temperature t : Temperature.values())

    pass 1 of 5
    56System.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
    passt
    1FREEZING
    2COLD
    3MILD
    4WARM
    5HOT
  12. 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}
  13. 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}
  14. 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}
  15. 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}
  16. 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}
  17. System.out.println(" Comfortable temperatures:");

    65// Find comfortable temperatures66System.out.println("\nComfortable temperatures:");67for (Temperature t : Temperature.values()) {
    output
    Comfortable temperatures:
  18. for (Temperature t : Temperature.values())

    pass 1 of 5
    66System.out.println("\nComfortable temperatures:");67for (Temperature tFREEZING : Temperature.values()) {68    if (t.isComfortable()) {
    All 5 passes — pass 1 is the card above
    passt
    1FREEZING
    2COLD
    3MILD
    4WARM
    5HOT
  19. if (t.isComfortable())

    pass 1 of 2
    67for (Temperature t : Temperature.values()) {68    if (t.isComfortable()) {69        System.out.println("  " + tMILD + ": " + t.getDescription());70    }
  20. System.out.println(" " + t + ": " + t.getDescription());

    68if (t.isComfortable()) {69    System.out.println("  " + tMILD + ": " + t.getDescription());70}
    output  MILD: Pleasant weather
  21. if (t.isComfortable())

    pass 2 of 2
    67for (Temperature t : Temperature.values()) {68    if (t.isComfortable()) {69        System.out.println("  " + tWARM + ": " + t.getDescription());70    }
  22. System.out.println(" " + t + ": " + t.getDescription());

    68if (t.isComfortable()) {69    System.out.println("  " + tWARM + ": " + t.getDescription());70}
    output  WARM: T-shirt weather
  23. t1 ← 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:
  24. 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());
  25. if (t1.getFahrenheit() < t2.getFahrenheit())

    82if (t1.getFahrenheit() < t2.getFahrenheit()) {83    System.out.println(t1COLD + " is cooler");84}
    outputCOLD is cooler
  1. public static void main(String[] args)

    43public class EnumMethods {44    public static void main(String[] args) {45        // Use enum methods46        Temperature current = Temperature.HOT;
  2. this.fahrenheit ← 32

    pass 1 of 5
    13Temperature(int fahrenheit32) {14    this.fahrenheit→ 32 = fahrenheit32;15}
    All 5 passes — pass 1 is the card above
    passfahrenheitthis.fahrenheit
    13232
    25050
    36565
    47575
    59090
  3. 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: HOT
  4. public int getFahrenheit()

    pass 1 of 10
    38public int getFahrenheit() {39    return fahrenheit90;40}
    All 10 passes — pass 1 is the card above
    passfahrenheitt1
    190
    232
    350
    465
    575
    690
    750
    890
    950
    1090COLD
  5. 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: 90
  6. public double toCelsius()

    pass 1 of 6
    17// Convert to Celsius18public double toCelsius() {19    return (fahrenheit90 - 32) * 5.0 / 9.0;20}
    All 6 passes — pass 1 is the card above
    passfahrenheit
    190
    232
    350
    465
    575
    690
  7. 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.22222222222222
  8. System.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 hydrated
  9. public boolean isComfortable()

    pass 1 of 11
    33// Check if comfortable34public boolean isComfortable() {35    return fahrenheit90 >= 65 && fahrenheit <= 75;36}
    All 11 passes — pass 1 is the card above
    passfahrenheitt
    190
    232
    350
    465
    575
    690
    732
    850
    965MILD
    1075WARM
    1190
  10. 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:
  11. for (Temperature t : Temperature.values())

    pass 1 of 5
    56System.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
    passt
    1FREEZING
    2COLD
    3MILD
    4WARM
    5HOT
  12. 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}
  13. 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}
  14. 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}
  15. 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}
  16. 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}
  17. System.out.println(" Comfortable temperatures:");

    65// Find comfortable temperatures66System.out.println("\nComfortable temperatures:");67for (Temperature t : Temperature.values()) {
    output
    Comfortable temperatures:
  18. for (Temperature t : Temperature.values())

    pass 1 of 5
    66System.out.println("\nComfortable temperatures:");67for (Temperature tFREEZING : Temperature.values()) {68    if (t.isComfortable()) {
    All 5 passes — pass 1 is the card above
    passt
    1FREEZING
    2COLD
    3MILD
    4WARM
    5HOT
  19. if (t.isComfortable())

    pass 1 of 2
    67for (Temperature t : Temperature.values()) {68    if (t.isComfortable()) {69        System.out.println("  " + tMILD + ": " + t.getDescription());70    }
  20. System.out.println(" " + t + ": " + t.getDescription());

    68if (t.isComfortable()) {69    System.out.println("  " + tMILD + ": " + t.getDescription());70}
    output  MILD: Pleasant weather
  21. if (t.isComfortable())

    pass 2 of 2
    67for (Temperature t : Temperature.values()) {68    if (t.isComfortable()) {69        System.out.println("  " + tWARM + ": " + t.getDescription());70    }
  22. System.out.println(" " + t + ": " + t.getDescription());

    68if (t.isComfortable()) {69    System.out.println("  " + tWARM + ": " + t.getDescription());70}
    output  WARM: T-shirt weather
  23. t1 ← 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:
  24. 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());
  25. 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.

example
ComplexEnum.java
Replay: real traced execution (multi-file project)
// 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));
        }

    }
}
  1. 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:
  2. this.baseCost ← 5.99, this.daysMin ← 5, this.description ← 5-7 business days

    pass 1 of 4
    14ShippingMethod(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
    passbaseCostdaysMindescriptionthis.baseCostthis.daysMinthis.description
    15.9955-7 business days5.9955-7 business days
    212.9922-3 business days12.9922-3 business days
    324.991Next business day24.991Next business day
    435.0107-14 business days35.0107-14 business days
  3. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    53for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {54    if (method.isAvailableFor(packageWeight)) {
    All 4 passes — pass 1 is the card above
    passmethod
    1STANDARD
    2EXPRESS
    3OVERNIGHT
    4INTERNATIONAL
  4. public boolean isAvailableFor(double weightPounds)

    pass 1 of 8
    31// 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
    passweightPounds
    110.0
    210.0
    310.0
    410.0
    55.0
    65.0
    75.0
    85.0
  5. if (method.isAvailableFor(packageWeight))

    pass 1 of 4
    53for (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",
  6. public double calculateCost(double weightPounds)

    pass 1 of 14
    20// Calculate cost with weight21public double calculateCost(double weightPounds10.0) {22    if (this == INTERNATIONAL) {
    14 passes — pass 1 is the card above
    passweightPounds
    110.0
    210.0
    310.0
    410.0
    55.0
    65.0
    75.0
    85.0
    95.0
    ⋯ 3 more passes ⋯
    135.0
    145.0
  7. else

    pass 1 of 8
    25    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
    passbaseCostweightPounds
    15.9910.0
    212.9910.0
    35.995.0
    45.995.0
    512.995.0
    65.995.0
    712.995.0
    85.995.0
  8. 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 {
  9. public String getDescription()

    pass 1 of 4
    39public 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
    passdescription
    15-7 business days
    22-3 business days
    3Next business day
    47-14 business days
  10. 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 {
  11. 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 {
  12. 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 {
  13. if (this == OVERNIGHT)

    pass 1 of 3
    23    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
    passweightPounds
    110.0
    25.0
    35.0
  14. 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 {
  15. 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 {
  16. if (this == INTERNATIONAL)

    pass 1 of 3
    21public 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
    passweightPounds
    110.0
    25.0
    35.0
  17. 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 {
  18. 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 {
  19. 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);
  20. lowestCost ← 8.49

    72ShippingMethod cheapest = ShippingMethod.STANDARD;73double lowestCost→ 8.49 = cheapest.calculateCost(weight5.0);
  21. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    75for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {76    if (method.isAvailableFor(weight)) {
    All 4 passes — pass 1 is the card above
    passmethod
    1STANDARD
    2EXPRESS
    3OVERNIGHT
    4INTERNATIONAL
  22. if (method.isAvailableFor(weight))

    pass 1 of 4
    75for (ShippingMethod method : ShippingMethod.values()) {76    if (method.isAvailableFor(weight5.0)) {77        double cost = method.calculateCost(weight5.0);78        if (cost < lowestCost) {
  23. cost ← 8.49

    76if (method.isAvailableFor(weight)) {77    double cost→ 8.49 = method.calculateCost(weight5.0);78    if (cost < lowestCost) {
  24. cost ← 15.49

    76if (method.isAvailableFor(weight)) {77    double cost→ 15.49 = method.calculateCost(weight5.0);78    if (cost < lowestCost) {
  25. cost ← 32.489999999999995

    76if (method.isAvailableFor(weight)) {77    double cost→ 32.489999999999995 = method.calculateCost(weight5.0);78    if (cost < lowestCost) {
  26. cost ← 47.5

    76if (method.isAvailableFor(weight)) {77    double cost→ 47.5 = method.calculateCost(weight5.0);78    if (cost < lowestCost) {
  27. 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;
  28. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    92for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {93    double cost = method.calculateCost(weight5.0);94    if (cost <= budget) {
    All 4 passes — pass 1 is the card above
    passmethod
    1STANDARD
    2EXPRESS
    3OVERNIGHT
    4INTERNATIONAL
  29. cost ← 8.49

    92for (ShippingMethod method : ShippingMethod.values()) {93    double cost→ 8.49 = method.calculateCost(weight5.0);94    if (cost <= budget) {
  30. if (cost <= budget)

    93double cost = method.calculateCost(weight);94if (cost8.49 <= budget15.0) {95    if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {
  31. fastest ← STANDARD

    94if (cost <= budget) {95    if (fastestnull == null || method.getDaysMin() < fastest.getDaysMin()) {96        fastest→ STANDARD = methodSTANDARD;97    }
  32. cost ← 15.49

    92for (ShippingMethod method : ShippingMethod.values()) {93    double cost→ 15.49 = method.calculateCost(weight5.0);94    if (cost <= budget) {
  33. cost ← 32.489999999999995

    92for (ShippingMethod method : ShippingMethod.values()) {93    double cost→ 32.489999999999995 = method.calculateCost(weight5.0);94    if (cost <= budget) {
  34. cost ← 47.5

    92for (ShippingMethod method : ShippingMethod.values()) {93    double cost→ 47.5 = method.calculateCost(weight5.0);94    if (cost <= budget) {
  35. 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}
  36. public int getDaysMin()

    40    public String getDescription() { return description; }41    public int getDaysMin() { return daysMin5; }42}
  37. 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}
  1. 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:
  2. this.baseCost ← 5.99, this.daysMin ← 5, this.description ← 5-7 business days

    pass 1 of 4
    14ShippingMethod(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
    passbaseCostdaysMindescriptionthis.baseCostthis.daysMinthis.description
    15.9955-7 business days5.9955-7 business days
    212.9922-3 business days12.9922-3 business days
    324.991Next business day24.991Next business day
    435.0107-14 business days35.0107-14 business days
  3. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    52for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {53    if (method.isAvailableFor(packageWeight)) {
    All 4 passes — pass 1 is the card above
    passmethod
    1STANDARD
    2EXPRESS
    3OVERNIGHT
    4INTERNATIONAL
  4. public boolean isAvailableFor(double weightPounds)

    pass 1 of 8
    31// 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
    passweightPounds
    120.0
    220.0
    320.0
    420.0
    55.0
    65.0
    75.0
    85.0
  5. if (method.isAvailableFor(packageWeight))

    pass 1 of 4
    52for (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",
  6. public double calculateCost(double weightPounds)

    pass 1 of 14
    20// Calculate cost with weight21public double calculateCost(double weightPounds20.0) {22    if (this == INTERNATIONAL) {
    14 passes — pass 1 is the card above
    passweightPounds
    120.0
    220.0
    320.0
    420.0
    55.0
    65.0
    75.0
    85.0
    95.0
    ⋯ 3 more passes ⋯
    135.0
    145.0
  7. else

    pass 1 of 8
    25    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
    passbaseCostweightPounds
    15.9920.0
    212.9920.0
    35.995.0
    45.995.0
    512.995.0
    65.995.0
    712.995.0
    85.995.0
  8. 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 {
  9. public String getDescription()

    pass 1 of 4
    39public 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
    passdescription
    15-7 business days
    22-3 business days
    3Next business day
    47-14 business days
  10. 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 {
  11. 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 {
  12. 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 {
  13. if (this == OVERNIGHT)

    pass 1 of 3
    23    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
    passweightPounds
    120.0
    25.0
    35.0
  14. 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 {
  15. 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 {
  16. if (this == INTERNATIONAL)

    pass 1 of 3
    21public 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
    passweightPounds
    120.0
    25.0
    35.0
  17. 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 {
  18. 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 {
  19. 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);
  20. lowestCost ← 8.49

    65ShippingMethod cheapest = ShippingMethod.STANDARD;66double lowestCost→ 8.49 = cheapest.calculateCost(weight5.0);
  21. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    68for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {69    if (method.isAvailableFor(weight)) {
    All 4 passes — pass 1 is the card above
    passmethod
    1STANDARD
    2EXPRESS
    3OVERNIGHT
    4INTERNATIONAL
  22. if (method.isAvailableFor(weight))

    pass 1 of 4
    68for (ShippingMethod method : ShippingMethod.values()) {69    if (method.isAvailableFor(weight5.0)) {70        double cost = method.calculateCost(weight5.0);71        if (cost < lowestCost) {
  23. cost ← 8.49

    69if (method.isAvailableFor(weight)) {70    double cost→ 8.49 = method.calculateCost(weight5.0);71    if (cost < lowestCost) {
  24. cost ← 15.49

    69if (method.isAvailableFor(weight)) {70    double cost→ 15.49 = method.calculateCost(weight5.0);71    if (cost < lowestCost) {
  25. cost ← 32.489999999999995

    69if (method.isAvailableFor(weight)) {70    double cost→ 32.489999999999995 = method.calculateCost(weight5.0);71    if (cost < lowestCost) {
  26. cost ← 47.5

    69if (method.isAvailableFor(weight)) {70    double cost→ 47.5 = method.calculateCost(weight5.0);71    if (cost < lowestCost) {
  27. 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;
  28. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    85for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {86    double cost = method.calculateCost(weight5.0);87    if (cost <= budget) {
    All 4 passes — pass 1 is the card above
    passmethod
    1STANDARD
    2EXPRESS
    3OVERNIGHT
    4INTERNATIONAL
  29. cost ← 8.49

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 8.49 = method.calculateCost(weight5.0);87    if (cost <= budget) {
  30. if (cost <= budget)

    86double cost = method.calculateCost(weight);87if (cost8.49 <= budget15.0) {88    if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {
  31. fastest ← STANDARD

    87if (cost <= budget) {88    if (fastestnull == null || method.getDaysMin() < fastest.getDaysMin()) {89        fastest→ STANDARD = methodSTANDARD;90    }
  32. cost ← 15.49

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 15.49 = method.calculateCost(weight5.0);87    if (cost <= budget) {
  33. cost ← 32.489999999999995

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 32.489999999999995 = method.calculateCost(weight5.0);87    if (cost <= budget) {
  34. cost ← 47.5

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 47.5 = method.calculateCost(weight5.0);87    if (cost <= budget) {
  35. 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}
  36. public int getDaysMin()

    40    public String getDescription() { return description; }41    public int getDaysMin() { return daysMin5; }42}
  37. 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}
  1. 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:
  2. this.baseCost ← 5.99, this.daysMin ← 5, this.description ← 5-7 business days

    pass 1 of 4
    14ShippingMethod(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
    passbaseCostdaysMindescriptionthis.baseCostthis.daysMinthis.description
    15.9955-7 business days5.9955-7 business days
    212.9922-3 business days12.9922-3 business days
    324.991Next business day24.991Next business day
    435.0107-14 business days35.0107-14 business days
  3. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    52for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {53    if (method.isAvailableFor(packageWeight)) {
    All 4 passes — pass 1 is the card above
    passmethodweightPounds
    1STANDARD
    2EXPRESS
    3OVERNIGHT60.0
    4INTERNATIONAL
  4. public boolean isAvailableFor(double weightPounds)

    pass 1 of 8
    31// 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
    passweightPoundsmethodbaseCost
    160.0
    260.0
    360.0OVERNIGHT
    460.0
    55.0
    65.0
    75.024.99
    85.0
  5. if (method.isAvailableFor(packageWeight))

    pass 1 of 3
    52for (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",
  6. public double calculateCost(double weightPounds)

    pass 1 of 13
    20// Calculate cost with weight21public double calculateCost(double weightPounds60.0) {22    if (this == INTERNATIONAL) {
    13 passes — pass 1 is the card above
    passweightPoundsbaseCost
    160.0
    260.0
    360.0
    45.0
    55.0
    65.0
    75.024.99
    85.0
    95.0
    ⋯ 2 more passes ⋯
    125.0
    135.0
  7. else

    pass 1 of 8
    25    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
    passbaseCostweightPounds
    15.9960.0
    212.9960.0
    35.995.0
    45.995.0
    512.995.0
    65.995.0
    712.995.0
    85.995.0
  8. 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 {
  9. public String getDescription()

    pass 1 of 3
    39public 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
    passdescription
    15-7 business days
    22-3 business days
    37-14 business days
  10. 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 {
  11. 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 {
  12. 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 {
  13. if (this == OVERNIGHT && weightPounds > 50)

    32public boolean isAvailableFor(double weightPounds) {33    if (this == OVERNIGHT && weightPounds60.0 > 50) {34        return false;  // Overnight has weight limit35    }
  14. else

    56        method, cost, method.getDescription());57} else {58    System.out.printf("%s: Not available for this weight%n", methodOVERNIGHT);59}
  15. if (this == INTERNATIONAL)

    pass 1 of 3
    21public 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
    passweightPounds
    160.0
    25.0
    35.0
  16. 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 {
  17. 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 {
  18. 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);
  19. lowestCost ← 8.49

    65ShippingMethod cheapest = ShippingMethod.STANDARD;66double lowestCost→ 8.49 = cheapest.calculateCost(weight5.0);
  20. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    68for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {69    if (method.isAvailableFor(weight)) {
    All 4 passes — pass 1 is the card above
    passmethodbaseCostweightPounds
    1STANDARD
    2EXPRESS
    3OVERNIGHT24.995.0
    4INTERNATIONAL
  21. if (method.isAvailableFor(weight))

    pass 1 of 4
    68for (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
    passbaseCostweightPounds
    1
    2
    324.995.0
    4
  22. cost ← 8.49

    69if (method.isAvailableFor(weight)) {70    double cost→ 8.49 = method.calculateCost(weight5.0);71    if (cost < lowestCost) {
  23. cost ← 15.49

    69if (method.isAvailableFor(weight)) {70    double cost→ 15.49 = method.calculateCost(weight5.0);71    if (cost < lowestCost) {
  24. if (this == OVERNIGHT)

    pass 1 of 2
    23    return baseCost + (weightPounds * 2.50);24} else if (this == OVERNIGHT) {25    return baseCost24.99 + (weightPounds5.0 * 1.50);26} else {
  25. cost ← 32.489999999999995

    69if (method.isAvailableFor(weight)) {70    double cost→ 32.489999999999995 = method.calculateCost(weight5.0);71    if (cost < lowestCost) {
  26. cost ← 47.5

    69if (method.isAvailableFor(weight)) {70    double cost→ 47.5 = method.calculateCost(weight5.0);71    if (cost < lowestCost) {
  27. 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;
  28. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    85for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {86    double cost = method.calculateCost(weight5.0);87    if (cost <= budget) {
    All 4 passes — pass 1 is the card above
    passmethodbaseCostweightPounds
    1STANDARD
    2EXPRESS
    3OVERNIGHT24.995.0
    4INTERNATIONAL
  29. cost ← 8.49

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 8.49 = method.calculateCost(weight5.0);87    if (cost <= budget) {
  30. if (cost <= budget)

    86double cost = method.calculateCost(weight);87if (cost8.49 <= budget15.0) {88    if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {
  31. fastest ← STANDARD

    87if (cost <= budget) {88    if (fastestnull == null || method.getDaysMin() < fastest.getDaysMin()) {89        fastest→ STANDARD = methodSTANDARD;90    }
  32. cost ← 15.49

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 15.49 = method.calculateCost(weight5.0);87    if (cost <= budget) {
  33. if (this == OVERNIGHT)

    pass 2 of 2
    23    return baseCost + (weightPounds * 2.50);24} else if (this == OVERNIGHT) {25    return baseCost24.99 + (weightPounds5.0 * 1.50);26} else {
  34. cost ← 32.489999999999995

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 32.489999999999995 = method.calculateCost(weight5.0);87    if (cost <= budget) {
  35. cost ← 47.5

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 47.5 = method.calculateCost(weight5.0);87    if (cost <= budget) {
  36. 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}
  37. public int getDaysMin()

    40    public String getDescription() { return description; }41    public int getDaysMin() { return daysMin5; }42}
  38. 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}
  1. 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:
  2. this.baseCost ← 5.99, this.daysMin ← 5, this.description ← 5-7 business days

    pass 1 of 4
    14ShippingMethod(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
    passbaseCostdaysMindescriptionthis.baseCostthis.daysMinthis.description
    15.9955-7 business days5.9955-7 business days
    212.9922-3 business days12.9922-3 business days
    324.991Next business day24.991Next business day
    435.0107-14 business days35.0107-14 business days
  3. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    52for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {53    if (method.isAvailableFor(packageWeight)) {
    All 4 passes — pass 1 is the card above
    passmethod
    1STANDARD
    2EXPRESS
    3OVERNIGHT
    4INTERNATIONAL
  4. public boolean isAvailableFor(double weightPounds)

    pass 1 of 8
    31// 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
    passweightPounds
    110.0
    210.0
    310.0
    410.0
    515.0
    615.0
    715.0
    815.0
  5. if (method.isAvailableFor(packageWeight))

    pass 1 of 4
    52for (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",
  6. public double calculateCost(double weightPounds)

    pass 1 of 14
    20// Calculate cost with weight21public double calculateCost(double weightPounds10.0) {22    if (this == INTERNATIONAL) {
    14 passes — pass 1 is the card above
    passweightPounds
    110.0
    210.0
    310.0
    410.0
    515.0
    615.0
    715.0
    815.0
    915.0
    ⋯ 3 more passes ⋯
    1315.0
    1415.0
  7. else

    pass 1 of 8
    25    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
    passbaseCostweightPounds
    15.9910.0
    212.9910.0
    35.9915.0
    45.9915.0
    512.9915.0
    65.9915.0
    712.9915.0
    85.9915.0
  8. 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 {
  9. public String getDescription()

    pass 1 of 4
    39public 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
    passdescription
    15-7 business days
    22-3 business days
    3Next business day
    47-14 business days
  10. 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 {
  11. 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 {
  12. 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 {
  13. if (this == OVERNIGHT)

    pass 1 of 3
    23    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
    passweightPounds
    110.0
    215.0
    315.0
  14. 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 {
  15. 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 {
  16. if (this == INTERNATIONAL)

    pass 1 of 3
    21public 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
    passweightPounds
    110.0
    215.0
    315.0
  17. 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 {
  18. 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 {
  19. 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);
  20. lowestCost ← 13.49

    65ShippingMethod cheapest = ShippingMethod.STANDARD;66double lowestCost→ 13.49 = cheapest.calculateCost(weight15.0);
  21. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    68for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {69    if (method.isAvailableFor(weight)) {
    All 4 passes — pass 1 is the card above
    passmethod
    1STANDARD
    2EXPRESS
    3OVERNIGHT
    4INTERNATIONAL
  22. if (method.isAvailableFor(weight))

    pass 1 of 4
    68for (ShippingMethod method : ShippingMethod.values()) {69    if (method.isAvailableFor(weight15.0)) {70        double cost = method.calculateCost(weight15.0);71        if (cost < lowestCost) {
  23. cost ← 13.49

    69if (method.isAvailableFor(weight)) {70    double cost→ 13.49 = method.calculateCost(weight15.0);71    if (cost < lowestCost) {
  24. cost ← 20.490000000000002

    69if (method.isAvailableFor(weight)) {70    double cost→ 20.490000000000002 = method.calculateCost(weight15.0);71    if (cost < lowestCost) {
  25. cost ← 47.489999999999995

    69if (method.isAvailableFor(weight)) {70    double cost→ 47.489999999999995 = method.calculateCost(weight15.0);71    if (cost < lowestCost) {
  26. cost ← 72.5

    69if (method.isAvailableFor(weight)) {70    double cost→ 72.5 = method.calculateCost(weight15.0);71    if (cost < lowestCost) {
  27. 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;
  28. for (ShippingMethod method : ShippingMethod.values())

    pass 1 of 4
    85for (ShippingMethod methodSTANDARD : ShippingMethod.values()) {86    double cost = method.calculateCost(weight15.0);87    if (cost <= budget) {
    All 4 passes — pass 1 is the card above
    passmethod
    1STANDARD
    2EXPRESS
    3OVERNIGHT
    4INTERNATIONAL
  29. cost ← 13.49

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 13.49 = method.calculateCost(weight15.0);87    if (cost <= budget) {
  30. if (cost <= budget)

    86double cost = method.calculateCost(weight);87if (cost13.49 <= budget15.0) {88    if (fastest == null || method.getDaysMin() < fastest.getDaysMin()) {
  31. fastest ← STANDARD

    87if (cost <= budget) {88    if (fastestnull == null || method.getDaysMin() < fastest.getDaysMin()) {89        fastest→ STANDARD = methodSTANDARD;90    }
  32. cost ← 20.490000000000002

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 20.490000000000002 = method.calculateCost(weight15.0);87    if (cost <= budget) {
  33. cost ← 47.489999999999995

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 47.489999999999995 = method.calculateCost(weight15.0);87    if (cost <= budget) {
  34. cost ← 72.5

    85for (ShippingMethod method : ShippingMethod.values()) {86    double cost→ 72.5 = method.calculateCost(weight15.0);87    if (cost <= budget) {
  35. 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}
  36. public int getDaysMin()

    40    public String getDescription() { return description; }41    public int getDaysMin() { return daysMin5; }42}
  37. 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.

operator
AbstractMethod.java
Replay: real traced execution (multi-file project)
// 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);
        }

    }
}
  1. 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.0
  2. this.symbol ← +

    pass 1 of 4
    33Operation(String symbol+) {34    this.symbol→ + = symbol+;35}
    All 4 passes — pass 1 is the card above
    passsymbolthis.symbol
    1++
    2--
    3**
    4//
  3. for (Operation op : Operation.values())

    pass 1 of 4
    53for (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
    passop
    1PLUS
    2MINUS
    3MULTIPLY
    4DIVIDE
  4. @Override public double apply(double x, double y)

    pass 1 of 8
    6PLUS("+") {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
    passxy
    110.03.0
    2100.050.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
  5. 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}
  6. public String getSymbol()

    pass 1 of 33
    40public String getSymbol() {41    return symbol+;42}
    33 passes — pass 1 is the card above
    passsymbol
    1+
    2-
    3*
    4/
    5*
    6+
    7+
    8+
    9+
    ⋯ 22 more passes ⋯
    32/
    33/
  7. 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}
  8. @Override public double apply(double x, double y)

    pass 1 of 8
    12MINUS("-") {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
    passxy
    110.03.0
    2100.050.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
  9. 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}
  10. 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}
  11. @Override public double apply(double x, double y)

    pass 1 of 9
    18MULTIPLY("*") {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
    passxy
    110.03.0
    27.06.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
    9100.050.0
  12. 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}
  13. 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}
  14. @Override public double apply(double x, double y)

    pass 1 of 8
    24DIVIDE("/") {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
    passxy
    110.03.0
    2100.050.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
  15. 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}
  16. 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}
  17. 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",
  18. 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);
  19. 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};
  20. this.left ← 100.0, this.op ← PLUS, this.right ← 50.0

    pass 1 of 4
    81Expression(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
    passopthis.leftthis.opthis.right
    1PLUS100.0PLUS50.0
    2MINUS100.0MINUS50.0
    3MULTIPLY100.0MULTIPLY50.0
    4DIVIDE100.0DIVIDE50.0
  21. 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:
  22. double evaluate()

    pass 1 of 28
    87double evaluate() {88    return op.apply(left100.0, right50.0);89}
  23. for (Expression expr : expressions)

    pass 1 of 4
    104System.out.println("\nExpressions:");105for (Expression expr100 + 50 = 150.00 : expressions) {106    System.out.println("  " + expr);
    All 4 passes — pass 1 is the card above
    passexpr
    1100 + 50 = 150.00
    2100 - 50 = 50.00
    3100 * 50 = 5000.00
    4100 / 50 = 2.00
  24. System.out.println(" " + expr);

    105for (Expression expr : expressions) {106    System.out.println("  " + expr100 + 50 = 150.00);107}
  25. System.out.println(" " + expr);

    105for (Expression expr : expressions) {106    System.out.println("  " + expr100 + 50 = 150.00);107}
    output  100 + 50 = 150.00
  26. System.out.println(" " + expr);

    105for (Expression expr : expressions) {106    System.out.println("  " + expr100 - 50 = 50.00);107}
  27. System.out.println(" " + expr);

    105for (Expression expr : expressions) {106    System.out.println("  " + expr100 - 50 = 50.00);107}
    output  100 - 50 = 50.00
  28. System.out.println(" " + expr);

    105for (Expression expr : expressions) {106    System.out.println("  " + expr100 * 50 = 5000.00);107}
  29. System.out.println(" " + expr);

    105for (Expression expr : expressions) {106    System.out.println("  " + expr100 * 50 = 5000.00);107}
    output  100 * 50 = 5000.00
  30. System.out.println(" " + expr);

    105for (Expression expr : expressions) {106    System.out.println("  " + expr100 / 50 = 2.00);107}
  31. System.out.println(" " + expr);

    105for (Expression expr : expressions) {106    System.out.println("  " + expr100 / 50 = 2.00);107}
    output  100 / 50 = 2.00
  1. 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.0
  2. this.symbol ← +

    pass 1 of 4
    33Operation(String symbol+) {34    this.symbol→ + = symbol+;35}
    All 4 passes — pass 1 is the card above
    passsymbolthis.symbol
    1++
    2--
    3**
    4//
  3. for (Operation op : Operation.values())

    pass 1 of 4
    53for (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
    passop
    1PLUS
    2MINUS
    3MULTIPLY
    4DIVIDE
  4. @Override public double apply(double x, double y)

    pass 1 of 9
    6PLUS("+") {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
    passxy
    110.03.0
    27.06.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
    9100.050.0
  5. 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}
  6. public String getSymbol()

    pass 1 of 33
    40public String getSymbol() {41    return symbol+;42}
    33 passes — pass 1 is the card above
    passsymbol
    1+
    2-
    3*
    4/
    5+
    6+
    7+
    8+
    9+
    ⋯ 22 more passes ⋯
    32/
    33/
  7. 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}
  8. @Override public double apply(double x, double y)

    pass 1 of 8
    12MINUS("-") {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
    passxy
    110.03.0
    2100.050.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
  9. 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}
  10. 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}
  11. @Override public double apply(double x, double y)

    pass 1 of 8
    18MULTIPLY("*") {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
    passxy
    110.03.0
    2100.050.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
  12. 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}
  13. 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}
  14. @Override public double apply(double x, double y)

    pass 1 of 8
    24DIVIDE("/") {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
    passxy
    110.03.0
    2100.050.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
  15. 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}
  16. 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}
  17. 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",
  18. 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);
  19. 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};
  20. this.left ← 100.0, this.op ← PLUS, this.right ← 50.0

    pass 1 of 4
    75Expression(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
    passopthis.leftthis.opthis.right
    1PLUS100.0PLUS50.0
    2MINUS100.0MINUS50.0
    3MULTIPLY100.0MULTIPLY50.0
    4DIVIDE100.0DIVIDE50.0
  21. 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:
  22. double evaluate()

    pass 1 of 28
    81double evaluate() {82    return op.apply(left100.0, right50.0);83}
  23. for (Expression expr : expressions)

    pass 1 of 4
    98System.out.println("\nExpressions:");99for (Expression expr100 + 50 = 150.00 : expressions) {100    System.out.println("  " + expr);
    All 4 passes — pass 1 is the card above
    passexpr
    1100 + 50 = 150.00
    2100 - 50 = 50.00
    3100 * 50 = 5000.00
    4100 / 50 = 2.00
  24. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 + 50 = 150.00);101}
  25. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 + 50 = 150.00);101}
    output  100 + 50 = 150.00
  26. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 - 50 = 50.00);101}
  27. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 - 50 = 50.00);101}
    output  100 - 50 = 50.00
  28. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 * 50 = 5000.00);101}
  29. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 * 50 = 5000.00);101}
    output  100 * 50 = 5000.00
  30. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 / 50 = 2.00);101}
  31. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 / 50 = 2.00);101}
    output  100 / 50 = 2.00
  1. 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.0
  2. this.symbol ← +

    pass 1 of 4
    33Operation(String symbol+) {34    this.symbol→ + = symbol+;35}
    All 4 passes — pass 1 is the card above
    passsymbolthis.symbol
    1++
    2--
    3**
    4//
  3. for (Operation op : Operation.values())

    pass 1 of 4
    53for (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
    passop
    1PLUS
    2MINUS
    3MULTIPLY
    4DIVIDE
  4. @Override public double apply(double x, double y)

    pass 1 of 8
    6PLUS("+") {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
    passxy
    110.03.0
    2100.050.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
  5. 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}
  6. public String getSymbol()

    pass 1 of 33
    40public String getSymbol() {41    return symbol+;42}
    33 passes — pass 1 is the card above
    passsymbol
    1+
    2-
    3*
    4/
    5/
    6+
    7+
    8+
    9+
    ⋯ 22 more passes ⋯
    32/
    33/
  7. 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}
  8. @Override public double apply(double x, double y)

    pass 1 of 8
    12MINUS("-") {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
    passxy
    110.03.0
    2100.050.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
  9. 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}
  10. 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}
  11. @Override public double apply(double x, double y)

    pass 1 of 8
    18MULTIPLY("*") {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
    passxy
    110.03.0
    2100.050.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
  12. 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}
  13. 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}
  14. @Override public double apply(double x, double y)

    pass 1 of 9
    24DIVIDE("/") {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
    passxy
    110.03.0
    27.06.0
    3100.050.0
    4100.050.0
    5100.050.0
    6100.050.0
    7100.050.0
    8100.050.0
    9100.050.0
  15. 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}
  16. 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}
  17. 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",
  18. 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);
  19. 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};
  20. this.left ← 100.0, this.op ← PLUS, this.right ← 50.0

    pass 1 of 4
    75Expression(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
    passopthis.leftthis.opthis.right
    1PLUS100.0PLUS50.0
    2MINUS100.0MINUS50.0
    3MULTIPLY100.0MULTIPLY50.0
    4DIVIDE100.0DIVIDE50.0
  21. 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:
  22. double evaluate()

    pass 1 of 28
    81double evaluate() {82    return op.apply(left100.0, right50.0);83}
  23. for (Expression expr : expressions)

    pass 1 of 4
    98System.out.println("\nExpressions:");99for (Expression expr100 + 50 = 150.00 : expressions) {100    System.out.println("  " + expr);
    All 4 passes — pass 1 is the card above
    passexpr
    1100 + 50 = 150.00
    2100 - 50 = 50.00
    3100 * 50 = 5000.00
    4100 / 50 = 2.00
  24. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 + 50 = 150.00);101}
  25. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 + 50 = 150.00);101}
    output  100 + 50 = 150.00
  26. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 - 50 = 50.00);101}
  27. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 - 50 = 50.00);101}
    output  100 - 50 = 50.00
  28. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 * 50 = 5000.00);101}
  29. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 * 50 = 5000.00);101}
    output  100 * 50 = 5000.00
  30. System.out.println(" " + expr);

    99for (Expression expr : expressions) {100    System.out.println("  " + expr100 / 50 = 2.00);101}
  31. 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.

constant-specific method Each enum constant can override methods with its own implementation.

Enum implementing interface

Enums can implement interfaces.

searchCode
ImplementingInterface.java
Replay: real traced execution (multi-file project)
// 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());
    }
}
  1. 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;
  2. this.code ← 200, this.message ← Success

    pass 1 of 5
    21HttpStatus(int code200, String messageSuccess) {22    this.code→ 200 = code200;23    this.message→ Success = messageSuccess;24}
    All 5 passes — pass 1 is the card above
    passcodemessagethis.codethis.message
    1200Success200Success
    2201Resource created201Resource created
    3400Invalid request400Invalid request
    4404Resource not found404Resource not found
    5500Internal error500Internal error
  3. status ← OK

    53// Use enum as interface type54Describable status→ OK = HttpStatus.OK;5556System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());
  4. @Override public String getDescription()

    pass 1 of 7
    26// Implement interface methods27@Override28public String getDescription() {29    return code200 + " " + name();30}
    All 7 passes — pass 1 is the card above
    passcode
    1200
    2400
    3400
    4404
    5500
    6200
    7201
  5. System.out.println("Description: " + status.getDescription());

    56System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());
    outputDescription: 200 OK
  6. @Override public String getDetails()

    pass 1 of 7
    32@Override33public String getDetails() {34    return messageSuccess;35}
    All 7 passes — pass 1 is the card above
    passmessage
    1Success
    2Success
    3Resource not found
    4Internal error
    5Resource created
    6Invalid request
    7Resource not found
  7. 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:
  8. for (HttpStatus resp : responses)

    pass 1 of 4
    73System.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
    passresp
    1OK
    2NOT_FOUND
    3SERVER_ERROR
    4CREATED
  9. public boolean isSuccess()

    pass 1 of 9
    42public boolean isSuccess() {43    return code200 >= 200 && code < 300;44}
    All 9 passes — pass 1 is the card above
    passcode
    1200
    2404
    3500
    4201
    5200
    6201
    7400
    8404
    9500
  10. 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}
  11. public int getCode()

    pass 1 of 8
    37// Enum-specific methods38public int getCode() {39    return code200;40}
    All 8 passes — pass 1 is the card above
    passcodesearchCodesfound
    1200
    2404
    3500
    4201
    5200
    6201
    7400
    8404404NOT_FOUNDNOT_FOUND
  12. 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}
  13. public boolean isError()

    pass 1 of 7
    46public boolean isError() {47    return code404 >= 400;48}
    All 7 passes — pass 1 is the card above
    passcode
    1404
    2500
    3200
    4201
    5400
    6404
    7500
  14. 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}
  15. 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}
  16. 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}
  17. 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}
  18. 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}
  19. 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}
  20. System.out.println(" Polymorphic usage:");

    84// Polymorphic processing85System.out.println("\nPolymorphic usage:");86printDescribable(HttpStatus.BAD_REQUEST);
    output
    Polymorphic usage:
  21. static void printDescribable(Describable d)

    121static void printDescribable(Describable dBAD_REQUEST) {122    System.out.println("  " + d.getDescription());123    System.out.println("  " + d.getDetails());
  22. 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_REQUEST
  23. System.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:
  24. for (HttpStatus s : HttpStatus.values())

    pass 1 of 5
    89System.out.println("\nError statuses:");90for (HttpStatus sOK : HttpStatus.values()) {91    if (s.isError()) {
    All 5 passes — pass 1 is the card above
    passs
    1OK
    2CREATED
    3BAD_REQUEST
    4NOT_FOUND
    5SERVER_ERROR
  25. System.out.println(" " + s.getDescription());

    91if (s.isError()) {92    System.out.println("  " + s.getDescription());93}
    output  400 BAD_REQUEST
  26. System.out.println(" " + s.getDescription());

    91if (s.isError()) {92    System.out.println("  " + s.getDescription());93}
    output  404 NOT_FOUND
  27. System.out.println(" " + s.getDescription());

    91if (s.isError()) {92    System.out.println("  " + s.getDescription());93}
    output  500 SERVER_ERROR
  28. System.out.println(" Success statuses:");

    96System.out.println("\nSuccess statuses:");97for (HttpStatus s : HttpStatus.values()) {
    output
    Success statuses:
  29. for (HttpStatus s : HttpStatus.values())

    pass 1 of 5
    96System.out.println("\nSuccess statuses:");97for (HttpStatus sOK : HttpStatus.values()) {98    if (s.isSuccess()) {
    All 5 passes — pass 1 is the card above
    passs
    1OK
    2CREATED
    3BAD_REQUEST
    4NOT_FOUND
    5SERVER_ERROR
  30. System.out.println(" " + s.getDescription());

    98if (s.isSuccess()) {99    System.out.println("  " + s.getDescription());100}
    output  200 OK
  31. System.out.println(" " + s.getDescription());

    98if (s.isSuccess()) {99    System.out.println("  " + s.getDescription());100}
    output  201 CREATED
  32. searchCode ← 404, found ← null

    103// Find by code104int searchCode→ 404 = 404;105//@searchCode=404, 200, 500106HttpStatus found→ null = null;
  33. for (HttpStatus s : HttpStatus.values())

    pass 1 of 4
    108for (HttpStatus sOK : HttpStatus.values()) {109    if (s.getCode() == searchCode) {
    All 4 passes — pass 1 is the card above
    passssearchCodefound
    1OK
    2CREATED
    3BAD_REQUEST
    4NOT_FOUND404NOT_FOUND
  34. found ← NOT_FOUND

    108for (HttpStatus s : HttpStatus.values()) {109    if (s.getCode() == searchCode404) {110        found→ NOT_FOUND = sNOT_FOUND;111        break;112    }
  35. if (found != null)

    115if (foundNOT_FOUND != null) {116    System.out.println("\nCode " + searchCode404 + ": " + found.getDetails());117}
  36. 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
  1. 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;
  2. this.code ← 200, this.message ← Success

    pass 1 of 5
    21HttpStatus(int code200, String messageSuccess) {22    this.code→ 200 = code200;23    this.message→ Success = messageSuccess;24}
    All 5 passes — pass 1 is the card above
    passcodemessagethis.codethis.message
    1200Success200Success
    2201Resource created201Resource created
    3400Invalid request400Invalid request
    4404Resource not found404Resource not found
    5500Internal error500Internal error
  3. status ← OK

    53// Use enum as interface type54Describable status→ OK = HttpStatus.OK;5556System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());
  4. @Override public String getDescription()

    pass 1 of 7
    26// Implement interface methods27@Override28public String getDescription() {29    return code200 + " " + name();30}
    All 7 passes — pass 1 is the card above
    passcode
    1200
    2400
    3400
    4404
    5500
    6200
    7201
  5. System.out.println("Description: " + status.getDescription());

    56System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());
    outputDescription: 200 OK
  6. @Override public String getDetails()

    pass 1 of 7
    32@Override33public String getDetails() {34    return messageSuccess;35}
    All 7 passes — pass 1 is the card above
    passmessage
    1Success
    2Success
    3Resource not found
    4Internal error
    5Resource created
    6Invalid request
    7Success
  7. 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:
  8. for (HttpStatus resp : responses)

    pass 1 of 4
    68System.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
    passresp
    1OK
    2NOT_FOUND
    3SERVER_ERROR
    4CREATED
  9. public boolean isSuccess()

    pass 1 of 9
    42public boolean isSuccess() {43    return code200 >= 200 && code < 300;44}
    All 9 passes — pass 1 is the card above
    passcode
    1200
    2404
    3500
    4201
    5200
    6201
    7400
    8404
    9500
  10. 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}
  11. public int getCode()

    pass 1 of 5
    37// Enum-specific methods38public int getCode() {39    return code200;40}
    All 5 passes — pass 1 is the card above
    passcodesearchCodesfound
    1200
    2404
    3500
    4201
    5200200OKOK
  12. 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}
  13. public boolean isError()

    pass 1 of 7
    46public boolean isError() {47    return code404 >= 400;48}
    All 7 passes — pass 1 is the card above
    passcode
    1404
    2500
    3200
    4201
    5400
    6404
    7500
  14. 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}
  15. 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}
  16. 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}
  17. 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}
  18. 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}
  19. 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}
  20. System.out.println(" Polymorphic usage:");

    79// Polymorphic processing80System.out.println("\nPolymorphic usage:");81printDescribable(HttpStatus.BAD_REQUEST);
    output
    Polymorphic usage:
  21. static void printDescribable(Describable d)

    115static void printDescribable(Describable dBAD_REQUEST) {116    System.out.println("  " + d.getDescription());117    System.out.println("  " + d.getDetails());
  22. 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_REQUEST
  23. System.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:
  24. for (HttpStatus s : HttpStatus.values())

    pass 1 of 5
    84System.out.println("\nError statuses:");85for (HttpStatus sOK : HttpStatus.values()) {86    if (s.isError()) {
    All 5 passes — pass 1 is the card above
    passs
    1OK
    2CREATED
    3BAD_REQUEST
    4NOT_FOUND
    5SERVER_ERROR
  25. System.out.println(" " + s.getDescription());

    86if (s.isError()) {87    System.out.println("  " + s.getDescription());88}
    output  400 BAD_REQUEST
  26. System.out.println(" " + s.getDescription());

    86if (s.isError()) {87    System.out.println("  " + s.getDescription());88}
    output  404 NOT_FOUND
  27. System.out.println(" " + s.getDescription());

    86if (s.isError()) {87    System.out.println("  " + s.getDescription());88}
    output  500 SERVER_ERROR
  28. System.out.println(" Success statuses:");

    91System.out.println("\nSuccess statuses:");92for (HttpStatus s : HttpStatus.values()) {
    output
    Success statuses:
  29. for (HttpStatus s : HttpStatus.values())

    pass 1 of 5
    91System.out.println("\nSuccess statuses:");92for (HttpStatus sOK : HttpStatus.values()) {93    if (s.isSuccess()) {
    All 5 passes — pass 1 is the card above
    passs
    1OK
    2CREATED
    3BAD_REQUEST
    4NOT_FOUND
    5SERVER_ERROR
  30. System.out.println(" " + s.getDescription());

    93if (s.isSuccess()) {94    System.out.println("  " + s.getDescription());95}
    output  200 OK
  31. System.out.println(" " + s.getDescription());

    93if (s.isSuccess()) {94    System.out.println("  " + s.getDescription());95}
    output  201 CREATED
  32. searchCode ← 200, found ← null

    98// Find by code99int searchCode→ 200 = 200;100HttpStatus found→ null = null;
  33. for (HttpStatus s : HttpStatus.values())

    102for (HttpStatus sOK : HttpStatus.values()) {103    if (s.getCode() == searchCode) {
  34. found ← OK

    102for (HttpStatus s : HttpStatus.values()) {103    if (s.getCode() == searchCode200) {104        found→ OK = sOK;105        break;106    }
  35. if (found != null)

    109if (foundOK != null) {110    System.out.println("\nCode " + searchCode200 + ": " + found.getDetails());111}
  36. System.out.println(" Code " + searchCode + ": " + found.getDetails());

    109if (found != null) {110    System.out.println("\nCode " + searchCode200 + ": " + found.getDetails());111}
    output
    Code 200: Success
  1. 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;
  2. this.code ← 200, this.message ← Success

    pass 1 of 5
    21HttpStatus(int code200, String messageSuccess) {22    this.code→ 200 = code200;23    this.message→ Success = messageSuccess;24}
    All 5 passes — pass 1 is the card above
    passcodemessagethis.codethis.message
    1200Success200Success
    2201Resource created201Resource created
    3400Invalid request400Invalid request
    4404Resource not found404Resource not found
    5500Internal error500Internal error
  3. status ← OK

    53// Use enum as interface type54Describable status→ OK = HttpStatus.OK;5556System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());
  4. @Override public String getDescription()

    pass 1 of 7
    26// Implement interface methods27@Override28public String getDescription() {29    return code200 + " " + name();30}
    All 7 passes — pass 1 is the card above
    passcode
    1200
    2400
    3400
    4404
    5500
    6200
    7201
  5. System.out.println("Description: " + status.getDescription());

    56System.out.println("Description: " + status.getDescription());57System.out.println("Details: " + status.getDetails());
    outputDescription: 200 OK
  6. @Override public String getDetails()

    pass 1 of 7
    32@Override33public String getDetails() {34    return messageSuccess;35}
    All 7 passes — pass 1 is the card above
    passmessage
    1Success
    2Success
    3Resource not found
    4Internal error
    5Resource created
    6Invalid request
    7Internal error
  7. 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:
  8. for (HttpStatus resp : responses)

    pass 1 of 4
    68System.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
    passresp
    1OK
    2NOT_FOUND
    3SERVER_ERROR
    4CREATED
  9. public boolean isSuccess()

    pass 1 of 9
    42public boolean isSuccess() {43    return code200 >= 200 && code < 300;44}
    All 9 passes — pass 1 is the card above
    passcode
    1200
    2404
    3500
    4201
    5200
    6201
    7400
    8404
    9500
  10. 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}
  11. public int getCode()

    pass 1 of 9
    37// Enum-specific methods38public int getCode() {39    return code200;40}
    All 9 passes — pass 1 is the card above
    passcodesearchCodesfound
    1200
    2404
    3500
    4201
    5200
    6201
    7400
    8404
    9500500SERVER_ERRORSERVER_ERROR
  12. 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}
  13. public boolean isError()

    pass 1 of 7
    46public boolean isError() {47    return code404 >= 400;48}
    All 7 passes — pass 1 is the card above
    passcode
    1404
    2500
    3200
    4201
    5400
    6404
    7500
  14. 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}
  15. 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}
  16. 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}
  17. 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}
  18. 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}
  19. 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}
  20. System.out.println(" Polymorphic usage:");

    79// Polymorphic processing80System.out.println("\nPolymorphic usage:");81printDescribable(HttpStatus.BAD_REQUEST);
    output
    Polymorphic usage:
  21. static void printDescribable(Describable d)

    115static void printDescribable(Describable dBAD_REQUEST) {116    System.out.println("  " + d.getDescription());117    System.out.println("  " + d.getDetails());
  22. 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_REQUEST
  23. System.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:
  24. for (HttpStatus s : HttpStatus.values())

    pass 1 of 5
    84System.out.println("\nError statuses:");85for (HttpStatus sOK : HttpStatus.values()) {86    if (s.isError()) {
    All 5 passes — pass 1 is the card above
    passs
    1OK
    2CREATED
    3BAD_REQUEST
    4NOT_FOUND
    5SERVER_ERROR
  25. System.out.println(" " + s.getDescription());

    86if (s.isError()) {87    System.out.println("  " + s.getDescription());88}
    output  400 BAD_REQUEST
  26. System.out.println(" " + s.getDescription());

    86if (s.isError()) {87    System.out.println("  " + s.getDescription());88}
    output  404 NOT_FOUND
  27. System.out.println(" " + s.getDescription());

    86if (s.isError()) {87    System.out.println("  " + s.getDescription());88}
    output  500 SERVER_ERROR
  28. System.out.println(" Success statuses:");

    91System.out.println("\nSuccess statuses:");92for (HttpStatus s : HttpStatus.values()) {
    output
    Success statuses:
  29. for (HttpStatus s : HttpStatus.values())

    pass 1 of 5
    91System.out.println("\nSuccess statuses:");92for (HttpStatus sOK : HttpStatus.values()) {93    if (s.isSuccess()) {
    All 5 passes — pass 1 is the card above
    passs
    1OK
    2CREATED
    3BAD_REQUEST
    4NOT_FOUND
    5SERVER_ERROR
  30. System.out.println(" " + s.getDescription());

    93if (s.isSuccess()) {94    System.out.println("  " + s.getDescription());95}
    output  200 OK
  31. System.out.println(" " + s.getDescription());

    93if (s.isSuccess()) {94    System.out.println("  " + s.getDescription());95}
    output  201 CREATED
  32. searchCode ← 500, found ← null

    98// Find by code99int searchCode→ 500 = 500;100HttpStatus found→ null = null;
  33. for (HttpStatus s : HttpStatus.values())

    pass 1 of 5
    102for (HttpStatus sOK : HttpStatus.values()) {103    if (s.getCode() == searchCode) {
    All 5 passes — pass 1 is the card above
    passssearchCodefound
    1OK
    2CREATED
    3BAD_REQUEST
    4NOT_FOUND
    5SERVER_ERROR500SERVER_ERROR
  34. found ← SERVER_ERROR

    102for (HttpStatus s : HttpStatus.values()) {103    if (s.getCode() == searchCode500) {104        found→ SERVER_ERROR = sSERVER_ERROR;105        break;106    }
  35. if (found != null)

    109if (foundSERVER_ERROR != null) {110    System.out.println("\nCode " + searchCode500 + ": " + found.getDetails());111}
  36. 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)