Guten Tag,

What we need to do is to call a variable (numbersToEnterInteger) from the main method to the total_average method.

Also how do we pass the entire array to this method, rather than a single array element.

Thanks in advance. Oh and also heres the code.

/**
* random description of this program *non-existent as of yet*
 * @author () 
 * @version ()
 */
import java.text.DecimalFormat;
import java.math.*;
import javax.swing.*;
public class array
{ //open public class array

public static void main (String [] args)
 { //open main
     String stringNumber; 
     int numbersToEnterInteger = 0; //initialise numbersToEnterInteger as 0
     while (numbersToEnterInteger <= 0)
     {  //open while loop
         String numberToEnter = JOptionPane.showInputDialog(null, "How many numbers do you want to enter?");
         // convert the variable numberToEnter into an integer
         try 
         {//open try
            numbersToEnterInteger = Integer.parseInt(numberToEnter);
         }//close try 
             
         catch (NumberFormatException nfe)
         {//open catch
                 numbersToEnterInteger = 0;  // reinitialise for the next line
         }//close catch
     } //close while loop
     double[] doubleArray = new double[numbersToEnterInteger];
     int count = 0; //initialise count as 0
     
     for (count = 0; count < numbersToEnterInteger; count++)
     {//open for
         stringNumber = JOptionPane.showInputDialog (null, "Please enter an integer between 25 and 150");
         doubleArray[count]= Integer.parseInt(stringNumber);
         while (doubleArray[count] < 25 || doubleArray[count] > 150)
                {//open while
                    JOptionPane.showMessageDialog (null, "That is not a valid number.");
                    stringNumber = JOptionPane.showInputDialog (null, "Please enter an integer between 25 and 150");
                    doubleArray[count]= Integer.parseInt(stringNumber);
                }//close while
     }//close for
                  
     for (count=0; count < numbersToEnterInteger; count++)
     {
        JOptionPane.showMessageDialog (null, doubleArray[count] + " squared is " + 
        calcSquare(doubleArray[count]) + "\n" + doubleArray[count]
        + " square root is " +calcSquareRoot(doubleArray[count]) + "\n" + 
        doubleArray[count] + " cubed is " + calcCube(doubleArray[count]));
     }
                    
            
 }//close main
 
double square, squareRoot, cube;
  
public static double calcSquare(double doubleArray)
 {
    DecimalFormat formatter = new DecimalFormat(",###.000");
    double result = doubleArray * doubleArray;
    return result;
            
 }
      
public static double calcSquareRoot(double doubleArray)
 {
    DecimalFormat formatter = new DecimalFormat(",###.000");
    double result = Math.sqrt (doubleArray);
    return result;
        
 }
        
public static double calcCube(double doubleArray)
 {
    DecimalFormat formatter = new DecimalFormat(",###.000");
    double result = doubleArray * doubleArray * doubleArray;
    return result;
            
 }

 public static void total_average (double doubleArray)
 {
     double sum = 0;
     double average = 0;
     int numbersToEnterInteger;
     numbersToEnterInteger = array.main(numbersToEnterInteger);
     for (int temp = 0; temp < doubleArray; temp++)

     {
         sum = sum + doubleArray;
         average = (sum / numbersToEnterInteger);
     }
     JOptionPane.showMessageDialog (null, "Total = "+sum+"\nAverage = "+average); 
 } 
} //close class

Recommended Answers

All 2 Replies

Well, you declared a double[] doubleArray. So you have to change the method header of the total_average method to double[], not to double, since you want to pass in an array of doubles. Then in your main method just say

total_average(doubleArray);

Be a part of the DaniWeb community

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