07.04.2015 Views

AP Computer Science Homework Set 4 Arrays and ArrayLists of ...

AP Computer Science Homework Set 4 Arrays and ArrayLists of ...

AP Computer Science Homework Set 4 Arrays and ArrayLists of ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong><br />

<strong>Homework</strong> <strong>Set</strong> 4<br />

<strong>Arrays</strong> <strong>and</strong> <strong>ArrayLists</strong> <strong>of</strong> Objects<br />

P4A. Write a class definition for a MyPod object that stores information about your<br />

personal music player. Class MyPod should include the following:<br />

a. an instance variable for its color,<br />

b. an instance variable for its memory capacity (in GB),<br />

c. an instance variable <strong>of</strong> type array that can hold 3 Song objects called<br />

“songLibrary” that represents all songs in the MyPod,<br />

d. a zero-argument <strong>and</strong> multi-argument constructor to initialize all instance<br />

variables.<br />

f. a toString() method to display the object’s instance variables in a userfriendly<br />

format.<br />

Write a MyPodDriver to perform the following:<br />

a. create a MyPod object carPod,<br />

b. initialize its instance variables using its multi-argument constructor. Pick your<br />

color, memory capacity, <strong>and</strong> songs to populate your pod with (this can be<br />

hardcoded in its constructor…no JOptionPane needed here)<br />

c. print out the information for each MyPod using the toString() method <strong>of</strong><br />

the MyPod class. Use a for-each loop instead <strong>of</strong> the typical for loop.<br />

Page 1<br />

<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> 4 Programs<br />

http://thecubscientist.com


P4B. Write a program that creates a “ClockStore” class that is able to hold<br />

“Clock” objects. You should reuse the Clock class from the previous chapter.<br />

The class should meet the following requirements:<br />

1. Write a class “ClockStore” that consists <strong>of</strong> a private array called<br />

“clocksInStock” that can hold 3 Clock objects.<br />

2. Create an instance <strong>of</strong> class “ClockStore” <strong>and</strong> populate it with three Clock<br />

objects using the Clock’s constructor. You set the time for each Clock.<br />

Here’s a sample demonstrating how to place a Clock in the array at position<br />

“0”:<br />

clocksInStock[0] = new Clock( 12, 30, 45 );<br />

4. Write a processor method mostSeconds() that returns index <strong>of</strong> the Clock<br />

that has the highest total seconds as calculated by the Clock method<br />

totalSeconds(). The ClockStore method mostSeconds() should<br />

have the following header:<br />

int mostSeconds()<br />

5. Write a toString() method that traverses the array <strong>of</strong> Clocks using a foreach<br />

loop. This should print the time <strong>of</strong> each clock using each Clock’s<br />

toString() method.<br />

6. Write a driver class “ClockDriver” that tests the above methods.<br />

P4C. Write a program that creates a “Roster” class that is able to hold<br />

“Student” objects. You should reuse the Student class from the previous<br />

chapter.<br />

The class should meet the following requirements:<br />

1. Write a class “Roster” that consists <strong>of</strong> a private array <strong>of</strong> Students.<br />

2. Create an instance <strong>of</strong> class “Roster” <strong>and</strong> populate it with three Student<br />

objects.<br />

3. Write a Roster findStudentWithMaxGPA() method that traverses the<br />

array with a for loop <strong>and</strong> returns out the name <strong>of</strong> the student with the max<br />

GPA.<br />

4. Write a Roster toString() method prints the name <strong>of</strong> the student with the<br />

maximum GPA (via calling the method findStudentWithMaxGPA().<br />

5. Write a driver class “RosterDriver” that tests the above methods.<br />

Page 2<br />

<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> 4 Programs<br />

http://thecubscientist.com


P4D. Write a program that creates an ArrayList <strong>of</strong> String objects that will hold<br />

the names <strong>of</strong> members <strong>of</strong> your family. After populating the ArrayList, perform<br />

the following:<br />

a. Print the number <strong>of</strong> elements in the ArrayList using the ArrayList<br />

“.size()” method. This should be consistent with the number <strong>of</strong> names in<br />

the ArrayList.<br />

b. Use a for loop to traverse the ArrayList <strong>and</strong> print the name <strong>of</strong> each<br />

person on a separate line.<br />

c. Use a separate for-each loop to traverse the ArrayList <strong>and</strong> print the<br />

name <strong>of</strong> each person on a separate line.<br />

See LewTube for demonstrations on how to create <strong>and</strong> use <strong>ArrayLists</strong> <strong>and</strong> all <strong>of</strong><br />

its methods. The line <strong>of</strong> code below shows how to create an ArrayList <strong>of</strong><br />

Strings:<br />

ArrayList myFamily = new ArrayList()<br />

Note that you can create an ArrayList <strong>of</strong> any type. For example, you can create an<br />

ArrayList <strong>of</strong> MySongs as follows:<br />

ArrayList playList = new ArrayList()<br />

P4E. Write a program that creates an ArrayList <strong>of</strong> String objects that will hold<br />

the names <strong>of</strong> the universities to which you are applying (include at least three schools<br />

that contain 4 letters…UC’s are easy: UCLA, UCSD, UCSB, or Yale, MITT (the<br />

baseball university, or SWIM (the aquatics university). After populating the<br />

ArrayList with at least 5 schools, perform the following:<br />

a. Print the number <strong>of</strong> elements in the ArrayList. This should be consistent<br />

with the number <strong>of</strong> schools in the original ArrayList.<br />

b. Use a for-each loop to traverse the ArrayList <strong>and</strong> print the name <strong>of</strong> each<br />

school on a separate line.<br />

c. Use a for loop (not a for-each loop), to remove all the schools that have a<br />

length <strong>of</strong> 4. Make sure to test the situation where there are two consecutive<br />

schools <strong>of</strong> length “4” in your ArrayList.<br />

d. Print the number <strong>of</strong> elements in the ArrayList after the removal. Again, this<br />

should be consistent with the number <strong>of</strong> schools remaining in the list.<br />

e. Finally, print the names <strong>of</strong> the schools remaining in the list. Were all <strong>of</strong> the 4<br />

letters schools properly removed?<br />

Page 3<br />

<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> 4 Programs<br />

http://thecubscientist.com


P4F. Let’s upgrade the MyPod class. Create a MyPod2 class that uses an<br />

ArrayList to store Songs instead <strong>of</strong> an array. Provide the same functionality as<br />

in P4A.<br />

P4G. Finally, let’s upgrade the Roster class so that it can accommodate “adding”<br />

<strong>and</strong> “dropping” Students from a roster. Write a Roster2 class called<br />

“ap<strong>Computer</strong><strong>Science</strong>” that meets the following requirements:<br />

a. Roster2 should use an ArrayList called myStudents (instead <strong>of</strong> an<br />

array) to store Student objects.<br />

b. A processor method called “addStudent” should be included that will add a<br />

given Student to the ArrayList myStudents. The method<br />

addStudent should have the following header:<br />

public void addStudent ( Student newStudent )<br />

Note that the addStudent method will ultimately call the ArrayList’s<br />

.add() method since Student objects are stored in the ArrayList <strong>of</strong><br />

myStudents.<br />

c. A processor method in the Roster class called “dropStudent” with the<br />

following header:<br />

public void dropStudent ( String lastName )<br />

The method dropStudent should remove the student with the given last<br />

name. For example, the following line <strong>of</strong> code will remove the student “Lew”<br />

from the ArrayList myStudents:<br />

period1.dropStudent( “Lew” );<br />

d. Write a Roster2Driver that performs the following:<br />

i. Creates a Roster2 object called “ap<strong>Computer</strong><strong>Science</strong>”,<br />

ii. Adds three students using the “addStudent” method,<br />

iii. Prints “period1” using Roster2’s toString() method to verify<br />

all “added” Students have indeed been added,<br />

iv. Calls the “dropStudent” method to drop one <strong>of</strong> the students in the<br />

ArrayList myStudents (i.e. any one <strong>of</strong> the Students in the<br />

class), <strong>and</strong><br />

v. Prints “ap<strong>Computer</strong><strong>Science</strong>” again to view the revised list <strong>of</strong><br />

Students in the “ap<strong>Computer</strong><strong>Science</strong>”.<br />

Page 4<br />

<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> 4 Programs<br />

http://thecubscientist.com


By the end <strong>of</strong> the lesson students should be able to:<br />

a. Write a class definition that includes instances <strong>of</strong> another class (class<br />

composition)<br />

b. Write an object method whose output depends on the object’s instance<br />

variables.<br />

c. Use arrays <strong>and</strong> <strong>ArrayLists</strong> in a class <strong>and</strong> be able to use either a for or<br />

for-each loop to traverse the array or ArrayList.<br />

Page 5<br />

<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> 4 Programs<br />

http://thecubscientist.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!