aspose file tools*
The moose likes Beginning Java and the fly likes Formal parameters, methods, arguments, decomposition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Formal parameters, methods, arguments, decomposition" Watch "Formal parameters, methods, arguments, decomposition" New topic
Author

Formal parameters, methods, arguments, decomposition

Gennadiy Rabkin
Greenhorn

Joined: Jul 11, 2012
Posts: 10
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

Joined: Jul 08, 2012
Posts: 35
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

Joined: May 05, 2010
Posts: 73
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

Joined: Mar 17, 2011
Posts: 8398
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Gennadiy Rabkin
Greenhorn

Joined: Jul 11, 2012
Posts: 10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formal parameters, methods, arguments, decomposition