• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having trouble with Chapter 1 in Head First Java  RSS feed

 
Seb Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
= 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seb Smith wrote:oh okay just to make sure i understand...

Seb,

Please DontWriteLongLines. They make your post very hard to read. I've broken yours up this time.

Thanks. And welcome to JavaRanch.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!