• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program Explanation  RSS feed

 
VineetS Sharma
Greenhorn
Posts: 7
MS IE MyEclipse IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need explanation for the program written on page 118 in Head First Java .
Please help
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
548
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Maybe you could post that program for those who don't have a copy of the book? And what in particular you don't understand?
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jeanne said, if you would please post the code for us all to see. ^_^
However, I do have the book here and will just write it out. Just keep it in mind for the future that not everybody has the HeadFirst Java Book! ;)

The idea of the exercise is to be the JVM. You must decide if the code will compile and if the code compiles, what is the output? You are given 3 choices and from top to bottom I will name them answer A, B and C.

The code in question is:



And the three options are outputs that look like:

Output A: 12 14

Output B: 12 14 x = 6

Output C: 13 15 x = 6

If you are a beginner the best way to solve this is to take it STEP BY STEP. Understanding the code and writing each step down was always the hugest help for me and it pushed me a long way in understanding the logic of code.
To get you moving along the way you would work this could be like:
1. class Output //Okay this is the class declaration, piece of cake! Next!
2. public static void main( String[] args ) //Alright we have the main method! I know what this does!
3. Output o = new Output() //Okay so this means...oh yeah! This means we create an object of type Output and name it o!
4. o.go() //It looks like we use the newly created Output object named o and tell it to use the go() method... Got it!
5. And keep going like this until you've finished the code!

It will seem very tedious and you may struggle but this is a great way to learn how it all works.

Another method is the way that I did it when I opened up to this exercise.
I looked at the outputs and what I was printing with the statements System.out.print() and System.out.println(). For option A I could see the output was 12 14. Looking at the print statements I KNOW the code has to output something with a " x = " in it so Option A cannot be right!
For option B I could see that the output contains the " x = " so that might be right.
For option C I could also see that the output contains the " x = " so that might also be right.
So we need to address the values that are printed and find which are right.
Right away I checked to see what condition needed to be met for " x = " to print. The condition is that the value y must be GREATER THAN BUT NOT EQUAL TO 14. This means that y has to be 15 or greater for the " x = " to be printed.
What this tells me is that when I output y with System.out.print(++y + " ") y has to greater than 14 for the " x = " + x to print.
Looking at outputs B and C I can see B outputs 12 and 14 while C outputs 13 and 15. Since this is just y printed twice and incremented by two, the answer must be output C since it has a y that is greater than 14.

Hope this helps!
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, Welcome to the Ranch!
 
VineetS Sharma
Greenhorn
Posts: 7
MS IE MyEclipse IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure Jeanne... will keep in mind from next time
 
VineetS Sharma
Greenhorn
Posts: 7
MS IE MyEclipse IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Syke...

It is easy to understand now....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!