Results 1 to 7 of 7
  1. #1
    Confused123 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 java-forums.org!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Confused123 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    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; 10-10-2010 at 04:55 PM. Reason: Moderator Edit: Code tags added

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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; 10-10-2010 at 05:03 PM.

  6. #6
    Confused123 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  2. Replies: 3
    Last Post: 11-21-2009, 05:48 PM
  3. Replies: 5
    Last Post: 02-07-2009, 07:48 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  5. How to access ArrayList in List of List?
    By alvations in forum New To Java
    Replies: 5
    Last Post: 10-08-2008, 12:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •