Summer 2014, The College of Saint Rose

Lab 4: Collections
Due: 4:30 PM, Tuesday, June 17, 2014

In this week's lab, you will practice with ArrayLists and arrays.

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 lecture assignment and 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.

Lecture Assignment Questions

We will usually discuss these questions at the start of class on the lab due date, so no credit can be earned for late submissions of lecture assignment questions.

LA Question 1: Write a method that takes an ArrayList of Integer values as its parameter and returns the sum of all elements in the array. (3 points)

LA Question 2: Gaddis Algorithm Workbench exercise 2, p 532. (1 point)

LA Question 3: Gaddis Algorithm Workbench exercise 3, p 532. (3 points)

LA Question 4: Write a traditional for loop that prints out every element of an ArrayList a. (2 points)

LA Question 5: Write a for-each loop (Gaddis refers to this type of loop as the "enhanced for loop") that prints out every element of an ArrayList a. (2 points)

LA Question 6: Write a short program (or modify an existing example) so that it attempts to get a non-existent element from an ArrayList. For example, if you have an ArrayList of Integer that contains 4 Integer elements, you would attempt to get a value from the array from a position numbered 4 or higher. What happens when you run such a program? (3 points)

Practice Programs

Practice Program: Gaddis Programming Challenge 13, p. 538. Name your program that creates the ArrayList of PhoneBookEntry objects PhoneBook.java. (6 points)

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

If the student is not found, print an appropriate error message.

Question 1: Did you add a new field (i.e., instance variable) to the StudentGrades class? 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
```

If the student is not found, print an appropriate error message.

• 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.

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

Before 4:30 PM, Tuesday, June 17, 2014, submit your lab for grading. There are two things you need to do to complete the submission: (i) Copy your file with the answers to the lecture assignment and 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. (ii) Upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab4".