posted 6 years ago
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
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
posted 6 years ago
 1
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(00)
when x=1
1,1(10)
when x=2
2,1(21)
when x=3
3,2(31)
when x=4
4,2(42)
Which the bold part is "Y"
Hope it helps.
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(00)
when x=1
1,1(10)
when x=2
2,1(21)
when x=3
3,2(31)
when x=4
4,2(42)
Which the bold part is "Y"
Hope it helps.
Louis Merz
Greenhorn
Posts: 2
posted 6 years ago
Try taking it out, and see what you get!!!
System.out.print(x + y + " ");
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 offbyone errors
posted 6 years ago
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 yaxis, 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 yaxis value?
Rgds
Ricci
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 yaxis, 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 yaxis value?
Rgds
Ricci
Acetylsalicylic acid is aspirin. This could be handy too:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJIDEA
