Computer Science 210
Data Structures

Fall 2016, Siena College

Lab 3: Defining Classes
Due: 10:00 AM, Wednesday, September 28, 2016

In this week's lab, you will practice defining and using custom classes.

You may work alone or with a partner on this lab. Only one submission per group is needed.

Getting Set Up

To get your BlueJ environment set up for this week's lab assignment, start BlueJ and choose "New Project" from the "Project" menu. Navigate to your folder for this course and choose the name "Lab3" (no spaces) for the project.

Create a document where you will record your answers to the lab questions. If you use plain text, call it "lab3.txt". If it's a Word document, you can call it whatever you'd like, but when you submit, be sure you convert it to a PDF document "lab3.pdf" before you submit it.

Further Enhancements to Ratio

For the first part of this lab, you will be working with the Ratios example. The mutator methods you wrote for a recent lecture assignment to multiply a Ratio by an integer and to multiply a Ratio by another Ratio were destructive methods. That is, calling them on an instance of Ratio will modify (i.e., destroy) the value of that Ratio. If the method to multiply a Ratio by an integer is called multBy, the following code segment:

  Ratio r = new Ratio(2, 7);

would result in r having a value of 6/7. r's original value of 2/7 was destroyed by the call to multBy.

Sometimes, we might want to compute something using a Ratio but not change its original value. For example, the method below would be a non-destructive version of multBy:

  public Ratio multByNonDestr(int n) {
    Ratio newRat = new Ratio(numerator*n, denominator);
    return newRat;

This one would be used like this:

  Ratio r1 = new Ratio(2, 7);
  Ratio r2 = r1.multByNonDestr(3);

Now, we still have r1 with its original 2/7 value, and r2 will have the value 6/7.

Question 1: Write a non-destructive method for the Ratio class in the Ratios example that takes a Ratio parameter and adds together the value of this ratio by that ratio, but returns a new Ratio that contains the sum. Neither this ratio (the one whose method you are executing) nor the other (the one you received as a parameter). Recall that you can compute the sum of two fractions a/b and c/d by computing the numerator of the sum as ad + bc, and the denominator as bd. (5 points)

Include your updated in your submission.

Practice with Memory Diagrams

Consider the Ratio class from the Ratios example, with the above mulyByNonDestr method added to it. Also consider this main method:

  public static void main(String args[]) {

    Ratio fred = new Ratio(4, 5);
    Ratio mary = new Ratio(2, 7);
    Ratio alice = mary;
    Ratio joe = alice.multByNonDestr(2);


Question 2: Draw a memory diagram for a program where the main method above is about to return from the call to multByNonDestr. If you draw the diagram by hand (which is perfectly fine) submit a scan or legible photo of your diagram as part of your submission for this lab. (10 points)


Read the topic notes about two-dimensional arrays (linked from the schedule page) and study the Matrix2D example. Don't worry about the details of the exception handling.

Practice Program: Your task is to add the functionality listed below to the Matrix2D example and write code to test it in the main method there. (20 points)

The functionality you are to add:

1 of the points for each of the categories above is for the appropriate tests in main.

Programming Assignment: Tracking Student Grades

Your programming task for this week is to write a program, using a custom class, to extract and print some information from a file of student grading data.

Your program will read a list of student grading information from a file which contains one student's information per line. Each line will consist of a name (always a single word), followed by 3 numbers representing scores on labs, a midterm exam, and a final exam. You may assume there is at least one valid line in the file, but your program should work for any non-zero number of lines. The only prompt you should issue and the only information you should supply to your program using the keyboard is the name of the file that contains all of the student information.

As you read the lines from the file, your program should keep track of the student who has the first name alphabetically, and the highest and lowest score in each category, and the highest and lowest total score (the sum of the three categories). At the end, print out the information for the student whose name/scores was the "winner" in each category. As we did in the class example for items purchased, you should break ties for the "lead" in any category by using the first student with that value for the category.

For example, given the following input file:

Brad 87 45.8 100
Alice 21 11 12
Greg 23 88 32
Bonnie 83 92.8311 88
Jeannie 77 77.77 77.77
Roberta 98 92 100
Cora 95 94.923 90

an appropriate output would look like:

First student, alphabetically:
Alice, labs: 21.00, midterm: 11.00, final: 12.00, total: 44.00
Student with the lowest labs score:
Alice, labs: 21.00, midterm: 11.00, final: 12.00, total: 44.00
Student with the highest labs score:
Roberta, labs: 98.00, midterm: 92.00, final: 100.00, total: 290.00
Student with the lowest midterm score:
Alice, labs: 21.00, midterm: 11.00, final: 12.00, total: 44.00
Student with the highest midterm score:
Cora, labs: 95.00, midterm: 94.92, final: 90.00, total: 279.92
Student with the lowest final score:
Alice, labs: 21.00, midterm: 11.00, final: 12.00, total: 44.00
Student with the highest final score:
Brad, labs: 87.00, midterm: 45.80, final: 100.00, total: 232.80
Student with the lowest total score:
Alice, labs: 21.00, midterm: 11.00, final: 12.00, total: 44.00
Student with the highest total score:
Roberta, labs: 98.00, midterm: 92.00, final: 100.00, total: 290.00


Question 3: Instead of using a class to encapsulate the information about a student, your main method could have kept track of all of the pieces of information about the "leader" in each category in variables of primitive types and/or String references. Compare how the program you wrote and a program written in this alternate manner would affect the code in terms of (i) number of variables needed, (ii) lines of code needed, (iii) readability of the code, and (iv) complexity of updating the program to add a new category, e.g., a programming project. (5 points)


Before 10:00 AM, Wednesday, September 28, 2016, submit your lab for grading. There are two things to do to complete the submission: (i) Copy your file with the answers to the lab questions into your project directory. Be sure to use the correct file name. If you prepared your answers in Word, export to a PDF file and submit that. and (ii) upload a copy of your lab (a .7z or .zip file containing your project directory) to Blackboard under "Lab3". Don't forget to check your programming assignment programs for compliance with the Style Guide for CSIS 210 Programs


This assignment is worth 85 points, which are distributed as follows:

> FeatureValueScore
Lab question 1 (Ratio add method) 5
Lab question 2 (memory diagram) 10
Matrix2D max method 5
Matrix2D scale method 5
Matrix2D multiply method 10
StudentGrades instance variables 3
StudentGrades constructor(s) 2
StudentGrades accessor methods 3
StudentGrades toString method 3
GradeTracker reads file name from keyboard 1
GradeTracker correct file I/O 2
GradeTracker processes one student 2
GradeTracker processes multiple students 3
GradeTracker correct answer in each category 9
GradeTracker report all answers at the end 4
GradeTracker keeps references only to current leaders 3
StudentGrades/GradeTracker comments 5
StudentGrades/GradeTracker good names and declarations 3
StudentGrades/GradeTracker formatting 2
Lab question 3 5
Total 85