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.

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

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

Saved successfully!

Ooh no, something went wrong!