I'm reading Head First Java 2e to try and pass my first Java class at school. It's an 8-week course and they went from writing "Hello World" to the screen on the first day, to a 13 file (class) packaged program that was due the second week. I'm lost, and it's been 13 years since I had C++ OOP. The exercise code on page 90 of Head First Java has me confused and there's only answers, not explanations. Could someone perhaps explain how "maybeNew", "index", "return 1", etc. work with the while loop to get the "Possible outputs" they have listed? I'm brand new to online discussion groups, and this very message is my first attempt to not look more stupid than I am. Many Thanks.
We hope you're enjoying the book! The first question I have to ask is, are you typing in the code and trying it yourself? If so, you can just go down the list of "candidates", put them in the missing places, and see what output you get. If you're having trouble understanding how the code is working, I'd recommend two things: 1 - add a couple of System.out.println() statements to the program - this is a common debugging technique for small programs like this. Whenever you get to a point of the program where you're not sure what's going on, add one of these println methods to print out the current values of the variables you're studying. 2 - go thru the code with a pencil and paper, and manually keep track of the values of the variables as you go through the iterations of the while loop - this is tedious for sure, but once you've done it a couple of times you'll start to see how things are working.
Finally, give us some more details about what you think you understand about the program, and more precisely where your questions are! It's valuable for you to post questions, not just for yourself, but because youever takes a whack at answering them will definitely learn in the process of writing an answer.
Let us know your progress, and have fun!
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Joined: Nov 11, 2005
Greetings Burt, I am enjoying the book very much, and I'm Still chuckling over the caption for the photo on page 80. I�m doing both; typing in the code to see the output, and looking at the code to try and understand how it�s working with pencil and paper. I do fairly well when tracking variables like �counter� and �x�, when they get used and incremented, but it got a little hazy for the �maybeNew� part. I got it into my thick head that �Mix4 m4 = new Mix4 ( );� was a new object and not a new array. But the �counter + 1� for m4 and �return 1� threw me off. I�m not clear on how two �counters� being incremented is affecting the program, on where �return 1� is returned to, and I need to refresh my memory on what the println is concatenating with the � �.
On page 83 can the statements in step 1 be combined as �Dog [ ] pets = new Dog [ 7 ];�? If so, would making them separate allow for a better programming practice? Just curious. I like the Code Magnets and matching type exercises, but the Pool Puzzles I find quite challenging with all the choices. I�ll keep trying, as it�s just my old age gettin in the way.
Thank you so much for providing an excellent recommendation. Putting System.out.println() statements in the program to print out variables is just the ticket I needed, and I can�t wait to try it out. I sincerely appreciate you taking the time to reply to me. I need all the help I can get, since these 8-week courses at this college are brutal, and some feel Java should be split into two classes. I think it should only be taught in 16-week classes at state colleges myself, but what do I know.
Many thanks & kind regards, Nigel
Joined: Nov 11, 2005
My sincere apologies. Within a half second I noticed your name is spelt Bert. I�m truly sorry.