w3resource

Java enum Types - Exercises, Practice, Solution

Java Enum Exercises

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week. Because they are constants, the names of an enum type's fields are in uppercase letters

1. Write a Java program to create an enum called "DaysOfWeek" representing the days of the week.

Sample Solution:

Java Code:

public class Main {
  public enum Days_Of_Week {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
  }
  public static void main(String[] args) {
    Days_Of_Week today = Days_Of_Week.MONDAY;
    System.out.println("Today is " + today);
  }
}

Sample Output:

Today is MONDAY

Explanation:

The above program defines an enum called Days_Of_Week with seven values representing each day of the week. Inside the main method, we create a variable today of type Days_Of_Weekand assign it the value MONDAY. Finally, we print the value of today using System.out.println(). Modify and use this enum as per your requirements.

2. Write a Java program to create an enum called "Weekend" with constants representing the days of the weekend.

Sample Solution:

Java Code:

public class Main {
  public enum Weekend {
    SATURDAY,
    SUNDAY
  }

  public static void main(String[] args) {
    Weekend day1 = Weekend.SATURDAY;
    Weekend day2 = Weekend.SUNDAY;

    System.out.println("First day of the weekend: " + day1);
    System.out.println("Second day of the weekend: " + day2);
  }
}

Sample Output:

First day of the weekend: SATURDAY
Second day of the weekend: SUNDAY

Explanation:

In the above exercise, we define an enum called Weekend with two values representing the weekend days, SATURDAY and SUNDAY. Inside the main method, we create two variables, day1 and day2, both of type Weekend, and assign them the respective values of SATURDAY and SUNDAY. Finally, we print day1 and day2 values using System.out.println ().

3. Write a Java program to implement an enum called "Shape" with constants representing different geometric shapes.

Sample Solution:

Java Code:

public class Main {
  public enum Shape {
    SQUARE,
    CIRCLE,
    RECTANGLE,
    TRIANGLE,
    HEXAGON
  }

  public static void main(String[] args) {
    Shape shape1 = Shape.CIRCLE;
    Shape shape2 = Shape.SQUARE;

    System.out.println("First shape: " + shape1);
    System.out.println("Second shape: " + shape2);
  }
}

Sample Output:

First shape: CIRCLE
Second shape: SQUARE

Explanation:

We implement an enum called Shape with five values representing geometric shapes: CIRCLE, SQUARE, TRIANGLE, RECTANGLE, and HEXAGON. We create two variables, shape1 and shape2, both of type Shape, and assign them the values CIRCLE and SQUARE. Finally, we print the values of shape1 and shape2 using System.out.println().

4. Write a Java program to implement an enum called "Direction" with constants representing the cardinal directions (North, South, East, West).

Sample Solution:

Java Code:

public class Main {
  public enum Direction {
    NORTH,
    SOUTH,
    EAST,
    WEST
  }

  public static void main(String[] args) {
    Direction direction1 = Direction.NORTH;
    Direction direction2 = Direction.WEST;

    System.out.println("First direction: " + direction1);
    System.out.println("Second direction: " + direction2);
  }
}

Sample Output:

First direction: NORTH
Second direction: WEST

Explanation:

The above program implements an enum called Direction with four values representing the cardinal directions: NORTH, SOUTH, EAST, and WEST. Inside the main method, we create two variables, direction1 and direction2, both of type Direction, and assign them the values NORTH and WEST. Finally, we print the values of direction1 and direction2 using System.out.println().

5. Write a Java program that defines an enum called "Months" with constants representing the months of the year.

Sample Solution:

Java Code:

 public class Main {
   public enum Months {
     JANUARY,
     FEBRUARY,
     MARCH,
     APRIL,
     MAY,
     JUNE,
     JULY,
     AUGUST,
     SEPTEMBER,
     OCTOBER,
     NOVEMBER,
     DECEMBER
   }

   public static void main(String[] args) {
     Months currentMonth = Months.MAY;
     System.out.println("Current month: " + currentMonth);
   }
 }

Sample Output:

Current month: MAY

Explanation:

The above program defines an enum called Months with twelve values representing the months of the year: JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, and DECEMBER. Inside the main method, we create a variable currentMonth of type Months and assign it the value MAY. Finally, we print currentMonth's value using System.out.println().

More to Come !

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.