Computer Science 252
Problem Solving with Java

Fall 2015, The College of Saint Rose

Lab 12: ArrayList Practice
Due: 11:59 PM, Thursday, November 19, 2015

For this lab, you will complete the functionalty of a few practice programs by adding and using ArrayLists.

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

Getting Set Up

Download the a starter project with partial implementations of these programs from here.

Create a document where you will record your answers to the lab questions. If you use plain text, call it "lab12.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 "lab12.pdf" before you submit it.

Working solutions to all programs can be downloaded here.

Practice Program: Enhancing the CourseGrades Example

In the starter, open the BlueJ project for the copy of the CourseGrades example, and read through the existing code to make sure you understand how it all works.

Organization of the Program

Before you can make any modifications, it's important to understand how the example as seen in class and in the starter code works.

The program is written in two classes: CourseGrades and StudentGrades. The CourseGrades class is responsible for tracking all of the students whose grades are stored in the system, and the StudentGrades class is responsible for tracking the information about one student (in this case, his/her name and list of grades).

The program operates through a main method in the CourseGrades class. However, since the main method in a Java program is required to be declared at static, meaning it has no access to instance variables, and we would like to use CourseGrades as a custom class that stores its information in instance variables, the main method begins by constructing an instance of CourseGrades it calls cg. The main method can then interact with that instance of the CourseGrades object to track all of the student information for the course.

The CourseGrades class has just one instance variable: an ArrayList of StudentGrades objects. There is one entry in this list for each student who has information in the system. There are a set of methods through which someone who has a reference to an instance of a CourseGrades object (as the main method does in its variable cg) can interact with it.

The StudentGrades class has two instance variables: one for the name of the student (stored in a String) whose grades a particular instance is responsible for, and one for the list of that student's grades (stored in an ArrayList of Double). It provides methods through which one can interact with that student's information. To start, it has a constructor, a method to add a new grade, an accessor for the name, an equals method that checks if an instance represents a particular student (by name), and a toString method that can be used to print out the student's name and grades in a human-readable format.

The interactive loop in the CourseGrades main method reads commands from the keyboard, interprets them, and performs the requested action. The big if..else if..else block has an entry to handle each command the program understands. The commands you'll be adding are less complicated than the add command, so we will examine how that works in the most detail.

The add command's functionality is broken into parts, each of which is the responsibility of a different part of the program:

  1. The main method's loop, the case where it is determined that the command entered starts with the word "add", is responsible for parsing the remainder of the command, making sure all required information is provided, and then passing that information on to its cg instance of CourseGrades if the required information is provided. A correctly-specified add command has two additional pieces of information in the command: a number representing a new score, and the name of the student whose score is being added. The implementation here used a Scanner over a String to accomplish this, but there are many ways Java can do the same thing. If any information was missing, an appropriate error message is printed, and a continue statement causes the program to jump back to the top of the "while(!done)" loop. If all information is specified, we would have a grade in the local variable grade and a name in the local variable name to be passed on to the add method of cg, the main method's instance of a CourseGrades object.
  2. The CourseGrades add method is responsible for adding the given grade to a StudentGrades object for the student with the given name. The list of all StudentGrades objects is stored in the instance variable studentList, so the task comes down to two cases: (i) if there is already an entry for this student in the list, get that entry and add this grade to it, and (ii), if there is no entry for thie student, create a new entry and add this grade to it. The search for a record with the given student's name is done by the private helper method findStudentGradesByName (which you should use as well in the methods you'll be adding). If that method returns a StudentGrades object reference, it means we already had a record for a student with that name, so we just add the grade to the existing record. To do this, we call that object's addGrade method. If the findStudentGradesByName method returns null, we need to construct a new StudentGrades object, after which we can call its addGrade method to store the grade. Note that we also need to add the new StudentGrades object to the studentList.
  3. The addGrades method of the StudentGrades class is responsible for adding one grade to the given object's list of grades. That's easy enough: we just add it to the ArrayList. Notice that the rest of the program does not know or care how a StudentGrades stores, updates, and accesses its list of grades. All of that is private to the class. Other parts of the program only interact via the public constructor and methods.

In your enhancements below, each command will require more to be done in some or all of the above parts of the program. In each case, you will add code to parse a new command in the main method. In each case, you will add a new method of CourseGrades that does the work that requires access to the studentList to look up and/or modify information about the list of students. In all except the delete command, you will add new methods to the StudentGrades class that can be called from your new method of the CourseGrades class when you need details of information from a given student. For example, you might (ok, will) need a new method there that computes the average of the grades stored in that StudentGrades object.

Your Enhancements

You will be making several enhancements to this program:

Practice Program: Finishing a Drawing Program

In the starter, open the BlueJ project for the DotsList program, and read through the existing code to make sure you understand how it all works. You will find the framework of the program that includes some of the code to do the initial setup, including everything that sets up and manages the drop-down menus, and comments indicating where you should make your changes to complete the program.

The operation of the program is straightforward. Three drop-down menus located at the bottom of the canvas select the operation and the size and color to be used in that operation. When the user presses the mouse on the canvas, the appropriate action is taken.

Your primary task is to declare, construct, and manage an ArrayList that will keep track of the dots on the canvas.

Some constants and instance variables are provided; you will need to provide more.

The begin method adds the drop-down menus to the bottom of the window. You need not worry about the details here (though you are welcome to look at how these menus are used if you are interested). At the end of the constructor, you will likely need to construct your ArrayList and initialize some instance variables.

The onMousePress method will call one of four methods, depending on which of the four options is selected in the first drop-down menu. You may or may not need to add any additional code to onMousePress.

The four methods called by onMousePress will form the bulk of your task: addNewDotAt, recolorDotAt, deleteDotAt, and selectDotAt. Be sure to make use of the code commented out in addNewDotAt which selects the appropriate size based on the middle drop-down menu. Also, a completed version of a getSelectedColor menu is provided: use it! The skeletons of another helper method getIndexOf is also provided. Implement this one first - you will use it in many other places.

Finally, a skeleton of the onMouseDrag method is provided where you will complete the usual object dragging code.

When you start coding, work on adding a dot first, then recoloring, then selecting (and the corresponding dragging), and finally, deleting. Get one part working before worrying about the next!

Submitting

Before 11:59 PM, Thursday, November 19, 2015, submit your lab for grading. There are three things you need 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. (ii) Email a .7z or .zip file containing your project directory to terescoj AT strose.edu. (iii) Demonstrate the execution of your programs for your instructor. (2 business day grace period for demos).

Grading

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

> FeatureValueScore
CourseGrades Program Enhancements
CourseGrades average command 5
CourseGrades delete command 5
CourseGrades stats command (basic) 4
CourseGrades stats command handles ties 3
CourseGrades updates to help command 2
Lab question 2
DotsList Correctness
ArrayList declaration and construction 3
Adding new dot 5
Recolor existing dot 5
Drag existing dot 5
Delete existing dot 5
Selection always uses dot "on top" 3
Selected dot moves to top of stacking order 3
Total 50