Navigating with Enum
Enums
are great for representing real-world directions clearly in code. Imagine you're creating a navigation feature for an application that provides instructions to users.
This exercise is part of the course
Input/Output and Streams in Java
Exercise instructions
- Define an
enum
namedDirection
containing constants:NORTH
,EAST
,SOUTH
, andWEST
. - Create a method with
Direction
parameter. - Print the
EAST
direction usingprintDirection()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
public class EnumDemo {
// Define enum
____ Direction {
NORTH, EAST, SOUTH, WEST
}
// Method with enum parameter
static void printDirection(____ direction) {
System.out.println("Direction: " + direction);
}
// Call method with EAST
public static void main(String[] args) {
printDirection(____);
}
}