Enums User Input
Enums User Input
}
{
An enum is a special "class" that represents a
group of constants (unchangeable
variables, like final variables).
To create an enum, use the enum keyword
(instead of class or interface), and separate
the constants with a comma.
}
HIGH The output will be?
public static void main(String[] args) {
Level myVar = Level.MEDIUM;
System.out.println(myVar);
}
}
Enum in a Switch Statement
enum Level {
LOW,
MEDIUM,
HIGH Enums are often
}
used
public class Main {
public static void main(String[] args) { in switch statemen
Level myVar = Level.MEDIUM;
ts to check for
switch(myVar) {
case LOW: corresponding
System.out.println("Low level");
break;
values:
case MEDIUM:
System.out.println("Medium level");
break;
case HIGH:
System.out.println("High level");
break;
}
}
}
Loop Through an Enum
The enum type has a values() method, which returns an array of all enum
constants. This method is useful when you want to loop through the
constants of an enum:
enum Level {
LOW,
MEDIUM,
HIGH,
}
public class Main {
public static void main(String[] args) {
for (Level myVar : Level.values()) {
System.out.println(myVar);
}
}
}
Difference between Enums and
Classes
// Define the enum implementing the interface for (Task task : Task.values()) {
enum Task implements Actionable { System.out.print(task.name() + ": ");
task.performAction();
CLEAN { }
public void performAction() { }
System.out.println("Cleaning in progress..."); }
}
}, Explanation:
1.Interface Definition: The Actionable interface is created with
WASH { the method performAction.
public void performAction() { 2.Enum Implementation: The Task enum implements the
System.out.println("Washing in progress..."); Actionable interface. Each constant provides its specific
} implementation for the performAction method.
}, 3.Dynamic Behavior: When performAction is called on a Task
COOK { constant, the respective implementation is executed.
public void performAction() {
System.out.println("Cooking in progress..."); Output of the Example:
}
} CLEAN: Cleaning in progress...
} WASH: Washing in progress...
COOK: Cooking in progress...
Why And When To Use Enums?
enum Level {
LOW,
MEDIUM,
HIGH }
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
// String input
String name = myObj.nextLine();
// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();
. . . ;
Which method is used to read Strings from
input?
The nextLine() method
The Scanner() method
The readString() method
The userInput() method
Which method is used to read integers from
input?
The nextLine() method
The integer() method
The nextInt() method
The readInt() method
Activity: Employee Management System
In this activity, users will be able to enter different types of information about an
employee: name (String), age (int), salary (double), and employee status (boolean). Then,
the program will display the details of the employee.
Output:
Objective:
•Input Types: String, int, double, boolean. Enter employee name: John Doe
•Output: Display the entered information. Enter employee age: 30
Enter employee salary: 55000.75
Step-by-Step Instructions:
Is the employee active?
1.Create a new Java class called
EmployeeManagementSystem.
(true/false): true
2.Use the following Java input types:
• String for employee name. Employee Details:
• int for employee age. Name: John Doe
• double for employee salary. Age: 30
• boolean for employee status (active or Salary: $55000.75
inactive). Active: Yes