Demo - Enumeration Example

 

image-20240510-162416.png

 

SportsCar Class

/** * CarType enumerated data type */ enum CarType { PORSCHE, FERRARI, JAGUAR } /** * CarColor enumerated data type */ enum CarColor { RED, BLACK, BLUE, SILVER } /** * SportsCar class */ public class SportsCar { private CarType make; // The car's make private CarColor color; // The car's color private double price; // The car's price /** * The constructor accepts arguments for the * car's make, color, and price. */ public SportsCar(CarType aMake, CarColor aColor, double aPrice) { make = aMake; color = aColor; price = aPrice; } /** * getMake method */ public CarType getMake() { return make; } /** * getColor method */ public CarColor getColor() { return color; } /** * getPrice method */ public double getPrice() { return price; } /** * toString method */ public String toString() { // Create a string representing the object. String str = String.format("Make: %s\n" + "Color: %s\n" + "Price: $%,.2f", make, color, price); // Return the string. return str; } }

This Java program is an example of how to create a class that uses enumerated types or "enums". The class SportsCar is defined with attributes that include two enums: CarType and CarColor.

Here is a breakdown of the code:

  1. Two enums, CarType and CarColor, are declared. These enums represent the make of a car and the color of a car respectively.

  2. The SportsCar class is defined, and it has three private instance variables: make of type CarType, color of type CarColor, and price of type double.

  3. The SportsCar constructor accepts three parameters to initialize the make, color, and price of a SportsCar object.

  4. Getter methods getMake(), getColor(), and getPrice() are defined to return the current value of the corresponding instance variables.

  5. The toString() method is overridden to return a formatted string representation of the SportsCar object. It uses String.format to format the price to two decimal places and to include commas as thousand separators. The %s placeholders are used for the make and color which will be replaced by their string representations (the names of the enum constants).

So in this program, a SportsCar object can be created with a specific make and color (defined by the enums CarType and CarColor) and a price. The toString() method can then be used to get a nicely formatted string that represents the SportsCar object.

Driver

/** * This program shows that you can switch on an * enumerated type. */ public class Demo7 { public static void main(String[] args) { // Create a SportsCar object. SportsCar yourNewCar = new SportsCar(CarType.PORSCHE, CarColor.RED, 100000); // Get the car make and switch on it. switch (yourNewCar.getMake()) { case PORSCHE : System.out.println("Your car was made in Germany."); break; case FERRARI : System.out.println("Your car was made in Italy."); break; case JAGUAR : System.out.println("Your car was made in England."); break; default: System.out.println("I'm not sure where that car " + "was made."); } } }

This Java program demonstrates how to use a switch statement with an enumerated type.

Here is a breakdown of the code:

  1. The main method of the Demo7 class is defined as the entry point of the program.

  2. Inside the main method, a SportsCar object named yourNewCar is created using the CarType.PORSCHE, CarColor.RED, and a price of 100000.

  3. The switch statement is used to decide what to print based on the make of yourNewCar. The getMake() method is called on the yourNewCar object, which returns an enumerated value of type CarType.

  4. Depending on the returned CarType value, one of the cases in the switch statement will be executed:

    • If yourNewCar's make is CarType.PORSCHE, the message "Your car was made in Germany." is printed.

    • If yourNewCar's make is CarType.FERRARI, the message "Your car was made in Italy." is printed.

    • If yourNewCar's make is CarType.JAGUAR, the message "Your car was made in England." is printed.

    • If yourNewCar's make is anything else (which should not happen because we have already covered all possible CarType values), the default case is executed, printing "I'm not sure where that car was made."

Each case ends with a break statement to exit the switch statement after executing the matching case. Without the break, the program would continue executing the remaining cases ("fall through"), which is not the desired behavior in this case.

Remember, CarType and CarColor are enums defined in a different class (SportsCar class). This is a perfect example of how enums can be utilized in switch statements, offering a clean and efficient way of handling multiple conditions based on discrete possible values.

COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark