Java:Language - Enums - Exercises

From Juneday education
Jump to: navigation, search

Work in progress - Exercises on enums


Q 1

Create a public enum Weekday with constants for MONDAY, TUESDAY,... until SUNDAY.

The enum should have an instance method boolean isWeekDay() and an instance method boolean isHoliday().

The isHoliday() method should return the opposite of isWeekDay().

Write a program which demonstrates how this enum could be used, which has a method which takes a Weekday as the argument and prints a message depending on whether the Weekday is a holiday or not.

We suggest that the main method loops over all values in the Weekday enum and sends them as argument to the method.

Hint: every enum in Java has a static values() method, which returns an array of the values in the enum, so you may use a for-each-loop (the enhanced for loop) for this.

Hint: every enum has a toString() implementation which return the constant name as it was declared in the enum, e.g. "FRIDAY".

Expand using link to the right to see a suggested solution/answer.

public class WeekdayDemo {

  public static void main(String[] args) {
    for (Weekday day : Weekday.values()) {
  private static void testDay(Weekday day) {
    System.out.print("It's " + day);
    if (day.isWeekday()) {
      System.out.println(" Go to work.");
    } else {
      System.out.println(" Stay home and play with your cat.");

public enum Weekday {

  public boolean isWeekday() {
    return this != SATURDAY && this != SUNDAY;

  public boolean isHoliday() {
    return !isWeekday();

// Compile and run:
// javac && java WeekdayDemo

Q 2

Use your weekday class to investigate if enums implement Comparable.

Declare a Weekday sat = Weekday.SATURDAY;.

Use a loop over Weekday.values() with day as loop variable and print out each value and whether it is less than, equal to or greater than sat, using the call day.compareTo(sat) - remember that the compareTo() method returns an int such that

  • a negative value means that day is less than sat
  • zero means that day is considered equal to sat (when comparing them on order)
  • a positive value means that day is greater than sat

Expand using link to the right to see a suggested solution/answer.

It is true that all enums implement Comparable, otherwise the call to compareTo would fail.

The natural order of the constants in the enum corresponds to the lexical order in which you declared them in the source code.

Internally, the compareTo() uses the ordinal() method, which returns a number starting from 0, incremented by one per declared constant, also in the same order as they were declared in the source.

  private static void whichIsGreater(Weekday day) {
    Weekday sat = Weekday.SATURDAY;
    int result = day.compareTo(sat);
    System.out.print(day + " is ");
    if (result < 0) {
      System.out.println("less than (before) " + sat);
    } else if (result ==  0) {
      System.out.println("the same as " + sat);
    } else {
      System.out.println("greater than (after) " + sat);


Source code

Where to go next

Previous page is the Enums chapter with examples and videos. Next is not decided yet. TOC is the More programming with Java page, the section on Java Language.

« PreviousBook TOCNext »