Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

CourseGrades BlueJ Project

Click here to download a BlueJ project for CourseGrades.


CourseGrades Source Code

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


CourseGrades.java

/*
 * Example CourseGrades: a program to keep track of course
 * grades for a group of students.  Uses ArrayLists in a few
 * ways.
 *
 * Jim Teresco, The College of Saint Rose, CSC 523, Summer 2014
 *
 * $Id: CourseGrades.java 2377 2014-06-10 03:12:01Z terescoj $
 */

import java.util.ArrayList;
import java.util.Scanner;

public class CourseGrades {

    // we will have some instance variables to track the 
    // contents of the list of students and their grades
    // and a set of methods to interact with the list, which
    // is done from main, below.
    private ArrayList<StudentGrades> studentList;
    
    // construct one of these
    public CourseGrades() {
        
        studentList = new ArrayList<StudentGrades>();
    }
    
    // method to add a grade for a (possibly new) student
    public void add(String name, double grade) {
        
        // use the private helper method to look up
        StudentGrades sg = findStudentGradesByName(name);
        if (sg != null) {
            sg.addGrade(grade);
        }
        else {
            sg = new StudentGrades(name);
            sg.addGrade(grade);
            studentList.add(sg);
        }
    }
    
    // print out all of the students and their grades
    public void printAll() {

        for (StudentGrades sg : studentList) {
            System.out.println(sg);
        }
    }
    
    // private helper method to look up the StudentGrades object
    // from the list that matches the name, returns null if none
    private StudentGrades findStudentGradesByName(String name) {
        
        // this is a good place to use a for-each loop
        for (StudentGrades sg : studentList) {
            if (sg.getName().equals(name)) return sg;
        }
        
        // if we got here and never found it, return null
        return null;
    }
    
    // main driver method for the program's interaction with the keyboard
    // and terminal
    public static void main(String[] args) {
        
        // create an instance of this class that we can interact with
        // in our main loop below
        CourseGrades cg = new CourseGrades();
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Welcome to our interactive course grade tracker.");
        System.out.println("Enter \"help\" to see a list of commands.");
        // we will have a main interactive loop here that
        // reads commands from the keyboard and interacts
        // with the CourseGrades instance
        boolean done = false;
        
        while (!done) {
            System.out.print("> ");
            String command = keyboard.nextLine();
            
            // see which command, if any, was entered, and process it
            if (command.equals("quit")) {
                done = true;
            }
            else if (command.equals("help")) {
                System.out.println("Available commands:");
                System.out.println("add score name -- add score to name's record");
                System.out.println("list -- list all students with their scores");
                System.out.println("help -- describe usage");
                System.out.println("quit -- exits program");
            }
            else if (command.startsWith("add")) {
                // add a new grade for a (possibly new) student
                // rest of line should be a grade and a name
                Scanner line = new Scanner(command);
                line.next();  // skip over "add"
                if (!line.hasNextDouble()) {
                    System.out.println("add command requires a grade and a name!");
                    continue;  // jump back to the top of the while loop immediately
                }
                double grade = line.nextDouble();
                if (!line.hasNext()) {
                    System.out.println("add command requires a grade and a name!");
                    continue;  // jump back to the top of the while loop immediately
                }
                String name = line.next();
                // finally, we can add it!
                cg.add(name, grade);
            }
            else if (command.equals("list")) {
                cg.printAll();
            }
            else {
                System.out.println("Unknown command, \"help\" for a list of commands");
            }
            
        }
        
    }
}

StudentGrades.java


/**
 * A class to track a student's name and a list of numeric grades for
 * that student
 * 
 * @author Jim Teresco
 * 
 * $Id: StudentGrades.java 2377 2014-06-10 03:12:01Z terescoj $
 */

import java.util.ArrayList;

public class StudentGrades {
    
    // we have a student's name and a list of that student's grades
    // as the fields for this class
    private String name;
    private ArrayList<Double> grades;
    
    // construct a new student, who has no grades when starting out
    public StudentGrades(String name) {
        
        this.name = name;
        grades = new ArrayList<Double>();
    }
    
    // add a grade to a StudentGrades
    public void addGrade(double grade) {
        
        grades.add(grade);
    }

    // name accessor
    public String getName() {
        
        return name;
    }
    
    // we will define two StudentGrades objects as equal
    // if they have the same name field
    public boolean equals(Object o) {
        
        StudentGrades other = (StudentGrades)o;
        return other.name.equals(name);
    }
    
    // a meaningful toString method
    public String toString() {
        StringBuilder s = new StringBuilder(name + ":");
        for (double score : grades) {
            s.append(" " + score);
        }
        return s.toString();
    }
}