Fall 2016, Siena College

Lab 4: ArrayList Practice
Due: 10:00 AM, Wednesday, October 5, 2016

This week's lab is a chance to practice a bit more using custom classes and the ArrayList ADT. While it is due at the usual time, hopefully you can finish up sooner to prepare for the exam.

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 "Lab4" (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 "lab4.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 "lab4.pdf" before you submit it.

More Matrix2D

This problem builds on the Matrix2D program from last week. Start with your version of Matrix2D.java that includes the matrix-matrix multiply method.

Practice Program: In a new class called Matrix2DList, write a main method that creates an arbitrarily-long list of matrices, then reports their sum and product. Specifics below. (20 points)

Your program should behave as follows:

1. Prompt for and read the matrix size to be used for all of the matrices.
2. Prompt for and read the range of values to be used to fill the first matrix with random values.
3. Create the first matrix and add it to the matrix list (which should be an ArrayList).
4. Ask repeatedly if more matrices should be created, and as long as there should be more, prompt again for the next range of values, create a matrix with values in that range, and add it to the matrix list.
5. Once no more matrices are requested, print the number of matrices that have been created, print each matrix, then compute and print the sum of the matrices and the product of the matrices.

A sample run of the program follows:

```What size should the matrices be? 4
Enter upper and lower bounds for values of the next matrix: 0 10
More matrices?  (yes or no) yes
Enter upper and lower bounds for values of the next matrix: -5 5
More matrices?  (yes or no) yes
Enter upper and lower bounds for values of the next matrix: 100 200
More matrices?  (yes or no) no
You created 3 matrices.
Matrix 0:
9.68220530639116 7.345769488480678 1.7412544053873813 2.3438316061712605
3.1944624268586654 3.9513050924461313 4.267474531699351 2.583640677971447
4.641348346843794 3.5690041627188753 3.3537004228656233 6.340187059095067
2.098098670498576 7.140456953926631 7.855035765375354 2.5806587268338586

Matrix 1:
4.806713489353562 0.4135288039878944 1.2768161447453794 -4.068342594706581
3.6280471321371586 -2.5156720232308336 4.048496011548281 4.266635696913379
-3.039326003927366 1.1870583652796984 -4.750758409032128 0.5086397570974199
2.3554889820951885 -0.6787271394749981 2.6905416351814733 2.3593801046651217

Matrix 2:
149.83228384235943 130.59594399972167 158.3705270054091 169.4955788825903
110.50901258217934 185.2866551484843 149.6145308945793 120.91463311180807
180.23028479845289 177.20036526697353 151.46037517431358 174.51331981681983
125.26086320907847 112.274872082545 158.7914846584583 172.05826577250718

164.32120263810415 138.35524229219024 161.38859755554185 167.771067894055
117.33152214117516 186.7222882176996 157.93050143782693 127.7649094866929
181.8323071413693 181.95642779497211 150.06331718814707 181.36214663301232
129.71445086167222 118.73660189699663 169.33706205901512 176.99830460400617

16482.548286913046 13922.983840001521 15352.500605703277 17474.653424398755
5567.009861117915 4553.491999143645 5766.601007853109 6489.223728863248
10682.275123478916 9126.782807692354 10552.788438086425 11877.89036959422
5902.680161976161 4421.1764573063265 6724.206610818697 7652.617790407841
```

Programming Assignment: Enhancing the CourseGrades Example

Start by making your own copy of the CourseGrades example, and read through the existing code to make sure you understand how it all works.

It is very important to make sure you understand which parts of the program are responsible for which computation and for storing the data. There are three major sections of the program:

1. The main method in CourseGrades is static, meaning it has no access to the instance variable of the class. Therefore, it created an instance of CourseGrades, storing it in its local variable cg. main is then responsible for creating prompting the user for each command, parsing that command, and, in the case of a valid command, calling a method of its instance of CourseGrades (cg) that can then access the instance variable to perform the requested action.
2. The non-static methods of CourseGrades are responsible for the access and modifications to the studentList instance variable. In most cases, it looks up an existing or creates a new instance of StudentGrades, then calls one or more methods of the StudentGrades object that represents the information about the student being considered.
3. The StudentGrades class, which has instance variables and methods responsible for maintaining the information about one student.

A memory diagram of the variables in existience after the following commands are entered:

```add 100 carrie
```

and the program is currently executing the line

```    cg.add(name, grade);
```

in main, the (second) line

```    sg.addGrade(grade);
```

in the add method of CourseGrades, and has just finished the line

```    grades.add(grade);
```

Do not start making modifications until you understand the breakdown and memory diagram above.

You will be making several enhancements to this program:

• Add a command to print out a student's average across all course grades. So if a student named Alice had course grades of 78.3, 98.4, and 88.8, the command
```average Alice
```

would print

```Alice has an average of 88.5.
```

Print the average with one digit after the decimal point.

Question 1: Did you add a new field (i.e., instance variable) to the StudentGrades class to store the average? Why or why not? What would you need to do differently if you chose the opposite approach? (4 points)

• Add a command to delete a student from the system:
```delete Bob
```

• Add commands to save all student and course grade information to a file, to load that grade information from a file (replacing all existing student information), and to merge in that grade information from a file (adding the students and grades to the existing data).

The command

```save somegrades.dat
```

would save all of the student and grade information to the file somegrades.dat, and the command

```load somegrades.dat
```

would load in the file, replacing any data currently in the program, and

```merge somegrades.dat
```

would load in the file, adding to any existing data currently in the program.

You may choose your own appropriate file format.

You need not handle I/O exceptions in a nice way here. Just add "throws IOException" to the method headers of your main method and any others that create or user file Scanners or PrintWriters.

Question 2: Describe your file format and why you chose the format you did. (2 points)

• Add a command "stats" to print the student(s) with the highest and lowest overall averages, and the highest and lowest grade for any individual course. Unlike in some previous programs, you need to handle ties here (consider introducing some ArrayLists to keep track of multiple students who had the same "top" scores).

Submitting