basically, instead of the "(N1 + N2 + N3)" which is in the average method we need to call the sum method to the average method as the sum method returns a value = (N1 + N2 + N3). does anyone know how to call the sum method to the average method? The program still runs fine the way it is but the questions asks for it this way. thanks.

``````import javax.swing.JOptionPane;
public class maths_comp
{
public static void main(String[]args)
{
/*Declare variables*/
String Input, Input_2, Input_3;
double num_1, num_2, num_3;

Input = JOptionPane.showInputDialog("Enter Number 1:");
num_1 = Double.parseDouble(Input);

Input_2 = JOptionPane.showInputDialog("Enter Number 2:");
num_2 = Double.parseDouble(Input_2);

Input_3 = JOptionPane.showInputDialog("Enter Number 3:");
num_3 = Double.parseDouble(Input_3);

JOptionPane.showMessageDialog(null, "The Sum of the 3 numbers entered is: " + sum(num_1, num_2, num_3) + "\nThe Average of the 3 numbers entered is: " + average(num_1, num_2, num_3));

}

public static double sum(double N1, double N2, double N3)
{
add_num = N1 + N2 + N3;
}

public static double average(double N1, double N2, double N3)
{
double avg_num;
avg_num = (N1 + N2 + N3) / 3;
return avg_num;
}
}``````

## All 4 Replies

You should except any number of variables as input in your method, like this:

``````public static double sum(double... numbers)
{
for (double d: numbers)
}``````
``````public static double average(double... numbers)
{
double avg_num;
avg_num = sum(numbers)/ numbers.length;
return avg_num;
}
}``````

so do I only need 1 input variable now (double d) for d because d is adding them in the sum method?

if you assigned a variable called inputSum and then called the average method with the parameter inputSum then you could do the same thing, except you wouldn't need to add your numbers in the average method.

so do I only need 1 input variable now (double d) for d because d is adding them in the sum method?

Look at the method. It is not just double d, but double...d, with three dots. This means you can put any amount of variables in there, even an array. Try it yourself.
Now you can use as many variables you want to sum up, and the same way is used to call the sum method in the average method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.