Java – Exam Stats Example

This post is another in the series of small Java Programs and their evolution.  The scenario for the program today, Exam Statistics, is calculating a couple statistics for a set of grades entered by the user. You can see in the code below, I work on one method, test it, then move on to the next method.

The program still has some areas for improvement.  For example, test grades should only fall between 0 and 100, or maybe 150 in the case of extra credit questions or something.

Upon reflection, I should have put more comments into the code as I was writing it.  It doesn’t make a ton of sense to write a bunch of code and then go back and comment it

Version 0.1

Basically, the goal of the first version was to get the project infrastructure set up (imports, class, main method), implement a method to print a welcome message, and get the user to enter the number of students.

Things to notice:

  • the Scanner is declared at the class level as a static var.
  • in the main method, I call the functions I’ve implemented, then I print out the result as a way of testing my code.
/**
 * Exam Statistics will help a teacher average a set of grades for the students in a class.
 * The teacher will enter the number of students and then the program will ask for the grades
 * for each student.  It will then print the average of the grades to the screen
 */

import java.util.Scanner;

public class ExamStatistics {

  public static Scanner s = new Scanner(System.in);

  public static void printWelcomeMessage() {
    System.out.println("Welcome to the Exam Statistics Program. It will ");
    System.out.println("allow you to enter the grades for an exam and will ");
    System.out.println("print some statistics about the exam. ");
    System.out.println();
  }

  public static int getNumberOfStudents() {
    int numStudents = 0;
    System.out.print("How many students are there in your class? --> ");
    numStudents = s.nextInt();
    return numStudents;  //TODO: Why not return s.nextInt()?
  }

  public static void main(String[] args) {

    printWelcomeMessage();
    int numStudents = getNumberOfStudents();
    
    System.out.println("Num students entered: " + numStudents);

  }

}

Version 0.2

The next milestone was to implement functionality to get the individual test grades from the user.

/**
 * Exam Statistics will help a teacher average a set of grades for the students in a class.
 * The teacher will enter the number of students and then the program will ask for the grades
 * for each student.  It will then print the average of the grades to the screen
 */

import java.util.Scanner;

public class ExamStatistics {

  public static Scanner s = new Scanner(System.in);

  public static void printWelcomeMessage() {
    System.out.println("Welcome to the Exam Statistics Program. It will ");
    System.out.println("allow you to enter the grades for an exam and will ");
    System.out.println("print some statistics about the exam. ");
    System.out.println();
  }

  public static int getNumberOfStudents() {
    int numStudents = 0;
    System.out.print("How many students are there in your class? --> ");
    numStudents = s.nextInt();
    return numStudents;  //TODO: Why not return s.nextInt()?
  }

  public static int[] getGradeData(int numStudents) {
    int[] grades = new int [numStudents];
    for (int i = 0; i < numStudents; i++) {
      System.out.print("Enter grade for student " + (i + 1) + " --> ");
      grades[i] = s.nextInt();
    }
    return grades;
  }

  public static void main(String[] args) {

    printWelcomeMessage();
    int numStudents = getNumberOfStudents();
    
    //get the grades for this exam
    int grades[] = getGradeData(numStudents);
    
    for (int i = 0; i < grades.length; i++) {
      System.out.println("grade[" + i + "] = " + grades[i]);
    }


  }

}

 

Version 0.3

In the next milestone, I added an averaging function.

/**
 * Exam Statistics will help a teacher average a set of grades for the students in a class.
 * The teacher will enter the number of students and then the program will ask for the grades
 * for each student.  It will then print the average of the grades to the screen
 */

import java.util.Scanner;

public class ExamStatistics {

  public static Scanner s = new Scanner(System.in);

  public static void printWelcomeMessage() {
    System.out.println("Welcome to the Exam Statistics Program. It will ");
    System.out.println("allow you to enter the grades for an exam and will ");
    System.out.println("print some statistics about the exam. ");
    System.out.println();
  }

  public static int getNumberOfStudents() {
    int numStudents = 0;
    System.out.print("How many students are there in your class? --> ");
    numStudents = s.nextInt();
    return numStudents;  //TODO: Why not return s.nextInt()?
  }

  public static int[] getGradeData(int numStudents) {
    int[] grades = new int [numStudents];
    for (int i = 0; i < numStudents; i++) {
      System.out.print("Enter grade for student " + (i + 1) + " --> ");
      grades[i] = s.nextInt();
    }
    return grades;
  }

  public static double averageGrades(int[] scores){
    double avg = 0.0;
    int sum = 0;
    for (int i = 0; i < scores.length; i++) {
      sum += scores[i];
    }
    avg = ((double)sum)/scores.length;
    return avg;
  }


  public static void main(String[] args) {

    printWelcomeMessage();
    int numStudents = getNumberOfStudents();
    
    //get the grades for this exam
    int grades[] = getGradeData(numStudents);

    for (int i = 0; i < grades.length; i++) {
      System.out.println("grade[" + i + "] = " + grades[i]);
    }

    double testAverage = averageGrades(grades);

    System.out.println("The average is " + testAverage + ".");

  }

}

Version 0.4

Here, you’ll see I’ve added functionality for calculating standard deviation.  See this link if you need a refresher on what standard deviation is.

/**
 * Exam Statistics will help a teacher average a set of grades for the students in a class.
 * The teacher will enter the number of students and then the program will ask for the grades
 * for each student.  It will then print the average of the grades to the screen
 */

import java.util.Scanner;

public class ExamStatistics {

  public static Scanner s = new Scanner(System.in);

  public static void printWelcomeMessage() {
    System.out.println("Welcome to the Exam Statistics Program. It will ");
    System.out.println("allow you to enter the grades for an exam and will ");
    System.out.println("print some statistics about the exam. ");
    System.out.println();
  }

  public static int getNumberOfStudents() {
    int numStudents = 0;
    System.out.print("How many students are there in your class? --> ");
    numStudents = s.nextInt();
    return numStudents;  //TODO: Why not return s.nextInt()?
  }

  public static int[] getGradeData(int numStudents) {
    int[] grades = new int [numStudents];
    for (int i = 0; i < numStudents; i++) {
      System.out.print("Enter grade for student " + (i + 1) + " --> ");
      grades[i] = s.nextInt();
    }
    return grades;
  }

  public static double averageGrades(int[] scores){
    double avg = 0.0;
    int sum = 0;
    for (int i = 0; i < scores.length; i++) {
      sum += scores[i];
    }
    avg = ((double)sum)/scores.length;
    return avg;
  }

  public static double calcStandardDeviation(int[] scores, double average) {
    double stdDev = 0.0;

    //calculate the sum of squares between test value and mean for test
    double sumSquares = 0.0;
    for(int i = 0; i < scores.length; i++) {
      sumSquares += Math.pow((scores[i] - average), 2);
    }

    //calculate the variance as the sum of squares divided by the
    //number of exams
    double variance = sumSquares/scores.length;

    //standard deviation is the square root of the variance
    stdDev = Math.sqrt(variance);

    return stdDev;
  }

  public static void main(String[] args) {

    printWelcomeMessage();
    int numStudents = getNumberOfStudents();
    
    //get the grades for this exam
    int grades[] = getGradeData(numStudents);

    for (int i = 0; i < grades.length; i++) {
      System.out.println("grade[" + i + "] = " + grades[i]);
    }

    double testAverage = averageGrades(grades);
    double stdDev = calcStandardDeviation(grades, testAverage);
    System.out.printf("The average is %5.2f.\n", testAverage);
    System.out.printf("The std dev is %5.2f.\n", stdDev);

  }

}

Version 0.5

In the last set of edits, I cleaned up the code a bit.

/**
 * Exam Statistics will help a teacher average a set of grades for the students in a class.
 * The teacher will enter the number of students and then the program will ask for the grades
 * for each student.  It will then print the average and standard deviation of the grades to
 * the screen
 */

import java.util.Scanner;

public class ExamStatistics {

  public static Scanner s = new Scanner(System.in);

  public static void printWelcomeMessage() {
    System.out.println("Welcome to the Exam Statistics Program. It will ");
    System.out.println("allow you to enter the grades for an exam and will ");
    System.out.println("print some statistics about the exam. ");
    System.out.println();
  }

  public static int getNumberOfStudents() {
    int numStudents = 0;
    System.out.print("How many students are there in your class? --> ");
    numStudents = s.nextInt();
    return numStudents;  //TODO: Why not return s.nextInt()?
  }

  public static int[] getGradeData(int numStudents) {
    int[] grades = new int [numStudents];
    for (int i = 0; i < numStudents; i++) {
      System.out.print("Enter grade for student " + (i + 1) + " --> ");
      grades[i] = s.nextInt();
    }
    return grades;
  }

  public static double averageGrades(int[] scores){
    double avg = 0.0;
    int sum = 0;
    for (int i = 0; i < scores.length; i++) {
      sum += scores[i];
    }
    avg = ((double)sum)/scores.length;
    return avg;
  }

  public static double calcStandardDeviation(int[] scores, double average) {
    double stdDev = 0.0;

    //calculate the sum of squares between test value and mean for test
    double sumSquares = 0.0;
    for(int i = 0; i < scores.length; i++) {
      sumSquares += Math.pow((scores[i] - average), 2);
    }

    //calculate the variance as the sum of squares divided by the
    //number of exams
    double variance = sumSquares/scores.length;

    //standard deviation is the square root of the variance
    stdDev = Math.sqrt(variance);

    return stdDev;
  }

  public static void main(String[] args) {

    printWelcomeMessage();
    int numStudents = getNumberOfStudents();
    
    //get the grades for this exam
    int grades[] = getGradeData(numStudents);

    //calculate stats on the grades entered
    double testAverage = averageGrades(grades);
    double stdDev = calcStandardDeviation(grades, testAverage);

    //display the stats to the user 
    System.out.printf("The average is %5.2f.\n", testAverage);
    System.out.printf("The std dev is %5.2f.\n", stdDev);

  }

}

 

 

Speak Your Mind

*

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.