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.

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

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

Saved successfully!

Ooh no, something went wrong!