-
February 11th, 2013, 03:08 PM
#1
help with overloading code
Can anyone help me with overloading. I have a task that starts with 2 numbers being entered by the user. then it increases to three numbers entered then 4 . how can I get the user input then have the program automatically figure the average of the numbers entered. Please help
Code:
import java.io.*;
import java.util.*;
class FinalAverage {
void Sum(int num1, int num2)
{
System.out.println("Sum of "+ num1 + " and " + num2+ " is " +(num1 + num2));
}
void Sum(int num1, int num2, int num3)
{
System.out.println("Sum of "+ num1 + " and " + num2+ " and " + num3+ " is " +(num1 + num2 + num3));
}
public static void main(String[] args) {
float average;
int count = 0, x,x2, sum = 0 ;
Average Sum1 = new Average();
Scanner input = new Scanner(System.in);
try {
System.out.println("Enter first number to find the Average ");
x = input.nextInt();
System.out.println("Enter second number to find the Average ");
count = 1;
//x2 = input.nextInt();
count++;
Sum1.Sum (x,x);
Sum1.Sum(x, x,x);
sum = x + x;
}
catch (NumberFormatException e)
{
System.out.println("You did not enter an integer, and an Error occured. Please enter an integer?");
}
average = (sum/count);
System.out.println("your average of the numbers entered equals......" + average);
System.exit(0);
}
}
-
February 11th, 2013, 04:00 PM
#2
Re: help with overloading code
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
February 12th, 2013, 04:25 AM
#3
Re: help with overloading code
Overloading means using the same method name but with different numbers and/or types of parameters.
Code:
public double average(int first, int second) {
double result = (first + second) / 2;
return result;
}
public double average(int first, int second, int third) {
double result = (first + second + third) / 3;
return result;
}
public double average(int first, int second, int third, int fourth) {
// Sure you can guess what goes here
}
Then you can call the average() method with 2, 3 or 4 integer arguments and always get a double as the result.
There comes a point where this gets excessive (e.g. with 5, 6, 7 or more integer arguments) - then it would be worth considering either using varargs (as suggested in the previous post) or passing the arguments as an array of integers.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|