You are on page 1of 5
Practice Free Response Question ~ Array Lists ‘AP Computer Science Consider a system for processing names and addresses from a mailing list. A Recipients class will be used as part of, this system. The line in the mailing list are stored in an ArrayList, a private instance variable in the Recipients class. The blank line that separates recipients in the mailing lst is stored as the empty string in this list, and the final elemnt in the list is an empty string. A portion of the mailing lst is shown below, with the corresponding part of the ArrayList. Mr. J. Adams 6 Rose St. Ithaca, NY 14850 Jack S. Smith 12 Posy Way Suite 201 Glendale, cA 91203 Ms. M.K. Delgado 2 River Dr. New York, NY 10013 oO 1 2 3 4 5 “tir, J, Adams” [6 Rose St.” | “ithaca, NY 14850 [*” [Jack 5. Smith | “12 Posy Way” 6 2 @ 9 10 ‘Waite 201” | “Glendale, Ch 51023 [*" | Ws, WK. Delgado” | “2 River De.” 1 12 “New York, N¥ 10013 [*” The Recipients class that process this data is shown below: public class Recipients t private ArraykList lines; / Constructor. Fill lines with mailing list data Postcondition: Each element in lines is one line of the mailing list Lines appear in the list in the same order that they appear in the mailing list. Blank line separators in the mailing list are stored as empty strings public Recipients () // implementation not shown , yee * Postcondition: Returns the city contained in the cityZip string of an * address. * parameter cityZip contains the city, state, and zipcode line * of an address * return the city substring contained in cityZip “sf public String extractCity (String cityZip) { // to be implemented in part (a) } pee * Precondition: The recipient name is the first line of each label s on the mailing list Postcondition: Prints a list of recipient names to console, one per Line public void printNames() { yee “yf // to be implemented in part (b) — } Postcondition: Returns the address of the recipient with the specified name. parameter name is a name in the lines ArrayList return the city substring contained in cityZip public String getAddress (String name) { // to be implemented in part (c) , (a) Wr by zip rite the ext ractCi ty method of the Recipients class. In the cityZip parameter, the city is followed a comma, then one blank space, then two capital letters for a state abbreviation, then a space and a 5-digit code, For example, if cityZipis "Ithaca, NY 14850”, the method call extractCity (cityZip) should return “Ithaca”. jee +f Po: ondition: Returns the city contained in the cityZip string of an address. parameter cityZip contains the city, state, and zipcode line of an address return the city substring contained in cityZip public String extractCity (String cityZip) { Alt. solution int stop = cityZip. index return cityZip.substring(0, stop); ys just one line for whole method turn cityZip.substring(0, cityZip.indexo£(*,”))7 (b) Write the printNames method of the Recipients class. Method printNames prints the names of all nts to the console, one per line. For the sample part of the mailing list shown at the beginning of the question, the output for printivames would be: Mr. J. Adams Jack $. Smith Ms. M.K. Delgado yes * Precondition: The recipient name is the first line of each label * on the mailing list * Posteondition: Prints a list of recipient names to console, one per * Line “sf public void printNames () { System.out printin(lines.get(0)); // 0" spot always a name // since lines always ends with an empty string, “", we only // only loop te size - 1 to avoid going out of bounds for (int i= 1; i < lines.size() - 1; i++) { if (lines.get (i)-equals("”)) // clue that next entry is name System.out.printin(lines.get(i + 1)); // use i+] to print the next entry in lines (c) Write the getAddress method of the Recipients class. This method should return a string that contains only the address of the corresponding name parameter. For example, if name is “Jack S. Smith’, a string containing the three subsequent lines of his address should be returned. This string should contain line breaks in the appropriate places, including after the last line of the address. This ensures that the address will have the proper address format when printed by a client class. fe Postcondition: Returns the address of the recipient with the * specified name. * parameter name is a name in the lines ArrayList * return the city substring contained in cityZip s public String getAddress (String name) { // plan ~ find the name in Array List lines // starting at the index after that, we want to copy down // all the entries up to and including the blank string int index = 0; for (int i= 0; i < list.size(); itt) { if (name.equals(List.get (i)) { index = i; break; // we leave the for loop w/o continuing } ) indext+; // index is the location in lines of name; we want // to return the lines starting with the spot // after index // blank string to build on; we are NOT // printing anything out in this method while (Lines.get (index) .equals("") == false) { // while loop parameter checks that the index is not ‘” ans += lines.get (index) + “\n"; // /n means line break ) return ans;

You might also like