• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Formal parameters, methods, arguments, decomposition

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic