Java find output programs (Enumeration) | set 1

Find the output of Java programs | Enumeration | Set 1: Enhance the knowledge of Java Enumeration concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 04, 2021

Question 1:

public class EnumEx {
  enum Week {
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    Week W1 = Week.Tue;
    Week W2 = Week.Thu;

    int W = 0;

    W = W1 + W2;

    System.out.println("W: " + W);
  }
}

Output:

/EnumEx.java:18: error: bad operand types for binary operator '+'
    W = W1 + W2;
           ^
  first type:  Week
  second type: Week
1 error

Explanation:

The above program will generate syntax error because we cannot add two enumeration operands using plus + operator directly.

Question 2:

public class EnumEx {
  enum Week {
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    Week W1 = Week.Tue;
    Week W2 = Week.Thu;

    int W = 0;

    W = (int) W1 + (int) W2;

    System.out.println("W: " + W);
  }
}

Output:

/EnumEx.java:18: error: incompatible types: Week cannot be converted to int
    W = (int) W1 + (int) W2;
              ^
/EnumEx.java:18: error: incompatible types: Week cannot be converted to int
    W = (int) W1 + (int) W2;
                         ^
2 errors

Explanation:

The above program will generate syntax error because enumeration Week does not contain integer values.

Question 3:

public class EnumEx {
  enum Week {
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    Week W1 = Week.Tue;
    Week W2 = Week.Thu;

    String W = "";

    W = W1.toString() + W2.toString();

    System.out.println(W);
  }
}

Output:

TueThu

Explanation:

In the above program, we created a class EnumEx that contains a user define enumeration Week and a main() method. The Week enumeration contains 7-week values.

Now look to the main() method, here we created two enum variables W1 and W2 that are initialized with Week. Tue and Week.Thu respectively,

W = W1.toString() + W2.toString();

In the above statement, we converted the values of W1 and W2 into the string and then concatenating them and assigned them to the string W and then print W on the console screen.

Question 4:

public class EnumEx {
  enum Week {
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    System.out.println(Week.valueOf("Mon"));
    System.out.println(Week.valueOf("Fri"));
  }
}

Output:

Mon
Fri

Explanation:

In the above program, we created a class EnumEx that contains a user define enumeration Week and a main() method. The Week enumeration contains 7-week values.

System.out.println(Week.valueOf("Mon"));
System.out.println(Week.valueOf("Fri"));

In the above statements, we printed the value of specified enum constant on the console screen.

Question 5:

public class EnumEx {
  enum Week {
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    System.out.println(Week.Wed.ordinal());
    System.out.println(Week.Sat.ordinal());
  }
}

Output:

3
6

Explanation:

In the above program, we created a class EnumEx that contains a user define enumeration Week and a main() method. The Week enumeration contains 7-week values.

System.out.println(Week.Wed.ordinal());
System.out.println(Week.Sat.ordinal());

In the above statements, we printed the value of the index of enum constants.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.