15-121 Homework 3: Grade Book - Due 9/29 at midnight
Download and unzip hw3-code.zip, which contains the
start of the Student.java and the GradeBook.java files. The
Student class is based on the Person class from HW2, but
will require substantial change. The GradeBook class is new for this
assignment and replaces the array used in HW2's contact listwith an ArrayList
of Student. There is also a new data file, 121grades.txt to
be used with this assignment. Of course, the grades are made up (don't take
anything personally); you can even change your own grades as you see fit! You
will also have to create a new class completely from scratch,
called Grade.java.
Note: You will be graded in part on your coding style. Your code
should be easy to read, well organized, and concise. You should avoid
duplicate code.
Background: A Grade Book
The purpose of this homework is to give you practice programming with your
first real "data Structure", and an important one in Java,
the ArrayList. You will also obtain practice writing and testing a
class from scratch! In this homework, you will implement a simple gradebook
for a course. The grade book will consist, basically, of
an ArrayList of Students. Each student will be an instance
of the Student class, but you will have to augment the
initial Student class with a private instance variable that can hold
an ArrayList of Grades. There are other, better, ways to
store grades, but it will work for this assignment, whose focus is on
ArrayLists and their operations. You will also need to write
the Grade class.
The Data File format
The data file for this assignment consists of a student's first name, last
name, andrew id, followed by the number of "grade pairs" for that student.
Each "grade pair" consists of the name of the grade (homework, quiz, etc.)
followed by the actual grade for that assignment. To keep things simple,
assume you have only 4 categories of grades: homeworks (HWn), quizzes
(Qn), midterm (MT) and Final (F). See 121grades.txt to see what
all this looks like.
The Grade Class
The Grade class will represent and store information about a single
grade. A grade has two components, a name (stored as a String, e.g.,
HW1), and the actual grade for that assignment (stored as an int).
You will need to write a constructor for this class as well as a few methods
(most likely, you will need a method to get a grade's name and actual value,
get the weight of this grade, and change an actual grade). To get the weight
of a grade, you will need to be able to determine the weight of an assignment
(stored as a double), given its "category" (as determined by the
grade's name). As dictated by the course policy, Homeworks have weight 0.07,
Quizzes have weight 0.015, the Midterm weight is 0.14, and the Final weight is
0.25. You may assume these weights are fixed.
The GradeBook Class
You will need to write the following methods in the GradeBook class:
GradeBook(String fileName)
This constructor needs to
be modified to work with the new file format. In particular, you will need to
decide how to incorporate the reading of the grades. You will want to use the
number of the grade pairs in a loop, but will have to decide how to get the
grades into the ArrayList of Grades in the Student
class. I would suggest building a Student with the names and id and
then create a method called addGrade to add each grade to the
appropriate instance variable of the Student object. YMMV...
String toString()
Needs to be modified to correctly return a properly formatted (spaced) String
that represents the contents (the roster) of the grade book.
void printIndividualGrades(String id)
Prints all the grades, nicely formatted, that are associated with the student
with andrew id id. Prints nothing if no entries match the specified
andrew id. HINT: It will likely be a good idea to write a (private)
helper method called findStudent(id) that returns the index of a
particular student.
void removeStudent(String id)
Removes the entry for the student with andrew ID, id, from the roster,
using the appropriate ArrayList method. If there is no such
student, prints an appropriate error message.
void changeGrade(String id, String assignment, int newgrade)
If a student with andrew ID, id, exists, changes the grade associated
with assignment to newgrade. Prints an appropriate error
message if either the id or assignment cannot be found.
void addGradeToAll()
Prompts the user for a new assignment to add to all the students, then loops
through all the students, asking the user for a grade for the new assignment.
Updates each Student object by adding the new assignment name and
grade to the ArrayList of Grades for that student.
void printCurrentGrade(String id)
Searches for the student with the specified andrew id and prints the current
letter grade for that student, if found. If there is no such student, prints
an appropriate error message. The current letter grade for a student can be
determined by multiplying the actual grade times the weight for each
assignment, summing all of these multiplications, and then dividing by the
sum of all the weights. Then apply the standard 90 and above is A, 80 to 89
is B, etc.
double whatDoINeed(String id, char grade)
You're almost done! This one is a lot like printCurrentGrade() except
once you calculate the student's current score, you need to see what they need
to average over the rest of the course to get the desired grade. Of course, if
there is no student with this id, it should (yet again) print an error
message. The calculation is fairly straightforward: you need to determine a
student's current score (similar to printCurrentGrade()), determine
the total of the weights consumed to this point in the course (total), and how
much weight is left (rest). Note that total + rest must equal 1. So if you
have the student's current score (score) out of 100, and a target grade ("A"
requires a minimum of 90, "B" a minimum of 80, and so on), what a student
needs to make that target can be expressed by the following formula:
need = (target - score * total)/rest.
void updateDatabase(String filename)
Creates a file with name provided by filename and writes the contents
of the grade book to this file. This file must
be readable by the GradeBook constructor!
Some of the methods you will write in the GradeBook class will
require that you change/add methods in the Student class. Others may
require a different getter or setter.
Of course, you will also need to test your methods by calling them from main.
Submitting Your Work
When you have completed this assignment and tested your code thoroughly,
create a .zip file with your work (including the Grade.java,
Student.java and GradeBook.java). Name the zip file
"your-last-name".zip and email it to
me mjs@cmu.edu.
Make sure to keep a copy of your work just in case something goes wrong!