Computer Science 210
Data Structures

Fall 2019, Siena College

GradeRangeCounter BlueJ Project

Click here to download a BlueJ project for GradeRangeCounter.


GradeRangeCounter Source Code

The Java source code for GradeRangeCounter is below. Click on a file name to download it.


GradeRangeCounter.java

/**
 * Example GradeRangeCounter: input a set of scores, track the number of
 * scores which fall into each of several grade ranges as defined by some
 * initialized arrays.
 * 
 * Note: This is a Java application.  In BlueJ, run by choosing "main"
 * when you right-click on the class icon in the main window.
 *
 * @author Jim Teresco, The College of Saint Rose, CSC 252, Fall 2013
 * @version Updated for Fall 2016, CSIS 210, Siena College
 *
 */

import java.util.Scanner;

public class GradeRangeCounter {

    /**
       Count grades that fall into letter grade ranges.

       @param args not used
    */
    public static void main(String[] args) {
       
        // define our grades and thresholds
        final String [] LETTER_GRADES = { "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "F" };
        final double [] THRESHOLDS = { 93.0, 90.0, 87.0, 83.0, 80.0, 77.0, 73.0, 70.0, 67.0, 63.0, 0.0 };
        
        // the array where we'll track the number of grades at each level
        // in Java, these are automatically initialized to 0's
        int [] grades = new int[LETTER_GRADES.length];
        
        // a Scanner to read in our grades from the keyboard
        Scanner keyboard = new Scanner(System.in);
        double grade;
        
        // read grades
        do {
            System.out.print("Next grade?  (Enter value outside 0-100 to quit) ");
            grade = keyboard.nextDouble();
            
            // make sure we're in the legal range
            if (grade >= 0 && grade <= 100) {
                for (int i = 0; i < THRESHOLDS.length; i++) {
                    if (grade >= THRESHOLDS[i]) {
                        grades[i]++;
                        break;  // once we've matched, stop the loop
                    }
                }
            }
            
        } while (grade >= 0 && grade <= 100);
        
        // print the resulting table
        System.out.println("Grade breakdown:");
        for (int i = 0; i < THRESHOLDS.length; i++) {
            System.out.println(LETTER_GRADES[i] + ": " + grades[i]);
        }
    }
}