File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Head First Java exercise problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java exercise problem" Watch "Head First Java exercise problem" New topic
Author

Head First Java exercise problem

Louis Merz
Greenhorn

Joined: Jan 09, 2012
Posts: 2
Hello everyone,

Lately I've been trying to learn Java with the Head First Java book. It seems like a good book so far except it's frustrating because the book doesn't explain how the code works in the exercises at the end of the chapters. They give the solutions to the exercises but no explanation.

Any help understanding how this code gets the following output would be great, thanks!



Program Output: 00 11 21 32 42



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

What part don't you understand? What output would you expect?
Ian Paul Budiman
Greenhorn

Joined: Jan 05, 2012
Posts: 7
Hi Louise,

I am a newbie also, but the logic solution for that particular is:

You will print out value X, Value Y from X=0, X=1, X=2, X=3, X=4

Which the format would be: "System.out.print(x + "" + y +" "); "

As you know the given parameter is, x = x + 1 and y = x - y

So, it will give you result:

when x=0
0,0(0-0)


when x=1
1,1(1-0)


when x=2
2,1(2-1)

when x=3
3,2(3-1)

when x=4
4,2(4-2)

Which the bold part is "Y"

Hope it helps.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4246
    
    7

And welcome to The Ranch, Louis!
Louis Merz
Greenhorn

Joined: Jan 09, 2012
Posts: 2

Thanks Ian, that makes a lot more sense.

Also I'm not sure what the "" in the "System.out.print(x + "" + y + " "); " means. Does it simply mean the program will print two numbers at a time?


And welcome to The Ranch, Louis!


Thanks Matthew, I'm glad to be here!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

Louis Merz wrote:Also I'm not sure what the "" in the "System.out.print(x + "" + y + " "); " means. Does it simply mean the program will print two numbers at a time?

Try taking it out, and see what you get!!!

System.out.print(x + y + " ");


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ricci Sithole
Greenhorn

Joined: Feb 10, 2012
Posts: 2

when x=0
0,0(0-0)


when x=1
1,1(1-0)


when x=2
2,1(2-1)

when x=3
3,2(3-1)

when x=4
4,2(4-2)

Could you kindly expand on these equations - I cannot seem to find how you get your y-axis values - what's the formula that you are using?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1469
    
    5

Ricci Sithole wrote:I cannot seem to find how you get your y-axis values - what's the formula that you are using?

You might find it yourself after doing paperwork.
New value of y = current value of x - current value of y.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Ricci Sithole
Greenhorn

Joined: Feb 10, 2012
Posts: 2

Anayonkar Shivalkar wrote:
New value of y = current value of x - current value of y.

Thank you so much Anayonkar - it makes sense now.

The second part of this question has this piece of code in place of (y = x - y)

code:
x = x + 1
y = y + x
with outcome expected to be: 11 34 59

Am I correct in saying that, to work out the y-axis, you'd first need to add +1 to the value of x, before you can plug in that value into (y = y + x) to get your corresponding y-axis value?

Rgds
Ricci
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Welcome to the Ranch

I don’t think those values are axes, but simply x and y. I think the bit about "" previously might be a misprint and should have read " ", which means you get 1 1 instead of 11. I don’t have my copy of the book to hand at present.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java exercise problem
 
Similar Threads
Probably simple code needed translated into English for me
Running a thread's run() method after the thread has completed.
MultiDimenaional Array sort
Head First Java - Can't understand the output
Simple 'for' loop question