Enums Flashcards
What is an ENUM?
An enum in Java is a special type of data structure that allows you to define a set of named constants. These constants represent a fixed set of values that an object can take, such as the days of the week, the months of the year, or the different states of a game.
What are the advantages of using the enum class?
The constants can not be changed
The constants are still public so getter methods aren’t needed
The constants have an index which keeps them in order
They have a values method that returns all of the constants
A string with the enum constant name can by converted using .valueOf(“????”)
What is the syntax for an enum named Directions with the values: North, South, South East, and North West
public enum Directions {
NORTH, SOUTH, SOUTH_EAST, NORTH_WEST
}
Note: enums are always capitalized and multiple words are separated by an underscore (_).
How do we use an enum value from the following class?
public enum Directions {
NORTH, SOUTH, SOUTH_EAST, NORTH_WEST
}
You call the enum value from the class name:
Directions.NORTH
Directions.SOUTH_EAST
What are 2 enums in the standard Java library?
DayOfWeek
Month
What is the syntax to get the values from an enum named Teams?
Teams.values();
How can you convert a ENUM into an array?
In Java, you can convert an enum into an array using the values() method that is automatically generated for every enum type. The values() method returns an array that contains all of the constant values defined in the enum. Here’s an example: DayOfWeek[] daysOfWeek = DayOfWeek.values();
How can you convert a string into an ENUM?
In Java, you can convert a string into an enum using the valueOf() method that is provided by the enum class. The valueOf() method takes a string argument that represents the name of the constant value you want to obtain from the enum.
String dayStr = “TUESDAY”;
DayOfWeek day = DayOfWeek.valueOf(dayStr);
Does the enum class need a constructor run?
No the constructor is automatically run.
“Write the Java code to define an enum called Direction, with the following values: NORTH, SOUTH, EAST, and WEST. Each direction should have an associated abbreviation field, which is a string representing a short abbreviation for the direction, and a degrees field, which represents the number of degrees that the direction is offset from due north. The abbreviation and degrees fields should be accessible from outside the enum using getter methods.”
public enum Direction {
NORTH(“N”, 0),
SOUTH(“S”, 180),
EAST(“E”, 90),
WEST(“W”, 270);
private final String abbreviation; private final int degrees; Direction(String abbreviation, int degrees) { this.abbreviation = abbreviation; this.degrees = degrees; } public String getAbbreviation() { return abbreviation; } public int getDegrees() { return degrees; } }
How can we turn a enum called direction into an array?
Direction[] directions = Direction.values();
“Write a Java enum called BankMenuOption that represents a menu of options for a bank’s ATM machine. Each menu option should have two properties: optionText, which is a string representing the text of the menu option, and optionNumber, which is an integer representing the number of the menu option. The menu options should include the following:
- “Check account balance”
- “Make a withdrawal”
- “Make a deposit”
- “Transfer funds”
- “Pay bills”
- “Exit”
public enum BankMenuOption {
ACCOUNT_BALANCE(“Check account balance”, 1),
WITHDRAWAL(“Make a withdrawal”, 2),
DEPOSIT(“Make a deposit”, 3),
TRANSFER(“Transfer funds”, 4),
PAY_BILLS(“Pay bills”, 5),
EXIT(“Exit”, 6);
private final String optionText; private final int optionNumber; BankMenuOption(String optionText, int optionNumber) { this.optionText = optionText; this.optionNumber = optionNumber; } public String getOptionText() { return optionText; } public int getOptionNumber() { return optionNumber; } }
how can this Enum be converted into a String[] array named dayNames?
public enum DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
DayOfWeek[] days = DayOfWeek.values(); // Get an array of the enum values
String[] dayNames = new String[days.length]; // Create a new String array with the same length
for (int i = 0; i < days.length; i++) {
dayNames[i] = days[i].name(); // Convert each enum value to a String using the name() method
}