Welcome to Dream.In.Code
Become a Java Expert!

Join 149,489 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,292 people online right now. Registration is fast and FREE... Join Now!




Help on user-defined methods

 
Reply to this topicStart new topic

Help on user-defined methods

deeRock26
19 Mar, 2007 - 06:36 PM
Post #1

New D.I.C Head
*

Joined: 19 Mar, 2007
Posts: 1


My Contributions
The assignment is to write a method that, given a name of the file with numbers, computes the average of all these numbers, and write a program that uses this method. I am lost my code gives me something totally off. I am able to compile it and run it but that isn't a Gpa.

import java.util.*;

//starts the class
public class totalSum
{


public int totalSum(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;

return sum;
}
public static void main(String[] args)
{
int num1 = 89;
int num2 = 75;
int num3 = 98;
totalSum Gpa;
Gpa = new totalSum();

System.out.println("You GPA is: " + Gpa);

}
}

User is offlineProfile CardPM
+Quote Post

keems21
RE: Help On User-defined Methods
19 Mar, 2007 - 07:28 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month