Enums Flashcards

1
Q

What is an ENUM?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the advantages of using the enum class?

A

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(“????”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax for an enum named Directions with the values: North, South, South East, and North West

A

public enum Directions {
NORTH, SOUTH, SOUTH_EAST, NORTH_WEST
}
Note: enums are always capitalized and multiple words are separated by an underscore (_).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we use an enum value from the following class?
public enum Directions {
NORTH, SOUTH, SOUTH_EAST, NORTH_WEST
}

A

You call the enum value from the class name:
Directions.NORTH
Directions.SOUTH_EAST

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are 2 enums in the standard Java library?

A

DayOfWeek
Month

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the syntax to get the values from an enum named Teams?

A

Teams.values();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you convert a ENUM into an array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you convert a string into an ENUM?

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Does the enum class need a constructor run?

A

No the constructor is automatically run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

“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.”

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can we turn a enum called direction into an array?

A

Direction[] directions = Direction.values();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

“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:

  1. “Check account balance”
  2. “Make a withdrawal”
  3. “Make a deposit”
  4. “Transfer funds”
  5. “Pay bills”
  6. “Exit”
A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how can this Enum be converted into a String[] array named dayNames?
public enum DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly