Name______________________________This lab covers some of the important array concepts in Java. You should print a copy of this lab so you can record your answers to questions. Turn in the completed questions and any supporting printouts as required in the lab. Here is a link to the API documentation in case you need it.
P1: Parallel Arrays - Create a folder and project and copy this class into a new file. This program stores course grade information in parallel arrays. Read through the program, make sure the program runs, and then go on to the next step.
Oh - you need a data file! You should create a file in the same folder as the program's class files. Name it ... well, you can read the code to see what it needs to be named! You can paste the contents from here.
Complete the method named gpa() to calculate (and return) the GPA. The GPA is the weighted average of the course grades. For each course, calculate its quality points (grade*credits). Sum these to get total quality points. Divide by the total number of credits. You should use the numericEquivalent method to convert the letter grade to a number. Read that function and then explain how it works:
Explanation:
Why is this method declared static?
The use of parallel arrays is difficult because it relies on every section of code that alters the list to be extremely careful to keep the arrays synchronized. If the data for a class is to be removed from the collection, you must be careful to remove the corresponding grade and credit data, and slide data in the same way in all the arrays. If any of the data gets out of order, the association between the arrays is lost.
Removing information from an array is more difficult that removing from an ArrayList. Notice that the number of items in these arrays is always stored in the data member, numGrades. To clear the arrays of all data, all you need to do is set numGrades to 0. That is all! Nothing else is required. To remove one item, we can simply decrement numGrades; this will indeed remove one element. But which one? Try it! Implement remove to simply decrement numGrades and run the program. You should uncomment one of the removes.
Decrementing the count is only one of the steps. You will also need to ensure that the items to be retained occupy the initial segment of the array (0 through numGrades-1). This requires some work. Programmers refer to it as sliding part of the array over. That is, a[i]=a[i+1] for a bunch of values of i. The remove method will need to do this. Be sure to slide in parallel. Whatever you do to one array must be done to the parallel ones.
Implement the remove method that searches for a course name (provided as an argument to the method). Write the search as a loop that terminates if the name is found or everything has been checked. Follow the search loop with a slide over loop (if found) to remove the item and its parallel data. Uncomment the statements in main that will remove the first and last classes in the list and then redisplay the course data and gpa. Check that the remove method works correctly. You might check one of the middle courses as well.
This program only handles data for one student. Consider the difficulty in expanding this parallel array structure to handle more than one student. Would we create an array of parallel arrays? Hmmm... sounds like fun. Read on!
P2: Replacing Parallel Arrays - Create a new file containing a class named Course. This will store the name of a course, the credit hours and the letter grade for the course. Provide public accessors and a constructor that requires all three items to be specified when a Course object is created. Include a public method that can return the quality points for a course (credit hours*numeric grade). This method should use the static method in your other class.
Make a copy of the original program (renaming it appropriately), and then modify it to use a single array of Course objects to hold course data and calculate GPA's. Test that everything works as before, including the remove method. You will need to make modifications in nearly every method. Reflect on the simplifications you have made.
Print all three files.
P3: Multidimensional Arrays - In this exercise we will simulate the Game of Life (read about it here). Create a new file in your folder and copy the provided code into it. The basic GUI has been written for you. It handles the screen display and mouse interface. You can left-click on the grid to change any cell. A slider and button are provided to animate the display.
Your job is to complete the methods that support the underlying simulation. You will find instructions in each of the methods to guide you along.
Print your completed solution.
Be sure your files contain your name and the current date. Submit your solutions (4 files) in a single stapled package together with this completed document.