• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formal parameters, methods, arguments, decomposition

 
Gennadiy Rabkin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have written the following code:



I wanted to decompose code by creating few additional methods into the following:



When I did it my code start to worked wrong and prints (0,0) pair, but initial code correct and works nice. How I can return values? Is it possible in that case ?
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The variables top and bottom in your methods are local variables to the method and shadow the ones you declare in the first line.

The latter ones always remain 0 since you never assign a new value to them.

Can you explain in words what you are trying to achive. Your code looks a bit too complicated for what you want to do...
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a method is called with a primitive parameter, a copy of the value is passed, not the pointer to the adress of memory where the number is stored.

you should post those number as members of the class not function parameters.
 
Winston Gutkowski
Bartender
Pie
Posts: 10266
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gennadiy Rabkin wrote:When I did it my code start to worked wrong and prints (0,0) pair, but initial code correct and works nice.

Michael's quite right. It'd be far better if you explained what you're trying to do (although I think I can guess). A couple of comment lines for each method would help as well, and it's a very good habit to get into.

However, a few pointers:
1. I'm surprised that your new code is printing anything at all, because the return statements at lines 22 and 31 look to me like they should cause a stack overflow because you've added an unconditional recursive call - and furthermore the one on line 22 has the parameters in a different order.
2. Returned values are generally meant to be used; on line 20 you don't.
3. Your compareNumbers() method changes two values but only returns one. Which one would that be? Hint: if you want to return two values, you should return an array (or an object that contains two values).
4. Try and avoid constructs like while(true). Essentially, it's an infinite loop, so it's a potential source of problems. Try replacing the true with a condition that causes the loop to end.

How I can return values? Is it possible in that case ?

Sure it is. You're just not doing it correctly. I suggest you read the tutorials.

HIH

Winston
 
Gennadiy Rabkin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody. Sorry that missed comments to parts of a code.
I have read tutorial. Deleted mad return statements and declare top, bottom and i as an instance variables: private static int i = 0, top = 0, bottom = 0;
And all works fine. You know this is an educational exercise. Main idea was to write a code of a program using top-bottom design practice and understand how methods communicate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic