Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

StudentAverages BlueJ Project

Click here to download a BlueJ project for StudentAverages.


StudentAverages Source Code

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


StudentAverages.java


/**
 * Example program for CSC 202
 * 
 * Compute student exam averages from data entered at the keyboard
 * 
 * @author Jim Teresco
 * @version $Id$
 * 
 **/

import java.util.Scanner;

public class StudentAverages {

    public static void main(String[] args) {
        // define the number of students and number of exams here
        final int NUM_STUDENTS = 5;
        final int NUM_EXAMS = 3;

        // create a Scanner to read keyboard input
        Scanner keyboard = new Scanner(System.in);

        // read name and grades, calculate avg for each student
        for (int student = 1; student <= NUM_STUDENTS; student++) {

            // read in the student's name from the keyboard
            System.out.print("Enter student " + student + "'s name: ");
            String name = keyboard.nextLine();

            // print a blank line
            System.out.println();

            double studentTotal = 0.0;

            // process exam scores
            for (int exam = 1; exam <= NUM_EXAMS; exam++) {

                // now read scores for each test
                System.out.print("Enter exam " + exam + " score for " + name + ": ");

                double examScore = keyboard.nextDouble();

                // add to running total for this student's grades
                studentTotal = studentTotal + examScore;
            }

            // we have this student's total, print the results
            // first get rid of the extra "newline" on the input (more later on this)
            keyboard.nextLine();
            double studentAverage = studentTotal / NUM_EXAMS;

            // print out student average
            System.out.println(name + "'s average is " + studentAverage);
            // and an extra blank line
            System.out.println();

        }
    }
}