This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum Question" Watch "Enum Question" New topic
Author

Enum Question

Deepthi Kanakam Rajan
Greenhorn

Joined: May 15, 2006
Posts: 13
Can someone please explain how the following code works:

class Stepper
{
enum Roman{I,V,X,L,C,M}

public static void main(String args[])
{
int x=7;
int z=2;

Roman t = Roman.X;

do
{

switch(t)
{
case C: Roman.L;break;
case X: t= Roman.C;
case L: if(t.ordinal()>2) z+=5;
case M: x++;
}
z++;
}while(x<10);
System.out.println(z);
}
}

how does the ordinal() work?

Thanks

Deepthi Rajan
Jon Lee
Ranch Hand

Joined: Mar 04, 2005
Posts: 134
First, the code you posted does not compile. The correct code should be



ordinal - - The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is assigned an ordinal of zero).

Roman.I.ordinal()=0
... ...
Roman.M.ordinal()=5
[ August 17, 2006: Message edited by: Jon Lee ]

SCJP 5.0 - 98% (2007)<br />SCWCD 1.4 - 97% (2007)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Jon has already quoted from the Enum API:
ordinal

public final int ordinal()

Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap.

Returns:
the ordinal of this enumeration constant



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

Joined: Aug 17, 2006
Posts: 36

Your code have some issue. I have to modified the line below

case C: Roman.L;break;

to avoid compilation error by adding "t =" statement

case C: t = Roman.L;break;

After that I compiled and ran the code. I get an output 21.

Now, your question is about ordinal(). I cut and paste a definition of the ordinal method
(see below) from java.sun.com/j2se/1.5.0/docs/api

"Returns the ordinal of this enumeration constant (its position in its enum declaration,
where the initial constant is assigned an ordinal of zero)."

I hope I explain this correctly. Here goes...


In your code the ordinal sequence is
I = 0
V = 1
X = 2
L = 3
C = 4
M = 5


To test out this theory, I inserted a statement "System.out.println(t.ordinal());" after
"Roman t = Roman.X;" statement. The output is 2, because Roman.X ordinal (order sequence)
is 2 (see ordinal sequence above). This verifies that the theory is correct.

Next, I ran a debugger and monitor the while loop. The statement "case L: if(t.ordinal()>2) z+=5;"
iterated through the while loop three times. It does this because of the x<10 condition.

[The 1st pass] t.ordinal() = 4 because t = Roman.C, z = 10, and x = 8
[The 2nd pass] t.ordinal() = 3 because t = Roman.L then break out, Z = 15, and x = 9
[The 3rd pass] t.ordinal() = 3 because t = Roman.L then break out, z = 20, and x = 10

Your while(x<10) condition becomes false because x = 10.

System.out.println(z); // output 21

the statement z++ --> z = z + 1 --> z = 20 + 1


r/
Jay
Deepthi Kanakam Rajan
Greenhorn

Joined: May 15, 2006
Posts: 13
Thanks guys.i understood how the code works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum Question
 
Similar Threads
please explain - code output
Whats the output...
Problem with a mock exam question
mock question
Switch with Enum (Was: Explain the output)