Forums Register Login

HFJ Mix4 exercise

+Pie Number of slices to send: Send
I wanted also ask about return 1 and return 0, good that I've seen this topic here. I am at page 122 in Head First! book. There is an exercise where we have this example:



My question is whats return value in for example 1st, 2nd, 3rd loop (for index = 0). And why is there return 1 and 0. Am I thinking right that when index value is bigger than 4 then return 0 = break?

And for:

1st m4.number = 1; (index = 0 )
2nd m4.number = 2; ( index = 1)
3rd m4.number = 3; (index = 2)

Sorry for little chaos in mine question, but I'm a bit confused with this return 1 and 0 values.
+Pie Number of slices to send: Send
I may just completely misunderstand your question.
This code snippet on its own does not really make that much sense, no matter what you set variable "m4" to it goes out of scope at line 7.
A class of Mix4 is created and a public member is set to some number. Either way this method will return the actual integer 1 or 0.

When you reach the return method the code will stop at that point, and return back to the code that invoked it (called it).
+Pie Number of slices to send: Send
Hello Henrik. thanks for stopping by and answering. Since I can't edit my latest post Ill post here all code. After Ive checked it in Eclipse Ive figured out what was my exact point.



Sooo...I didnt know what return 1 and return 0 do here. I say what Ive came to and please correct me if I'm wrong.

In this case 'return 1' allows method maybeNew to run untill index is less than 5. Now 'return 0' : when index is equal or greater than 5, then'IF' loop no longer exist for main 'WHILE' loop and this is bassically amount = amount. (Because e.g. in m4a[5].maybeNew(5) index is equal 5 and maybeNew() returns 0).

+Pie Number of slices to send: Send
Previously in this thread.

Please don't hi‑jack threads by asking questions unrelated to the original subject. Fortunately I can split you off into a new topic. I don't have the time to go through Mix4 at the moment, but I would warn you it is more complicated than it looks at first sight.
+Pie Number of slices to send: Send
 

 'return 1' allows method maybeNew to run untill index is less than 5


The execution of the return 1 statement is done when the value of index is less than 5.
I'm not sure what is meant by "until".  There is only one if statement in the maybeNew method that controls what is returned: either a 0 or a 1

then'IF' loop no longer exist  


Some confusion there.  An if statement is not a loop.  
+Pie Number of slices to send: Send
Yes of course IF is not a loop, it is statement, sorry for my mistake. I used word untill in my previous post because Untill x is 5 or more, that what is in IF statement is added to ‚amount’ value in WHILE loop, is it Right?

Sorry and thank you for splitting topics Campbell Richie.
+Pie Number of slices to send: Send
Statements on lines 20 and 21 don't do anything that effects anything else in the program and could be removed without changing the values anywhere.  m4 is a local variable that disappears when the maybeNew method exits.
+Pie Number of slices to send: Send
So now the Question is, Why is these lines in exercise, are they for misdirection ?
+Pie Number of slices to send: Send
 

Why is these lines in exercise, are they for misdirection ?


Can you ask the authors or your instructor that question?

Is it possible that the posted code is different from what is in the book?
+Pie Number of slices to send: Send
No, code is exactly the same, I've just translated class and values names etc. I've looked into this code one more time, and tried few other examples e.g. x <20 and index < 5. As you have said, results are the same with and without lines 20-21. That's weird but it seems that I must take it for granted.
+Pie Number of slices to send: Send
Some more code that makes me think the author of the code did not know how to program:

Why do that in 2 lines?  Why not:
or


Also this line has an undefined variable: ilosc
+Pie Number of slices to send: Send
I'm posting picture of exercise in book:

+Pie Number of slices to send: Send
Also this line has an undefined variable: ilosc

Excuse me, I can't edit my post.  Variable 'ilosc' should be called 'amount'. That is the same word in two different languages.

+Pie Number of slices to send: Send
The problem is that you posted code that you had not compiled and executed to make sure what was being posted was correct.
+Pie Number of slices to send: Send
 

jack exovery wrote:So now the Question is, Why is these lines in exercise, are they for misdirection ?


If you ever go for the OCAJP, be prepared that many questions are trick questions, and many try to lay smoke curtains (like this one).
+Pie Number of slices to send: Send
Thank you all for stopping here by, sorry for my chaos with posting etc. , topic resolved.
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 636 times.
Similar Threads
Head First Java
Head First Java - Chapter 4 Mixed Messages Puzzle p.90
return - problem
code
Code Snippet Help
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:04:23.