aspose file tools*
The moose likes Beginning Java and the fly likes Be the compiler excercise in chap 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Be the compiler excercise in chap 3" Watch "Be the compiler excercise in chap 3" New topic
Author

Be the compiler excercise in chap 3

Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
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?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I thought the exercise was to be the compiler--doesn't using a compiler kind of defeat the purpose?
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

I have no idea what book you're talking about.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 25, 2009
Posts: 85
Sorry -- I'm reading Head First Java by Kathy Sierra and Bert Bates.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Be the compiler excercise in chap 3