This discussion is archived
10 Replies Latest reply: Mar 5, 2007 9:50 PM by 807606

Currently Being Moderated
I've been working with this for hours, it may seem simple to several people but apparently I cannot figure it out. If anyone can do it, please reply.

The problem: Average Calculator
Purpose: To practice writing overloaded operators. Remember...overloaded methods are methods that have the same name and different parameters. The compiler can tell the difference two between overloaded methods because they have different "signatures". A method signature consists of the methodName and the data types of its parameters. For example: a method with the header public void methodName( int num1, double num2) has the signature methodName, int, double . THE RETURN TYPE IS NOT PART OF THE SIGNATURE. Writing two methods with the same signature will cause an error...even if they have different return types.

Problem Specifications:
Write an applet or application that will accept 2-5 integers from the user, calculate the average of the numbers input, and display the result.

Let's get more specific: You must allow the user to choose how many integers to enter (2-5 are allowed). The average must be calculated in 1 of 4 overloaded methods--each named average. Each of these methods must calculate the average of a different number of integers (2, 3, 4 or 5) and return the result (remember to cast the answer to a double. Your program must print the average to the screen.

Let's talk pseudocode:
So that you can focus writing and using the overloaded methods, I have provided sample pseudocode that you can use for your program. This should allow you to focus solely on creating the overloaded operators. You are not required to use this pseudocode...you can substitute your own logic, as long as your program meets the requirements above.

Declare 5 variables to store numbers
Declare variable to store average
Declare and Initialize counter

Do
Get an integer
Update counter
While the user wants to continue entering numbers

Switch on counter
Case with 2 numbers
Calculate and save the average of the 2 integers (send to method average)
Case with 3 numbers
Calculate and save the average of the 3 integers (send to method average)
Case with 4 numbers
Calculate and save the average of the 4 integers (send to method average)
Case with 5 numbers
Calculate and save the average of the 5 integers (send to method average)
Endswitch

Display results

Let's talk method headers: If you get stuck on setting up your methods...here's a hint. All your methods will have the same name. The number of parameters will be different. They will all calculate the avereage of the numbers they receive in the method call...so be sure to use the correct number of arguments in your method calls! An example of what your method headers will look like is below.

public double average( int num1, int num2)
public double average( int num1, int num2, int num3)
public double average( int num1, int num2, int num3, int num4)
public double average( int num1, int num2, int num3, int num4, int num5)
Currently Being Moderated
What is your exact question? What have you done so far and what problems are you having with it?

Just posting your assignment and saying "help" could mean one of two things:

1) You have absolutely no idea where to start and need to learn problem solving and/or Java basics from the very beginning. We can't help with that. There are plenty of textbooks, tutorials, and the class you're presumably taking right now for that.

2) You want somebody to just do it for you. Most people here won't do that.
Currently Being Moderated

~
Currently Being Moderated
Okay, heres my code. I know it has to do something where my switch is, but if I have the switch in the main method then I cannot create the average method. I'm quite stuck here.
``````import javax.swing.* ;

double averages;
int input;

public void main( String args [] ) {
input = Integer.parseInt( JOptionPane.showInputDialog( "How many integers? ( 2-5 ): " ));
JOptionPane.showMessageDialog( null, "Average is " + averages , "Average", JOptionPane.PLAIN_MESSAGE );
}
switch( input ) {
case 2: {

public double average( int num1, int num2 ) // 1
{
num1 = Integer.parseInt( JOptionPane.showInputDialog( "Number 1: "));
num2 = Integer.parseInt( JOptionPane.showInputDialog( "Number 2: "));
averages = (num1 + num2)/ 2;
}
break;
}

case 3: {
public double average( int number1, int number2, int number3 )// 2
{
number1 = Integer.parseInt( JOptionPane.showInputDialog( "Number 1: "));
number2 = Integer.parseInt( JOptionPane.showInputDialog( "Number 2: "));
number3 = Integer.parseInt( JOptionPane.showInputDialog( "Number 3: "));
averages = (number1 + number2 + number3)/ 3;
}
break;
}

case 4: {
public double average( int numz1, int numz2, int numz3, int numz4 )// 3
{
numz1 = Integer.parseInt( JOptionPane.showInputDialog( "Number 1: "));
numz2 = Integer.parseInt( JOptionPane.showInputDialog( "Number 2: "));
numz3 = Integer.parseInt( JOptionPane.showInputDialog( "Number 3: "));
numz4 = Integer.parseInt( JOptionPane.showInputDialog( "Number 4: "));
averages = ( numz1 + numz2 + numz3 + numz4 )/ 4;
}
break;
}

case 5: {
public double average( int thing1, int thing2, int thing3, int thing4, int thing5 )// 4
{
thing1 = Integer.parseInt( JOptionPane.showInputDialog( "Number 1: "));
thing2 = Integer.parseInt( JOptionPane.showInputDialog( "Number 2: "));
thing3 = Integer.parseInt( JOptionPane.showInputDialog( "Number 3: "));
thing4 = Integer.parseInt( JOptionPane.showInputDialog( "Number 4: "));
thing5 = Integer.parseInt( JOptionPane.showInputDialog( "Number 5: "));
averages = ( thing1 + thing2 + thing3 + thing4 + thing5 )/ 5;
}
break;
}
}
}``````
Currently Being Moderated
You can't just put code willy-nilly outside of methods like that.

I'll give you a hint about the structure of the finalized program, but not necessarily following the exact assignment (I created static methods instead of storing state (averages, input variables) in the class). You can adopt to fit your exact assignment.
``````public class Overload {
public static void main(String[] args) {
// implementation here. In this implementation, you may
// invoke the average methods
// The switch statement will also be inside this method
}

private static double average(int num1, int num2) {
// implementation here. Return the average of the 2
}

private static double average(int num1, int num2, int num3) {
// implementation here. Return the average of the 3
}
}``````
Currently Being Moderated
for the switch statement, does this work:
``````switch( input ) {
case 2: {
average( int num1, int num2 );
break;
}
case 3:{
average( int num1, int num2, int num3 );
break;
}``````
and so on.... ?
Currently Being Moderated
so that I can implement the stuff for each method outside of the main method"?***
Currently Being Moderated
for the switch statement, does this work:
``````switch( input ) {
case 2: {
average( int num1, int num2 );
break;
case 3:{
average( int num1, int num2, int num3 );
break;``````
and so on.... ?
No.
``````double result;
switch (input) {
case 2:
result = average(num1, num2);
break;
case 3:
result = average(num1, num2, num3);
break;
// etcetera
}``````