Objective: Use the ArrayList class
/*** * This code is complete */ public class Person { private String firstName; private String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirst() { return firstName; } public String getLast() { return lastName; } public String toString() { return firstName + " " + lastName; } }
/** * <<< This code is Not complete >>> * Creates an ArrayList of People */ import java.util.*; public class PeopleList { private ArrayList< Person > people; public PeopleList() { people = new ArrayList< Person >(); } /** <<< Complete the code >>> * Adds a new person to the array list */ public void addPerson(String first, String last) { // <<< Complete the code >>> } /** <<< Complete the code >>> * Deletes all people in the ArrayList who's * first name is NOT "Bob" */ public void BobClub() { // <<< Complete the code >>> } /** * This code Is complete */ public String toString() { String s = ""; for (int i=0; i < people.size(); i++) { s += people.get(i) + "\n"; } return s; } }
/** * <<< This Tester class is complete >>> * * Below is the output from this class: * * >>> List of names before method call to BobClub() * * Bob Newhart * Bob Cobb * Bob Square Pants * Jim Smith * Tom Jones * John Crump * Ethel McKamey * Joe Still * Bob Mudd * Bob Barker * Bob Knight * Bob Dylan * Bob Rathbone * Julie Epley * Phil Walker * Bob Herrmann * Jen Cozart * Carol Medina * Nancy Bentz * Bob Medlock * Bob Dole * * >>> List of names after method call to BobClub() * * Bob Newhart * Bob Cobb * Bob Square Pants * Bob Mudd * Bob Barker * Bob Knight * Bob Dylan * Bob Rathbone * Bob Herrmann * Bob Medlock * Bob Dole **/ import java.util.*; public class Tester { public static void main(String args[]) { PeopleList myList = new PeopleList(); myList.addPerson("Bob","Newhart"); myList.addPerson("Bob","Cobb"); myList.addPerson("Bob","Square Pants"); myList.addPerson("Jim","Smith"); myList.addPerson("Tom","Jones"); myList.addPerson("John","Crump"); myList.addPerson("Ethel","McKamey"); myList.addPerson("Joe","Still"); myList.addPerson("Bob","Mudd"); myList.addPerson("Bob","Barker"); myList.addPerson("Bob","Knight"); myList.addPerson("Bob","Dylan"); myList.addPerson("Bob","Rathbone"); myList.addPerson("Julie","Epley"); myList.addPerson("Phil","Walker"); myList.addPerson("Bob","Herrmann"); myList.addPerson("Jen","Cozart"); myList.addPerson("Carol","Medina"); myList.addPerson("Nancy","Bentz"); myList.addPerson("Bob","Medlock"); myList.addPerson("Bob","Dole"); System.out.println("\n" + myList); myList.BobClub(); System.out.println("\n" + myList); } }