ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Java find output programs (Enumeration) | set 2

Find the output of Java programs | Enumeration | Set 2: 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 Vals[] = Week.values();

    for (Week w: Vals) {
      System.out.println(w + " at " + w.ordinal());
    }
  }
}

Output:

Sun at 0
Mon at 1
Tue at 2
Wed at 3
Thu at 4
Fri at 5
Sat at 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.

In the main() method we created an array of Week enum, which is initialized with values of enum and then we accessed each value of Week enum using a foreach loop and printed on the console screen.

Question 2:

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

  public static void main(String[] args) {
    Week Vals[] = Week.values();

    for (Week w: Vals) {
      System.out.println(w + " at " + w.ordinal());
    }
  }
}

Output:

/EnumEx.java:3: error: ',', '}', or ';' expected
    Sun = 101,
        ^
/EnumEx.java:3: error: '}' expected
    Sun = 101,
         ^
/EnumEx.java:4: error: <identifier> expected
    Mon,
       ^
/EnumEx.java:9: error: ';' expected
    Sat
       ^
/EnumEx.java:12: error: class, interface, or enum expected
  public static void main(String[] args) {
                ^
/EnumEx.java:15: error: class, interface, or enum expected
    for (Week w: Vals) {
    ^
/EnumEx.java:17: error: class, interface, or enum expected
    }
    ^
7 errors

Explanation:

The above program will generate syntax error because we cannot assign value to the enum constants in Java.

Question 3:

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

  public static void main(String[] args) {
    Week Vals[] = Week.values();
    int index = 0;
    for (Week w: Vals) {
      index = w.ordinal();
      System.out.println(w + " at " + index);
    }
  }
}

Output:

/EnumEx.java:2: error: modifier final not allowed here
  final enum Week {
        ^
1 error

Explanation:

The program will generate syntax error because we cannot declare enumerate with the final keyword in Java.

Question 4:

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

  public static void main(String[] args) {
    Week Vals[] = Week.values();
    int index = 0;
    for (Week w: Vals) {
      index = w.ordinal();
      System.out.println(w + " at " + index);
    }
  }
}

Output:

Sun at 0
Mon at 1
Tue at 2
Wed at 3
Thu at 4
Fri at 5
Sat at 6

Explanation:

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

In the main() method we created an array of Week enum, which is initialized with values of enum and then we accessed each value and indices of Week enum using the foreach loop and printed on the console screen.

Question 5:

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

  public static void main(String[] args) {
    Week Vals[] = Week.values();

    switch (Vals[3].ordinal()) {
    case 0:
      System.out.println("Sunday");
      break;
    case 1:
      System.out.println("Monday");
      break;
    case 2:
      System.out.println("Tuesday");
      break;
    case 3:
      System.out.println("Wednesday");
      break;
    case 4:
      System.out.println("Thrusday");
      break;
    case 5:
      System.out.println("Friday");
      break;
    case 6:
      System.out.println("Saturday");
      break;
    }
  }
}

Output:

Wednesday

Explanation:

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

In the main() method we created an array of Week enum, which is initialized with values of enum and then we defined a switch-case block and defined case according to indices.

Vals[3].ordinal()

The above statement will return 3 then case 3 will execute and print "Wednesday" on the console screen.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.