Loyola College in Maryland

CS 201 - Computer Science I
Fall 2008


Loyola College > Department of Computer Science > Dr. James Glenn > CS 201 > Examples and Lecture Notes > Exam Statistics Calculator

Statistics.java

import javax.swing.*;

/**
 * Computes the mean and standard deviation of data input by the user.
 *
 * @author Jim Glenn
 * @version 0.1 11/17/2003
 */

public class Statistics
{
	public static void main(String[] args)
	{
		// three variables to handle the three pieces of data
	
		int score1, score2, score3;
		
		// three statements that input data into those variables
		
		score1 = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #1"));
		score2 = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #2"));
		score3 = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #3"));
		
		// compute the average
		
		int tot = score1 + score2 + score3;
		double avg = tot / 3.0; // 3.0 to force double division
		
		// temp variables to hold the variance of each datum
		
		double var1 = Math.pow(score1 - avg, 2);
		double var2 = Math.pow(score2 - avg, 2);
		double var3 = Math.pow(score3 - avg, 2);
		
		// compute the standard deviation
		
		double stddev = Math.sqrt((var1 + var2 + var3) / 3);

		// display results

		JOptionPane.showMessageDialog(null, "Average " + avg + "\nStandard deviation " + stddev);
		System.exit(0);
	}
}

Statisticsv2.java

import javax.swing.*;

/**
 * Computes the mean and standard deviation of data input by the user.
 * This is the same as Statistics.java except we allow five data instead
 * of three.
 *
 * @author Jim Glenn
 * @version 0.1 11/17/2003
 */
 
public class Statisticsv2
{
	public static void main(String[] args)
	{
		// now five separate variables to hold the sum
	
		int score1, score2, score3, score4, score5;
		
		// now five statements to read the five data
		
		// the need for a loop should be apparent now, but as things
		// are currently set up we can't do this in a loop -- the loop
		// would look something like
		// for (int i = 1; i <= 5; i++)
		//    ??? = Integer.parseInt(...);
		// but what do we put in ???  not "scorei" since the compiler
		// will thing we're using an undeclared variable called
		// "scorei" -- we need to use arrays here (see Statistics v3)
		
		score1 = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #1"));
		score2 = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #2"));
		score3 = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #3"));
		score4 = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #4"));
		score5 = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #5"));
		
		// compute the sum (imagine if there were 30 data)
		
		int tot = score1 + score2 + score3 + score4 + score5;
		double avg = tot / 5.0;
		
		// temp variables for individual variances
		
		double var1 = Math.pow(score1 - avg, 2);
		double var2 = Math.pow(score2 - avg, 2);
		double var3 = Math.pow(score3 - avg, 2);
		double var4 = Math.pow(score4 - avg, 2);
		double var5 = Math.pow(score5 - avg, 2);
			
		// compute standard deviation
			
		double stddev = Math.sqrt((var1 + var2 + var3 + var4 + var5) / 5);

		// display results

		JOptionPane.showMessageDialog(null, "Average " + avg + "\nStandard deviation " + stddev);
		System.exit(0);
	}
}

Statisticsv3.java

import javax.swing.*;

/**
 * Computes the mean and standard deviation of data input by the user.
 * This is the same as Statisticsv2.java except we use an array instead
 * of five separate variables (no loops yet).
 *
 * @author Jim Glenn
 * @version 0.1 11/17/2003
 */

public class Statisticsv3
{
	public static void main(String[] args)
	{
		// declare an array to hold the data
	
		int[] scores;

		// create the array so it can hold 5 data

		scores = new int[5];
		
		// read into the five elements in the array (replace with a loop later)
		
		scores[0] = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #1"));
		scores[1] = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #2"));
		scores[2] = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #3"));
		scores[3] = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #4"));
		scores[4] = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #5"));
		
		// add the data and divide by number (replace with a loop later)
		
		int tot = scores[0] + scores[1] + scores[2] + scores[3] + scores[4];
		double avg = tot / (double)scores.length;
		
		// declare, create, and initialize a temp array to hold individual
		// variances
		
		double[] var = new double[5];
		var[0] = Math.pow(scores[0] - avg, 2);
		var[1] = Math.pow(scores[1] - avg, 2);
		var[2] = Math.pow(scores[2] - avg, 2);
		var[3] = Math.pow(scores[3] - avg, 2);
		var[4] = Math.pow(scores[4] - avg, 2);
			
		// compute standard deviation	
		
		double stddev = Math.sqrt((var[0] + var[1] + var[2] + var[3] + var[4]) / scores.length);

		// display results

		JOptionPane.showMessageDialog(null, "Average " + avg + "\nStandard deviation " + stddev);
		System.exit(0);
	}
}

Statisticsv4.java

import javax.swing.*;

/**
 * Computes the mean and standard deviation of data input by the user.
 * This is the same as Statisticsv3.java except we use loops as appropriate
 * and we allow the user to specify how much data there is.
 *
 * @author Jim Glenn
 * @version 0.1 11/17/2003
 */

public class Statisticsv4
{
	public static void main(String[] args)
	{
		// ask the user how much data there is
		
		int numStudents = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of students"));
		
		// declare array and make it as big as needed
	
		int[] scores = new int[numStudents];
		
		// read user input and store in array
		
		for (int stu = 0; stu < numStudents; stu++)
			scores[stu] = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #" + (stu + 1)));
		
		// compute sum of data
		
		int tot = 0;
		for (int stu = 0; stu < numStudents; stu++)
			tot += scores[stu];

		// compute average of data

		double avg = (double)tot / numStudents;
		
		// compute sum of individual variances
		
		double totVar = 0.0;
		for (int stu = 0; stu < numStudents; stu++)
			totVar += Math.pow(scores[stu] - avg, 2);
			
		// compute standard deviation
			
		double stddev = Math.sqrt((double)totVar / numStudents);
		
		// display results
		
		JOptionPane.showMessageDialog(null, "Average " + avg + "\nStandard deviation " + stddev);
		System.exit(0);
	}
}

Statisticsv5.java

import javax.swing.*;

/**
 * Computes the mean and standard deviation of data input by the user.
 * This is the same as Statisticsv4.java except we use a sentinel loop
 * to determine how much data there is.
 *
 * @author Jim Glenn
 * @version 0.1 11/17/2003
 */

public class Statisticsv5
{
	/**
	 * The maximum number of students we expect to have to handle.
	 */

	public static final int MAX_STUDENTS = 500;

	public static void main(String[] args)
	{
		// Chicken and egg problem: we need to create the array
		// before the loop so we can store the user's input in it,
		// but we can't create the array until we know how big it
		// should be, and we don't know how big it should be until
		// we see the sentinel and the loop ends.  Solution:
		// make the array really big but don't use all of it.  We will
		// also have to keep track of how much of the array we're
		// actually using (since scores.length will give how
		// much data it _can_ hold instead of how much data it
		// _does_ hold).
	
		int[] scores = new int[MAX_STUDENTS];
		
		// read user input until sentinel (-1) entered and store in array
		
		int numStudents = 0; // how much data we've put in the array
		int score = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #1 or -1 if finished"));
		while (score != -1)
		{
			scores[numStudents] = score; // store user input in array
			numStudents++; // remember how much data is in there now
			score = Integer.parseInt(JOptionPane.showInputDialog("Enter the score for student #" + (numStudents + 1) + " or -1 if finished"));
		}
		
		// compute sum of data
		
		int tot = 0;
		for (int stu = 0; stu < numStudents; stu++)
			tot += scores[stu];

		// compute average of data

		double avg = (double)tot / numStudents;
		
		// compute sum of individual variances
		
		double totVar = 0.0;
		for (int stu = 0; stu < numStudents; stu++)
			totVar += Math.pow(scores[stu] - avg, 2);
			
		// compute standard deviation
			
		double stddev = Math.sqrt((double)totVar / numStudents);
		
		// display results
		
		JOptionPane.showMessageDialog(null, "Average " + avg + "\nStandard deviation " + stddev);
		System.exit(0);
	}
}
This code can also be downloaded from the files
Statistics.java, Statisticsv2.java, Statisticsv3.java, Statisticsv4.java, and Statisticsv5.java.