Alright, you're doing a few weird things here. First of all, your file name and your method name are exactly the same...this is bad. It's not like it'll cause you problems, but it is something that you want to avoid.
If you were trying to create a constructor, then that is a completely different situation. In this case, what you would do is write:
CODE
public totalSum() {}
Where you could then pass arguements and such.
So, here's how we're going to fix your problem.
First of all, let's change the method in your program from totalSum() to average().
So this is what it should look like:
CODE
public int average(int num1, int num2, int num3)
{
int sum = 0;
int counter = 0;
if(num1 > 0)
{sum = sum + num1;
counter++;}
if(num2 > 0)
{sum = sum + num2;
counter++;}
if(num3 > 0)
{sum = sum + num3;
counter++;}
sum = sum / counter;
System.out.println("SUM: " + sum);
return sum;
}
Now, what you want to do is to be able to call this method. To do that you need to create an instance of the class, just as you did in your main method:
CODE
totalSum Gpa = new totalSum();
Now what you want to do is call the method contained in the class totalSum. This is done by using the variable you created (Gpa) and using the dot operator (.) then the method name:
CODE
int avg = gpa.average(num1, num2, num3);
Since the method average() returns an integer, that value will be stored in the newly created variable avg. Now, if you can do whatever you want to this value.
Notice that gpa.average() has three int values contained between the parentheses. This coresponds to how the method was created within the totalSum class. This is called the method's signature, and it's important that the values being passed corespond to what the method is expecting.
Here's a quick sidenote. When you divide ints, you are doing what is called integer division, which disregards any fractional piece left over after the division is done (for example, in integer division 5/2 returns the value 2, not 2.5). If you want to have the fractional parts, you need to use float values instead of ints. I'll leave you to try and figure out how to incorperate that into your program if you want.
Good luck.
This post has been edited by keems21: 19 Mar, 2007 - 07:47 PM