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);
}
}
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);
}
}
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);
}
}
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], 2);
// compute standard deviation
double stddev = Math.sqrt((double)totVar / numStudents);
// display results
JOptionPane.showMessageDialog(null, "Average " + avg + "\nStandard deviation " + stddev);
System.exit(0);
}
}
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], 2);
// compute standard deviation
double stddev = Math.sqrt((double)totVar / numStudents);
// display results
JOptionPane.showMessageDialog(null, "Average " + avg + "\nStandard deviation " + stddev);
System.exit(0);
}
}
import javax.swing.*;
/**
* A set of exam scores. Certain statistics may be computed.
*
* @author Jim Glenn
* @version 0.1 11/19/2003
*/
public class ExamStatistics
{
/**
* The array that holds the scores.
*/
private int[] scores;
/**
* The number of scores currently stored in the array
* (the number of array elements that are truly in use).
*/
private int numStudents;
/**
* The size of the array (but not all array elements are
* necessarily ging to be used).
*/
public static final int MAX_STUDENTS = 2000;
/**
* Creates an empty set of exam scores.
*/
public ExamStatistics()
{
scores = new int[MAX_STUDENTS];
numStudents = 0;
}
/**
* Adds the given score to this set of data.
*
* @param s the score to add
*/
public void addScore(int s)
{
scores[numStudents] = s;
numStudents++;
}
/**
* Returns the average score.
*
* @return the average score
*/
public double calcMean()
{
int tot = 0;
for (int i = 0; i < numStudents; i++)
tot += scores[i];
return tot / (double)numStudents;
}
/**
* Returns the standard deviation of the exam scores.
*
* @return the standard deviation
*/
public double calcStandardDeviation()
{
double var = 0.0;
double mean = calcMean();
for (int i = 0; i < numStudents; i++)
var += Math.pow(scores[i] - mean, 2);
return Math.sqrt(var / numStudents);
}
/**
* Returns the highest score.
*
* @return the highest score
*/
public int findMax()
{
int max = Integer.MIN_VALUE;
for (int i = 0; i < numStudents; i++)
max = Math.max(max, scores[i]);
return max;
}
/**
* Returns the lowest score.
*
* @return the lowest score
*/
public int findMin()
{
int min = Integer.MAX_VALUE;
for (int i = 0; i < numStudents; i++)
{
// the following does the same thing as
// min = Math.min(min, scores[i]);
if (min > scores[i])
min = scores[i];
}
return min;
}
/**
* Returns the most extreme (farthest from the mean) score
* among these statistics. For example, if the scores
* are 80, 60, and 10 then this method returns 10.
*
* @return the most extreme score
*/
public int findExtremum()
{
// we keep track of the largest difference we see
// _and_ the value that gave us the maximum difference
double maxDiff = Double.NEGATIVE_INFINITY;
int extremum = Integer.MAX_VALUE;
double mean = calcMean();
for (int i = 0; i < numStudents; i++)
if (maxDiff < Math.abs(scores[i] - mean))
{
maxDiff = Math.abs(scores[i] - mean);
extremum = scores[i];
}
return extremum;
}
/**
* Determines if all scores are the same.
*
* @return true if and only if all scores are the same
*/
public boolean allSame()
{
// to determine if scores[0] == scores[1] && scores[1] == scores[2] && ... && scores[numStudents - 2] == scores[numStudents - 1] is true
// we are searching for an index i such that scores[i] != scores[i + 1]
int loc = 0;
while (loc < numStudents - 1 && scores[loc] == scores[loc + 1])
loc++;
if (loc == numStudents - 1)
return true;
else
return false;
}
/**
* Test driver for ExamStatistics.
*
* @param args ignored
*/
public static void main(String[] args)
{
ExamStatistics stats = new ExamStatistics();
int score = Integer.parseInt(JOptionPane.showInputDialog("Enter a score or -1 to quit"));
while (score != -1)
{
stats.addScore(score);
score = Integer.parseInt(JOptionPane.showInputDialog("Enter a score or -1 to quit"));
}
JOptionPane.showMessageDialog(null, "The average is " + stats.calcMean() + "\nThe stddev is " + stats.calcStandardDeviation());
System.exit(1);
}
}
This code can also be downloaded from the files Statistics.java, Statisticsv2.java, Statisticsv3.java, Statisticsv4.java, Statisticsv5.java, and ExamStatistics.java.