
1
NAME (PRINT LEGIBLY!)___________________________________________
(last, first)
This 90-minute exam has 6 questions worth a total of 100 points. Spend a few minutes looking at all
questions before beginning so that you can see what is expected. Budget your time wisely. Use the
back of the pages, if you need more space.
Question 0 (2 points). Write your netid and your name, legibly, at the top of each page.
Question 1 (15 points). Write the body of method fixZip,
specified below. The following methods of class String may be
useful:
• s.length() = the number of characters in s.
• s.charAt(k) = the character at position (or index) k of string s
• s.substring(h, k) = a string consisting of characters s[h], s[h+1], …, s[k–1]
• s.substring(h) = a string consisting of characters s[h], s[h+1], …, s[s.length()–1]
• s.indexOf(c) = index of first occurrence of character c in s (-1 if none)
• s.indexOf(s1) = index of first char of the first occurrence of String s1 in s (-1 if none)
/** s, an address, may be a Cornell address with the wrong zip number. If s contains the substring
"Cornell" and also the substring "14850", then return s but with "14850" replaced by "14853".
Otherwise, return s.
E.g. for s = "Gries; Olin 167; Cornell; Ithaca, NY 14850; USA",
return "Gries; Olin 167; Cornell; Ithaca, NY 14853; USA". */
public String fixZip(String s) {
}
CS100J Prelim I, 25 Sept. 2007 CORNELL NETID ______________________