Results 1 to 7 of 7
 10102010, 04:39 PM #1Member
 Join Date
 Oct 2010
 Posts
 3
 Rep Power
 0
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
Thanks in advance!

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.
Also, please have a look at the last link in my signature links below.
Best of luck and welcome to the javaforums.org!

OP, please reread the forum FAQ and do not post duplicate posts. They have been deleted.
 10102010, 04:44 PM #4Member
 Join Date
 Oct 2010
 Posts
 3
 Rep Power
 0
This is the code that I have:
Java 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)); } }
Last edited by Fubarable; 10102010 at 04:55 PM. Reason: Moderator Edit: Code tags added

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.Last edited by Fubarable; 10102010 at 05:03 PM.
 10102010, 05:06 PM #6Member
 Join Date
 Oct 2010
 Posts
 3
 Rep Power
 0
I am taking a firstyear 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?

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.
Similar Threads

How do I list values of an arrays in a comma seperted list
By nmvictor in forum New To JavaReplies: 2Last Post: 11222009, 05:24 PM 
How do I list values of an arrays in a comma seperted list
By nmvictor in forum New To JavaReplies: 3Last Post: 11212009, 05:48 PM 
finding the largest k numbers in an array without sorting the whole list?
By jmmjm in forum Advanced JavaReplies: 5Last Post: 02072009, 07:48 AM 
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10142008, 11:46 PM 
How to access ArrayList in List of List?
By alvations in forum New To JavaReplies: 5Last Post: 10082008, 12:23 PM
Bookmarks