AP Computer Science

Homework Set 4

Arrays and ArrayLists of Objects

P4A. Write a class definition for a MyPod object that stores information about your personal music player. Class MyPod should include the following:

a. an instance variable for its color,

b. an instance variable for its memory capacity (in GB),

c. an instance variable of type array that can hold 3 Song objects called “songLibrary” that represents all songs in the MyPod,

d. a zero-argument and multi-argument constructor to initialize all instance variables.

f. a toString() method to display the object’s instance variables in a user-friendly format.

Write a MyPodDriver to perform the following:

a. create a MyPod object carPod,

b. initialize its instance variables using its multi-argument constructor. Pick your color, memory capacity, and songs to populate your pod with (this can be hardcoded in its constructor…no JOptionPane needed here)

c. print out the information for each MyPod using the toString() method of the MyPod class. Use a for-each loop instead of the typical for loop.

P4B. Write a program that creates a “ClockStore” class that is able to hold “Clock” objects. You should reuse the Clock class from the previous chapter.

The class should meet the following requirements:

1. Write a class “ClockStore” that consists of a private array called “clocksInStock” that can hold 3 Clock objects.

2. Create an instance of class “ClockStore” and populate it with three Clock objects using the Clock’s constructor. You set the time for each Clock. Here’s a sample demonstrating how to place a Clock in the array at position “0”:

clocksInStock[0] = new Clock( 12, 30, 45 );

4. Write a processor method mostSeconds() that returns index of the Clock that has the highest total seconds as calculated by the Clock method totalSeconds(). The ClockStore method mostSeconds() should have the following header:

int mostSeconds()

5. Write a toString() method that traverses the array of Clocks using a for-each loop. This should print the time of each clock using each Clock’s toString() method.

6. Write a driver class “ClockDriver” that tests the above methods.

P4C. Write a program that creates a “Roster” class that is able to hold “Student” objects. You should reuse the Student class from the previous chapter.

The class should meet the following requirements:

1. Write a class “Roster” that consists of a private array of Students.

2. Create an instance of class “Roster” and populate it with three Student objects.

3. Write a Roster findStudentWithMaxGPA() method that traverses the array with a for loop and returns out the name of the student with the max GPA.

4. Write a Roster toString() method prints the name of the student with the maximum GPA (via calling the method findStudentWithMaxGPA().

5. Write a driver class “RosterDriver” that tests the above methods.

P4D. Write a program that creates an ArrayList of String objects that will hold the names of members of your family. After populating the ArrayList, perform the following:

a. Print the number of elements in the ArrayList using the ArrayList “.size()” method. This should be consistent with the number of names in the ArrayList.

b. Use a for loop to traverse the ArrayList and print the name of each person on a separate line.

c. Use a separate for-each loop to traverse the ArrayList and print the name of each person on a separate line.

See LewTube for demonstrations on how to create and use ArrayLists and all of its methods. The line of code below shows how to create an ArrayList of Strings:

ArrayList<String> myFamily = new ArrayList<String>()

Note that you can create an ArrayList of any type. For example, you can create an ArrayList of MySongs as follows:

ArrayList<MySong> playList = new ArrayList<MySong>()

P4E. Write a program that creates an ArrayList of String objects that will hold the names of the universities to which you are applying (include at least three schools that contain 4 letters…UC’s are easy: UCLA, UCSD, UCSB, or Yale, MITT (the baseball university, or SWIM (the aquatics university). After populating the ArrayList with at least 5 schools, perform the following:

a. Print the number of elements in the ArrayList. This should be consistent with the number of schools in the original ArrayList.

b. Use a for-each loop to traverse the ArrayList and print the name of each school on a separate line.

c. Use a for loop (not a for-each loop), to remove all the schools that have a length of 4. Make sure to test the situation where there are two consecutive schools of length “4” in your ArrayList.

d. Print the number of elements in the ArrayList after the removal. Again, this should be consistent with the number of schools remaining in the list.

e. Finally, print the names of the schools remaining in the list. Were all of the 4 letters schools properly removed?

P4F. Let’s upgrade the MyPod class. Create a MyPod2 class that uses an ArrayList to store Songs instead of an array. Provide the same functionality as in P4A.

P4G. Finally, let’s upgrade the Roster class (from P4C) to a Roster2 class that can accommodate “adding” and “dropping” Students from a roster. Write the Roster2 class that meets the following requirements:

a. Roster2 should use an ArrayList called enrolledStudents (instead of an array) to store Student objects.

b. The “brain” method addStudent should be added to the Roster2 class. It will add a given Student to the ArrayList enrolledStudents to the end of the ArrayList. The method addStudent should have the following header:

public void addStudent ( Student newStudent )

Note that the addStudent method will ultimately call the ArrayList’s .add() method since Student objects are stored in the ArrayList of enrolledStudents.

c. The “brain” method dropStudent should be added to the Roster2 class. The method dropStudent should have the following header:

public void dropStudent ( String lastName )

The method dropStudent should remove the student with the given last name from the ArrayList enrolledStudents. For example, if apComputerScience is an instance of the Roster2 class, the following line of code will remove the student “Lew” from the Roster2’s ArrayList of enrolledStudents:

apComputerScience.dropStudent( “Lew” );

d. Write a Roster2Driver that performs the following:

i. Creates an instance of the Roster2 class called “apComputerScience”,

ii. Adds three students using the “addStudent” method,

iii.  Prints “apComputerScience” using Roster2’s toString() method to verify all “added” Students have indeed been added,

iv.  Calls the “dropStudent” method to drop one of the students in the ArrayList enrolledStudents (i.e. any one of the Students in the class), and

v.  Prints “apComputerScience” again to view the revised list of Students in the “apComputerScience”.

By the end of the lesson students should be able to:

a. Write a class definition that includes instances of another class (class composition)

b. Write an object method whose output depends on the object’s instance variables.

c. Use arrays and ArrayLists in a class and be able to use either a for or for-each loop to traverse the array or ArrayList.

Page 4

AP Computer Science – HW Set 4 Programs

http://thecubscientist.com