I am trying to configure an app that calculates the product of a series of doubles that are passed to the method calcProduct. calcProduct will have a variable length arguements and should return the product that is calculated. This is supposed to then be printed with the array's identification.
Here is my code so far:
import java.text.DecimalFormat; import java.text.NumberFormat;
public class variableLength {
public static double calProduct(double... numbers)
{
double total = 0.0;
for (double number : numbers)
total += number;
return total / numbers.length;
}
public static void main(String[] args)
{
NumberFormat myFormat = NumberFormat.getInstance();
myFormat.setGroupingUsed(true);
DecimalFormat decimalFormat = new DecimalFormat("#.###");
decimalFormat.setGroupingUsed(true);
decimalFormat.setGroupingSize(3);
double[] array1 = {2.1, 3.2, 4.3};
double[] array2 = {1, 2, 3, 4, 5};
double[] array3 = {2.0, 110.12345};
double[] array4 = {10.0, 12, 13.1, 4, 5.2, 6, 7.3, 8, 9.4, 100};
double[] array5 = { };
System.out.println(myFormat.format(array1));
System.out.println(myFormat.format(array2));
System.out.println(myFormat.format(array3));
System.out.println(myFormat.format(array4));
System.out.println(myFormat.format(array5));
System.out.println(decimalFormat.format(array1));
System.out.println(decimalFormat.format(array2));
System.out.println(decimalFormat.format(array3));
System.out.println(decimalFormat.format(array4));
System.out.println(decimalFormat.format(array5));
System.out.printf("array1 = %.1f\narray2 = %.1f\narray3 = %.1f\narray4 = %.1f\narray5 = %.1f\n\n", array1, array2, array3, array4);
System.out.println("The average of array1 and array2 is \n" + calProduct(array1, array2));
System.out.println("The average of array1, array2, and array3 is \n" + calProduct(array1, array2, array3));
System.out.println("The average of array1, array2, and array3, array4 is \n" + calProduct(array1, array2, array3, array4));
System.out.println("The average of array1, array2, array3, array4 and array5 is \n" + calProduct(array1, array2, array3, array4, array5));
}
}
I keep getting this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method calProduct(double...) in the type variableLength is not applicable for the arguments (double[], double[]) The method calProduct(double...) in the type variableLength is not applicable for the arguments (double[], double[], double[]) The method calProduct(double...) in the type variableLength is not applicable for the arguments (double[], double[], double[], double[]) The method calProduct(double...) in the type variableLength is not applicable for the arguments (double[], double[], double[], double[], double[])
at variableLength.main(variableLength.java:52)
What am I doing wrong? How can I achieve a working program? Thank you in advance!