Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HFJ Mix4 exercise  RSS feed

 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jacek Rajczak
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

 
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 2946
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

  '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. 
 
Jacek Rajczak
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Norm Radder
Rancher
Posts: 2946
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jacek Rajczak
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now the Question is, Why is these lines in exercise, are they for misdirection ?
 
Norm Radder
Rancher
Posts: 2946
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Jacek Rajczak
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Norm Radder
Rancher
Posts: 2946
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jacek Rajczak
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm posting picture of exercise in book:

 
Jacek Rajczak
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Norm Radder
Rancher
Posts: 2946
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you posted code that you had not compiled and executed to make sure what was being posted was correct.
 
Master Rancher
Posts: 2539
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
 
Jacek Rajczak
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for stopping here by, sorry for my chaos with posting etc. , topic resolved.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!