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);
}
}