• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum example

 
ashni Prakash
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the code below:

code
-----------------------------------------------------------------------
import java.util.*;

public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}



public class EnumTest {
Day day;

public EnumTest(Day day) {
this.day = day;
}

public void tellItLikeItIs() {
switch (day) {
case MONDAY: System.out.println("Mondays are bad.");
break;

case FRIDAY: System.out.println("Fridays are better.");
break;

case SATURDAY:
case SUNDAY: System.out.println("Weekends are best.");
break;

default:System.out.println("Midweek days are so-so.");
break;
}
}

public static void main(String[] args) {
EnumTest firstDay = new EnumTest(Day.MONDAY);
firstDay.tellItLikeItIs();
EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
thirdDay.tellItLikeItIs();
EnumTest fifthDay = new EnumTest(Day.FRIDAY);
fifthDay.tellItLikeItIs();
EnumTest sixthDay = new EnumTest(Day.SATURDAY);
sixthDay.tellItLikeItIs();
EnumTest seventhDay = new EnumTest(Day.SUNDAY);
seventhDay.tellItLikeItIs();


}
}

-------------------------------------------------------------------
code

I am getting complie time errors.It would be nice if anyone can solve this.

Th output should be:
Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praks,

it seems to me that the following are to do with your code:

- when you declare "enum" this is like calling a class
like
if you declare public Enum outside a class then it expects that enum to be in a file of its own.
Either declare the enum with package identifier ie no public
or put the Enum within the Enum class

Hope this helps,

Niall
[ December 07, 2006: Message edited by: Niall Loughnane ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am getting complie time errors.It would be nice if anyone can solve this.



It would also be nice if you mention exactly what compile errors you get and where you get them. Explain why you cannot understand the compiler errors.

Please edit your post and replace "code" by proper code tags , then the program will be displayed with its formatting.
Thanks
[ December 07, 2006: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Niall wrote, there is nothing wrong with your code if you put your two public classes (Days, EnumTest) into their own files. But the compiler error I get actually tells you to do that: "class Day is public, should be declared in a file named Day.java"
 
ashni Prakash
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,

I figured out that to implement enum we should have JDK versions later than 1.5.x,After updating the JDK version to 1.5.x i got the program working.Thanks for your suggestions.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by praks bodduna:
hey guys,

I figured out that to implement enum we should have JDK versions later than 1.5.x,After updating the JDK version to 1.5.x i got the program working.Thanks for your suggestions.


Yes, that would help, using the 5.0 compiler. You'd better read the Sun Java document about the new features in Java 5.0, praks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic