• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Enum Question  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
I've corrected the missing codes. Thank you.Now, it's well understood.
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!