Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum example" Watch "Enum example" New topic
Author

Enum example

ashni Prakash
Ranch Hand

Joined: Dec 05, 2006
Posts: 50
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

Joined: Dec 07, 2006
Posts: 208
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

Joined: Aug 03, 2002
Posts: 7729

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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Dec 05, 2006
Posts: 50
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

Joined: Aug 03, 2002
Posts: 7729
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.
 
Consider Paul's rocket mass heater.
 
subject: Enum example