This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
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 ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Lao Kinsuyi
Ranch Hand
Joined: Mar 22, 2007
Posts: 30
posted
0
Please, someone should explain this thread(enum question) Over to you Chandra Bhatt for your usual effort.
Padma Asrani
Ranch Hand
Joined: Mar 22, 2007
Posts: 111
posted
0
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
ram shah
Greenhorn
Joined: Jun 02, 2007
Posts: 28
posted
0
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
Joined: Mar 22, 2007
Posts: 30
posted
0
I've corrected the missing codes. Thank you.Now, it's well understood.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.