# How to computeMean, computeMin, computeMax, and computeVariance in list of numbers

• 10-10-2010, 04:39 PM
Confused123
How to computeMean, computeMin, computeMax, and computeVariance in list of numbers
In my class, my prof wants me to compute the Mean, Max, Min, and variance in a list of numbers by writing it out in code. So, how exactly do I do this? I looked in my JAVA book for class, yet there are no examples in the book on how to do this. Here are the list of numbers:

10
1.0
2.0
4.0
5.0
5.0
6.0
8.0
9.0
13.0
-1.0

• 10-10-2010, 04:42 PM
Fubarable
Do you have any code yet? How are the numbers entered, for instance?

Next, how would you do these calculations by hand if someone were reading you a list of numbers? Figure this out, then you can use this algorithm to guide you in writing your code.

Best of luck and welcome to the java-forums.org!
• 10-10-2010, 04:44 PM
Fubarable
OP, please re-read the forum FAQ and do not post duplicate posts. They have been deleted.
• 10-10-2010, 04:44 PM
Confused123
This is the code that I have:

Code:

```import java.util.Scanner; import javax.swing.JFileChooser; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class ArrayStatsMain {         public static double[] readFile(File file) {                 FileInputStream inStream = null;                 try {                         inStream = new FileInputStream(file);                 } catch (FileNotFoundException e) {                         e.printStackTrace();                 }                 Scanner scanner = new Scanner(inStream);                 int length = scanner.nextInt();                 double[] data = new double[length];                 for (int index=0; index<length; index++) {                         data[index] = scanner.nextDouble();                 }                 return data;         }                 public static double computeMean(double[] data) {                 double mean = 0.0;                                 // insert code here                 // including a "for" loop                                   return mean;         }                 public static double computeVariance(double[] data) {                 double sumOfSquaresOfDifferences = 0.0;                                 // insert code here                 // including a "for" loop                                 return sumOfSquaresOfDifferences / (data.length - 1);         }                 public static double computeMin(double[] data) {                 double min = 0.0;                                 // insert code here                 // including a "for" loop                                 return min;         }                 public static double computeMax(double[] data) {                 double max = 0.0;                                 // insert code here                 // including a "for" loop                                 return max;         }                         /**         * @param args         */         public static void main(String[] args) {                 JFileChooser fileChooser = new JFileChooser();                 fileChooser.setCurrentDirectory(new File("."));                 if (fileChooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {                         return;                 }                                 double[] data = readFile(fileChooser.getSelectedFile());                 System.out.println("length=" + data.length);                                 // remove this section of code                 double sum = 0.0;                 for (int index=0; index<data.length; index++) {                         sum += data[index];                 }                 double mean = sum/data.length;                 System.out.println("mean=" + mean);                 // end of code to be removed                                 System.out.println("mean=" + computeMean(data));                 System.out.println("min =" + computeMin(data));                 System.out.println("max =" + computeMax(data));                 System.out.println("variance=" + computeVariance(data));                         } }```
• 10-10-2010, 04:56 PM
Fubarable
So your teacher has already provided most of the code for you, and all you have to do is what he tells you to in the comments. So divide and conquer: break your problem down into small steps and solve one step at a time, starting with computeMean(). Given a list of numbers, how do you compute the mean or average on paper?

Also, I've added code tags to your post so the code will retain its formatting. To learn how to do this yourself, please see the first link in my signature links.

Best of luck.
• 10-10-2010, 05:06 PM
Confused123
I am taking a first-year course for programming in college. This is my FIRST programming course that I have ever taken. So, I am a bit confused when doing anything in Eclipse.

I know how to compute the mean. The mean is the average of a set of numbers. I am just confused on listing the variables and starting the for loop. Are there any EXAMPLES that I could look at?
• 10-10-2010, 05:15 PM
Fubarable
Our goal here is to try to nudge you toward a solution since you often know more about how to solve this problem than you realize.

For instance, what I was getting at above, when calculating an average of a list of numbers, you usually add the items together and then divide by the number items that have been added. So if you have the numbers 3, 5, 8, the average is (3 + 5 + 8)/3.

Well to do this in Java is much the same thing. You start with a double variable to hold the sum, say called sum, then after this you have a for loop where you loop through the data array (int i = 0; i < data.length; i++) and add the ith item in the array to the sum variable inside the for loop. Then after the for loop has been completed, you divide the sum by the length of the array, data.length and that's your mean -- so you return this value.