Computer Science 252
Problem Solving with Java
Fall 2013, The College of Saint Rose
GradeRangeCounter Demo
A working demo of GradeRangeCounter will appear below. Click inside the applet to interact with it.
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.
/* * 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 rather than an Applet. In BlueJ, run * by choosing "main". * * Jim Teresco, The College of Saint Rose, CSC 252, Fall 2013 * * $Id: templateapp.java 2238 2013-10-30 20:43:55Z terescoj $ */ import java.util.Scanner; public class GradeRangeCounter { public static void main(String[] args) { // define our grades and thresholds final String [] LETTER_GRADES = { "A", "A-", "B+", "B", "B-", "C+", "C", "D", "F" }; final double [] THRESHOLDS = { 93.0, 90.0, 87.0, 83.0, 80.0, 77.0, 73.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]); } } }