4 Replies - 654 Views - Last Post: 11 February 2013 - 03:01 PM Rate Topic: -----

#1 dstevens   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-February 13

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?
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);
		}	
			
			}



Is This A Good Question/Topic? 0
  • +

Replies To: confused about overloading, loops

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

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).
Was This Post Helpful? 0
  • +
  • -

#3 dstevens   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-February 13

Re: confused about overloading, loops

Posted 11 February 2013 - 12:25 PM

View PostRyano121, 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).


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?
Was This Post Helpful? 0
  • +
  • -

#4 dstevens   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-February 13

Re: confused about overloading, loops

Posted 11 February 2013 - 12:39 PM

View Postdstevens, on 11 February 2013 - 12:25 PM, said:

View PostRyano121, 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).


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.
Was This Post Helpful? 0
  • +
  • -

#5 Sheph   User is offline

  • D.I.C Lover
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,032
  • Joined: 12-October 11

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.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1