• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

head first java - mixed messages

 
j patrick
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi im having problems understanding the second mixed message problem in chapter 5, can anyone help, this is the code

class MixFor5 {
public static void main(String[]args) {
int x = 0;
int y = 30;
for(int outer =0; outer<3; outer++) {
for(int inner =4; inner >1; inner--) {
 
Rodrigo Tomita
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think your code got incomplete to the forum. May I suggest that you also use the [CODE] button when posting it? This will keep the formatting.

Also... what exactly you haven't understood?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you seem to also be missing some of the code...
 
j patrick
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi im having problems understanding the second mixed message problem in chapter 5, can anyone help, this is the code



I can work out the outputs for most of the questions except x = x+6; and x = x + 0; The affect of break command has thrown me, if anyone is familiar with this chapter and the associated problems, some help would be appreciated.

cheers

[edit]Added code tags[/edit]
[ June 05, 2008: Message edited by: Campbell Ritchie ]
 
j patrick
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about the code, havent got the hang of posting yet

cheers
 
Rodrigo Tomita
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In this link you can have the explanation of the break statement. Basically (in your case) when x == 6, it will quit looping the inner for.

So, it does 9 times the below statements:
x = x + 6; // or x = x + 0
y = y - 2;

and sometimes (when x != 6) it does x = x + 3;

Hope it helps.
 
j patrick
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply, i still cant do it tho lol. For x = x+6; the text answer is x = 60 y = 10, i cant seem to come to these values. I might just go to the next chapter.

Cheers for the help anyway.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure i understand the question, but one thing you can try is to put some System.out.println statements in the code, and run it. i'd suggest right after you change any variable, print it out. for starters:

 
j patrick
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback, ill try your suggestion.
 
Larry Dillions
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me correct this question for you...I copied this exactly same down from Head First Java 2nd edition, chapter five on page 121. I don't know how it work with the loop of 2 for....Can anyone explain how it work please and thank you.

A short Java program is listed below. One block of the program is missing. Your challenge is to match the candidate block of code (on the left), with the output that you'd see if the block were inserted. Not all the lines of output will be used, and some of the lines of output might be used more than once. Draw lines connecting the candidate blocks of code with their matching command-line output.



match each candidate with one of the possible outputs


Candidates: Possible output:

x = x + 3; 45 6

x = x + 6; 36 6

x = x + 2; 54 6

x++; 60 10

x--; 18 6

x = x + 0; 6 14

12 14

Solution:

x = x + 3; ---------- 54 6

x = x + 6; ---------- 60 10

x = x + 2; ---------- 45 6

x++; ---------- 36 6

x--; ---------- 18 6

x = x + 0; ---------- 6 14

12 14

[edit]Added code tags, minor spelling correction. CR[/edit]
[ June 05, 2008: Message edited by: Campbell Ritchie ]
 
Larry Dillions
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

match each candidate with one of the possible outputs


Candidates: --------- Possible output:

x = x + 3; ------------ 45 6

x = x + 6; ------------ 36 6

x = x + 2; ------------ 54 6

x++; ------------------ 60 10

x--; ------------------ 18 6

x = x + 0; ------------ 6 14

----------------------- 12 14

Solution:

x = x + 3; ---------- 54 6

x = x + 6; ---------- 60 10

x = x + 2; ---------- 45 6

x++; ---------------- 36 6

x--; ---------------- 18 6

x = x + 0; ---------- 6 14

---------------------- 12 14
 
Campbell Ritchie
Sheriff
Pie
Posts: 49805
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rodrigo Tomita has told you the solution.

To elaborate: The outer loop runs 3 times and the inner loop runs 3 times. The "break" keyword terminates the inner loop only, if x has the value of 6. But if the outer loop hasn't run 3 times, it will restart the inner loop . . . until x is equal to 6 again, if ever. If you put the same starting value in the outer loop, then x might be 6 again.

It is an exercise is working out the indices and values in for loops. You can work it out with a pencil and paper, writing the values of x and y after each run of the loops, and whether the inner loop will complete normally.

If you go through JavaRanch you will find this. Some people think it is a bit extreme, but note that 3.1 suggests we ought to avoid "break." You can see that using "break" can make coding hard to understand. I would suggest you also (wherever possible) start a for-loop with 0 and use < just as in the "outer" loop; this is the conventional format and, once you have got used to it, is easier to understand.

Please, both, use code tags round quoted code; I have added them to those posts where it actually helps, so you can see how it improves illegibility. Please avoid abbreviations like "tho" and "lol"; although their meaning is pretty obvious to those of us who grew up with English, it may be obscure to Ranchers who learned English at school or are using translation software. You can mimic "lol" with one of the "graemlins" at the bottom left.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49805
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by myself:
Rodrigo Tomita has told you the solution.
But you need to remember that after a "break" in the inner loop, the inner loop does not restart. The outer loop continues to run, and this starts a new inner loop.

You cannot be confident that the inner loop will run 9 times.
 
Larry Dillions
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain the step by step of working table or calculation?

like this...for x = x + 3;

________inner_________outer
Loop=__4__3__2__then 1__2__3

x =____6__12_18______18_18_18

y =____28_26_24______22_20_18

My answer are totally wrong that x = 18 and
y = 18. It should be x = 54 and y = 6.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49805
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go back to the code you posted earlier. Just before where you wrote "candidate code" add a print statement, something like this:
System.out.printf("Start inner loop: Outer = %d, inner = %d, x = %d, y = %d%n", outer, inner, x, y);
and similar lines after "y = y - 2;" and "x = x + 3;"

You will now get a printout showing the execution of the code; see whether that helps work out the answers.

Show us what you get, please, and be sure to ask again.

Remember, it is not inner 4..3..2 outer 1..2..3.
It is (or more precisely might be) outer 1.. inner 4..3..2 outer 2.. inner 4..3..2..etc.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49805
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by myself:
It is (or more precisely might be) outer 1.. inner 4..3..2 outer 2.. inner 4..3..2..etc.


No, that's not right. It should read:

Outer 0.. inner 4..3..2 outer 1.. inner 4..3..2..etc.
 
Larry Dillions
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally I have worked it out. I understand how this loop work now.

...4...3...2....4...3...2....4...3...2
------------------------------------------
x..6...12..18...24..30..36...42..48..56
------------------------------------------
y..26..24..22...18..16..14...10..8...6


Thank you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49805
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jak hurley:
I understand how this loop work now.
Thank you.
Well done

 
Ryan McNeely
Greenhorn
Posts: 1
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All,
I'm struggling with p 121 just like our original inquirer. It's the same code as above. I've figured them all out except for the insertion code: "x = x + 0"
Obviously I'm getting to know the nature of "break;". I also appreciate that there's a philosophy that says don't use breaks in loops. But I'm learning a lot from this problem anyway.

..........0...............1..................2
.....4...3...2......4...3...2......4...3...2
x...3...6...........3...6...........3...6....
y...28.26.........22.20.........16.14....
.........24.............18..............12.........

I include the last line here because the inner loop breaks at 2nd iteration of inner loop, bringing me to "y=y-2" in the outer loop.

I keep ending with x=6, y=12
The book says it should be x=6, y=14

life is green
Ryan
 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope it's OK to revive this old thread.

I'm having trouble keeping track of the variables as I try to work through this mentally/manually. I finally resorted to using pen and a paper and it's not helping much. What I've tried is making four columns. One each for x, y, outer, and inner and then adding a row indicating what the variable value is for each iteration. I'm still getting lost. I have some experience with nested loops but usually just the iterator values to keep track of and not manually.
Of course I could just plug in the values, run the program, and get the answer but that seems like cheating. Any other tips on how to work through this manually?

Gary
 
Campbell Ritchie
Sheriff
Pie
Posts: 49805
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try a few print statements. Scatter this sort of thing liberally through the code:I like to put those test comments, so I can find test lines and delete them later using ctrl-F.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think once you've given your best shot at working through it by hand, it's not "cheating" to add printlns or use a debugger to observe what's actually happening. I've been doing this stuff professionally for 20 years and I do that stuff all the time.

It's good to try to work it out yourself, but if you get stuck, taking a peek at what's going on is fine. Once you see it, it's probably worth going back and trying to step through it by hand again to make sure the lesson has taken root.
 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank-you for your responses. I appreciate the input.

Gary
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic