programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Having trouble with Chapter 1 in Head First Java

Seb Smith
Greenhorn
Posts: 4
I was doing alright with the beginning exercises I got to the end of chapter 1 I struggled with most of the exercises so I decided to reread and try again. I'm still having trouble completing most of them. Is there any tips you guys can give to help me through this?

Also, I could do this exercise but I need a little bit of an explanation.. I think i'm just over thinking it but I want to be sure that I fully grasp it

I don't really know how to phrase my other question but im a little confused at the x = x + 1 part
it says that the int x = 1 so i dont get how 1 = 1 + 1

can someone explain this code to me so that I can understand it better.

Joe Harry
Ranch Hand
Posts: 10128
3
you should interpret it as follows: Add the old value of x which is 1, with 1 and reassign it back to x. So effectively it is x = 1 + 1. It is just the very basic way that you do math.

Seb Smith
Greenhorn
Posts: 4
Joe Harry wrote:You should interpret it as follows: Add the old value of x which is 1, with 1 and reassign it back to x. So effectively it is x = 1 + 1. It is just the very basic way that you do math.

oh okay just to make sure i understand

correct?

is there any tips you can give me to get through the end of chapter one? I want to be able to do most of these exercises before advancing

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
= means assignment, it's not an equation like in mathematics. So, x = x + 1 means: set x to the current value of x plus one.

Seb Smith
Greenhorn
Posts: 4
Jesper de Jong wrote:= means assignment, it's not an equation like in mathematics. So, x = x + 1 means: set x to the current value of x plus one.

oh okay thats why i was so confused, so my post above you is correct then (the one with the note)?

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Yes. Translated to English, the program does this:

line 3: create a variable x and set it to 1
line 4: check if x is less than 3, if yes then run the loop (it is, so it continues into the loop)
line 5: print "Doo"
line 6: print "Bee"
line 7: set x to the value of x plus one: since x is currently 1, this sets x to 2
line 9: check if x is equal to 3; it is not, so line 10 is skipped
line 12: end of the while loop, it jumps back to line 4
line 4: check if x is less than 3, if yes then run the loop (it is, so it continues into the loop)
...
line 7: set x to the value of x plus one: since x is currently 2, this sets x to 3
line 9: check if x is equal to 3; it is, so line 10 is executed
line 10: print "Do"
line 12: end of the while loop, it jumps back to line 4
line 4: check if x is less than 3, it is not so it skips the loop
line 13: end of the program

Seb Smith
Greenhorn
Posts: 4
Jesper de Jong wrote:Yes. Translated to English, the program does this:

line 3: create a variable x and set it to 1
line 4: check if x is less than 3, if yes then run the loop (it is, so it continues into the loop)
line 5: print "Doo"
line 6: print "Bee"
line 7: set x to the value of x plus one: since x is currently 1, this sets x to 2
line 9: check if x is equal to 3; it is not, so line 10 is skipped
line 12: end of the while loop, it jumps back to line 4
line 4: check if x is less than 3, if yes then run the loop (it is, so it continues into the loop)
...
line 7: set x to the value of x plus one: since x is currently 2, this sets x to 3
line 9: check if x is equal to 3; it is, so line 10 is executed
line 10: print "Do"
line 12: end of the while loop, it jumps back to line 4
line 4: check if x is less than 3, it is not so it skips the loop
line 13: end of the program

thanks for explaining it very well. is there any tips you could give me to help me complete the exercises at the end of chapter 1 of head first java?

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
I don't have that book so I can't give you specific recommendations, but in general: when you want to understand a program, walk through it line by line, as if you're running the program in your head, keeping track of the values of variables; make sure you understand what each line is doing.

Winston Gutkowski
Bartender
Posts: 10575
66
Seb Smith wrote:oh okay just to make sure i understand...

Seb,