I'm doing the Be the Compiler excercise, chap 3, part b (Class Hobbits). Here's the code:
The point of the exercise is to see if the code will compile. I decided to enter the code on my computer instead of just figure it out mentally (Is it okay to do that ). The code compiled fine, but when I ran it I got this:
Is the exercise still valid? Because the answer to it was that array elements need to start with 0, and this is what the answer was supposed to be:
p.s. I tried to bold and make black the int z=-1 and while (z<2) statements, but couldn't make it work. What did I miss?
I thought the exercise was to be the compiler--doesn't using a compiler kind of defeat the purpose?
posted 9 years ago
But what still concerned me was that the exercise was to determine if the code would compile -- it compiled fine the way it was written, even though it gives an error at runtime. The answer to the exercise said that the array element had to start with 0. That, however, made no difference regarding the code's compiling, only the output -- with z set to -1, it would have run fine as well, with no errors. Unless I typed something wrong that made it compile fine, which maybe wasn't supposed to happen.
While I'm here, are you familiar with the codemagnets exercise in Chap3? I got it right, but my code was this:
the answer in the book had the index array code first, and the string array code second. Does it matter? because like I said, the code compiled and ran the same either way.
If the code compiles, and produces the correct output, I'd assume it doesn't matter. When code is localized like that it's a simple matter of seeing if anything in one of the code blocks is dependent on anything in the other--I'm guessing you can answer this question yourself.
posted 9 years ago
Sorry -- I'm reading Head First Java by Kathy Sierra and Bert Bates.