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);
r.multBy(3);
```

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)

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);
fred.setNumerator(2);
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)

Matrices

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:

• A method max that returns the largest number in the matrix (5 points)
• A destructive method, that is, a method that modifies the matrix on which it is called, named scale that multiplies each entry in the matrix by a number (5 points)
• A non-destructive method, that is, one that does not change the matrix on which it is called, named multiply that works much like add but computes the matrix-matrix multiplication result. If you don't remember how to multiply matrices, see the "Matrix Product" section of the Wikipedia entry to Matrix multiplication. (10 points)

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

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
```

Notes:

• Define a class StudentGrades that holds the name and grade information for a single student. Include an appropriate constructor, all needed accessors, and a toString method.
• Two String objects can be compared based on alphabetical order using the String method compareTo. See the String documentation for more details.
• All information about the student who "wins" each category should be printed. Fortunately, if you define a class with all of that information contained, and you define an appropriate toString method, this becomes easy.
• You do not need nor should you use any arrays or ArrayLists to keep track of all students - only remember references to the objects representing the student that is leading in each category.
• All scores should be printed with 2 digits after the decimal point.
• Please name your class that contains the main method that reads the input file and finds and reports the list of winners GradeTracker.

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)

Submitting