Demo - Enumeration Example
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:
Two enums,
CarType
andCarColor
, are declared. These enums represent the make of a car and the color of a car respectively.The
SportsCar
class is defined, and it has three private instance variables:make
of typeCarType
,color
of typeCarColor
, andprice
of typedouble
.The
SportsCar
constructor accepts three parameters to initialize themake
,color
, andprice
of aSportsCar
object.Getter methods
getMake()
,getColor()
, andgetPrice()
are defined to return the current value of the corresponding instance variables.The
toString()
method is overridden to return a formatted string representation of theSportsCar
object. It usesString.format
to format the price to two decimal places and to include commas as thousand separators. The%s
placeholders are used for themake
andcolor
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:
The
main
method of theDemo7
class is defined as the entry point of the program.Inside the
main
method, aSportsCar
object namedyourNewCar
is created using theCarType.PORSCHE
,CarColor.RED
, and a price of100000
.The switch statement is used to decide what to print based on the make of
yourNewCar
. ThegetMake()
method is called on theyourNewCar
object, which returns an enumerated value of typeCarType
.Depending on the returned
CarType
value, one of the cases in the switch statement will be executed:If
yourNewCar
's make isCarType.PORSCHE
, the message "Your car was made in Germany." is printed.If
yourNewCar
's make isCarType.FERRARI
, the message "Your car was made in Italy." is printed.If
yourNewCar
's make isCarType.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 possibleCarType
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