File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to get a method to call a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get a method to call a method" Watch "How to get a method to call a method" New topic
Author

How to get a method to call a method

Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
I have an assignment that requires a method average() to call a method that is in the same program called sum(). As you can see from my output the second call to method sum() is failing to send back a sum of the element values.
Any help will be greatly appreciated.
thanx
djb
P.S.
Anyone know how to keep the indentation in java code when posting here? Evertime i post it jams everything to the left.
The output of the code is here:
0 130
1 179
2 132
3 126
4 199
5 153
6 157
7 124
8 108
9 198
10 130
11 157
1793
Sum call results: 0.0 12 0.0
Average call results: 0 0.0
The program main is MyDriver listed here:

The class MyArray called from main is here:

[ February 27, 2004: Message edited by: Douglas Braxton ]
[ February 27, 2004: Message edited by: Douglas Braxton ]
[ February 27, 2004: Message edited by: Douglas Braxton ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You need to assign the result that is returned from the function to a variable:
someVariable = my.sum();
p.s. Please use code tags to preserve formatting for yoru code that you post on this board. Click here to learn how.
[ February 27, 2004: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
layne
thanx for the tip on the tags. i was wondering how to do that. In fact I edited the post while you were replying to ask that very question.
thanx for the answer to the dilemna as well. i'll try it.
djb
[ February 27, 2004: Message edited by: Douglas Braxton ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I need to take a closer look at your code since I'm not sure how to start wording my answer. In the mean time someone else may come by with some suggestions. Please be patient while I figure out what your problem is as well as try to explain how you can fix it.
Layne
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
First of all, I am surprised that your code even compiles. I see several minor problems that *should* cause compiler errors. I assume this means that the code you posted here isn't exactly the same as the code you are compiling. You should copy and paste your code, rather than type it, so that such inconsistencies are avoided.
With that siad, I would like to address the issue of your variable declarations. Although this isn't directly linked to your problem, the solution I propose will make your code much more readable.
It looks like each variable you use is only used within a single method. You should declare them in the method where they are used.

For one thing, this will guarantee that sumVal gets reset to zero every time you call sum(). Otherwise, it retains the value from the last time you called sum.
You should make similar changes with your other variables.
Then when you need the result of the sum() method, you use syntax similar to what I posted earlier. This probably will go in the main() method of the MyDriver class:

I think I'll stop there. If you still don't understand any of the details, please come back with more questions. Be sure to post any compiler errors you get along the way so we can help you fix those, too.
Keep coding!
Layne
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
Thanks; i'll try this!
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
One thing you might want to consider that can make your code simpler--if you're only interested in an average, and not the numbers that make up that average, you don't need to keep an array containing all those numbers.
For example, if I have the list {1, 2, 3} and I'm not interested in knowing the list, only that the average is 2, I could write a class:

This class allows you to keep a "running" average. Notice nowhere am I actually storing the list of numbers that comprise the average, just their sum and count. To use it:

Neat, huh? It's easy to extend this idea to include weighted averages:

Using this feature of the Average class might be useful if you have a list of 99 80's and one 30:

Note one additional thing--because I've used my setters and getters everywhere in my class (particularly the setOrder() method), I don't ever have to worry about setting the order to a negative value. If ever setOrder() is called with a number less than 0, the order will be set to 0 and the sum removed (which would have to be documented behavior in the javadoc of this class, of course).
One more thing...it might be fun to engage in a bit of what mathematicians call "domain stretching". Currently, this class only recognizes orders that are of the set of natural numbers: 0, 1, 2... You could define order to be a double throughout instead, as well as let it go into the negative side of the number line. If you think about it, this idea of what an "order" is could be useful for certain applications that require averaging.
I mention this because it's a better implementation of the idea of what an average is...it corresponds with Java's idea of the mod operator (%), which is traditionally used with operands that are natural numbers, but Java allows you to use with negatives and fractional operands with mod too.
sev
[ February 27, 2004: Message edited by: sever oon ]
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
Thanks Sev!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get a method to call a method