import java.io.*;
import java.util.*;
class Average {
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,x2);
sum = x + x2;
}
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);
}
}
confused about overloading, loops
Page 1 of 14 Replies - 654 Views - Last Post: 11 February 2013 - 03:01 PM
#1
confused about overloading, loops
Posted 11 February 2013 - 11:34 AM
I am trying to complete a task for overloading and I have the code below. my task is to be able to enter 2, 3 or 4 integers and have the program average them out. I have it set to enter 2 integers but I am a little confused on how to have it take as little or many integers as I want to enter, then average them out. Please help?
Replies To: confused about overloading, loops
#2
Re: confused about overloading, loops
Posted 11 February 2013 - 12:07 PM
This is no a good use of overloading. This calls for one method that takes an array of int's as a parameter. I don't know if you have covered arrays yet but this is really bad example of overloading. You have to repeat your code again and again. What happens when I want to add the sum of 6 numbers, then 7 - it gets messy.
If you are on about getting numbers from the users which I think you are. You either prompt the user to tell you how many numbers they will enter, and then prompt them that many times, or start a while loop that runs until the user for example inputs -1 (although this one doesn't make much sense in this case).
If you are on about getting numbers from the users which I think you are. You either prompt the user to tell you how many numbers they will enter, and then prompt them that many times, or start a while loop that runs until the user for example inputs -1 (although this one doesn't make much sense in this case).
#3
Re: confused about overloading, loops
Posted 11 February 2013 - 12:25 PM
Ryano121, on 11 February 2013 - 12:07 PM, said:
This is no a good use of overloading. This calls for one method that takes an array of int's as a parameter. I don't know if you have covered arrays yet but this is really bad example of overloading. You have to repeat your code again and again. What happens when I want to add the sum of 6 numbers, then 7 - it gets messy.
If you are on about getting numbers from the users which I think you are. You either prompt the user to tell you how many numbers they will enter, and then prompt them that many times, or start a while loop that runs until the user for example inputs -1 (although this one doesn't make much sense in this case).
If you are on about getting numbers from the users which I think you are. You either prompt the user to tell you how many numbers they will enter, and then prompt them that many times, or start a while loop that runs until the user for example inputs -1 (although this one doesn't make much sense in this case).
Yes the task is to have the user input however many integers/numbers then get the average. the task starts out with entering 2 numbers, then increases to 3 then increases to 4numbers entered by the user. does this help any?
#4
Re: confused about overloading, loops
Posted 11 February 2013 - 12:39 PM
dstevens, on 11 February 2013 - 12:25 PM, said:
Ryano121, on 11 February 2013 - 12:07 PM, said:
This is no a good use of overloading. This calls for one method that takes an array of int's as a parameter. I don't know if you have covered arrays yet but this is really bad example of overloading. You have to repeat your code again and again. What happens when I want to add the sum of 6 numbers, then 7 - it gets messy.
If you are on about getting numbers from the users which I think you are. You either prompt the user to tell you how many numbers they will enter, and then prompt them that many times, or start a while loop that runs until the user for example inputs -1 (although this one doesn't make much sense in this case).
If you are on about getting numbers from the users which I think you are. You either prompt the user to tell you how many numbers they will enter, and then prompt them that many times, or start a while loop that runs until the user for example inputs -1 (although this one doesn't make much sense in this case).
Yes the task is to have the user input however many integers/numbers then get the average. the task starts out with entering 2 numbers, then increases to 3 then increases to 4numbers entered by the user. does this help any?
We are not into arrays yet so I cant use arrays.
#5
Re: confused about overloading, loops
Posted 11 February 2013 - 03:01 PM
if your assignment is to make three different functions with the same name, then that's gonna be how it has to be. BUT you can at least do a couple things to make it easier.
- Use Java naming conventions and begin your variables with lowercase letters.
- Have your sum() methods return an integer, and worry about printing the result in your main method.
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|