• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Enum Question

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
This is a question from master exam accompanying Bert holy bible book.

Given,
Class Stepper {
Enum Roman {I,V,X,L,C,M}
public static void main(String...bang){
int x =7;
int z =2;
Roman r=Roman.x;
do{
Switch(r){
case C:r=Roman.L; break;
case X:r=Roman.C;
case L:if (r.ordinal)>2z+=5;
case M:x++;
}z++;
}
While (x>10);
System.out.println(Z);
}
}

What is the result?
a 5
b v
c 18
d 21
e 22
f compilation fails



But,i think the answer should be compilation fails. Because, not all the alphabelts in Enum Roman block were used in Switch block(case)
Only case C,X,L,M were used.

Please, i want someone to explain with reasons why it is not compilation fails.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lao Kinsuyi:
...i think the answer should be compilation fails...


Have you tested this code?

There are 7 errors (typos) that will prevent this from compiling. Once you fix these, try experimenting with values that are not represented as cases (for example, Roman.V).
[ July 21, 2007: Message edited by: marc weber ]
 
Lao Kinsuyi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, someone should explain this thread(enum question)
Over to you Chandra Bhatt for your usual effort.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Lao

There are lot of typos in the program and also the correct output 8 is not list, so it leads to mark for compilation fails but was the program really testing us to spot typos. If it is then I guess it is really a poor example.
Anyways i have corrected the program

You can manually find out the difference.

Regards
Padma
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try to explain...



In line 6, "r" is set to value X. In line 10,Case X is executed, which sets "r" to C.Since, there is no break statement, all other cases are also executed.In line 11, as "r.ordinal()" returns 5, z is incremented to 7.
In line 14, z is again incremented by 1. value of z is now 8. In line 16, the while condition is evaluated and found to be false.So, the execution comes out of the loop and value of z is printed as 8.

Somebody please correct me if I'm wrong.
 
Lao Kinsuyi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've corrected the missing codes. Thank you.Now, it's well understood.
 
Have you no shame? Have you no decency? Have you no tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic