• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Be the compiler excercise in chap 3

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the exercise was to be the compiler--doesn't using a compiler kind of defeat the purpose?
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True (Doh!)

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.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no idea what book you're talking about.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry -- I'm reading Head First Java by Kathy Sierra and Bert Bates.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes agreed. Just to answer the question in a straight forward manner, "Yes".

Compiling and running are two different phases and better don't try to mix them up!

The successfully compiled code may blow up at run time . so don't bother.
 
reply
    Bookmark Topic Watch Topic
  • New Topic