Page 1367 - AP Computer Science A, 7th edition
P. 1367
2. (a) public String replaceAll(String line, String sub, String repl)
{
int pos = line.indexOf(sub); while (pos >= 0)
{
line = line.substring(0, pos) + repl + line.substring(pos + sub.length());
pos = line.indexOf(sub); }
return line; }
(b) public void createPersonalizedLetters() {
for (int i = 0; i < customers.size(); i++) {
List<String> tempLines = makeCopy(); Customer c = customers.get(i);
for (int j = 0; j < tempLines.size(); j++) {
tempLines.set(j, replaceAll(tempLines.get(j), “@”, c.getName()));
tempLines.set(j, replaceAll(tempLines.get(j), “&”, c.getCity()));
tempLines.set(j, replaceAll(tempLines.get(j), “$”, c.getState()));
}
writeLetter(tempLines); }
}
NOTE
• In part (a), each time you encounter sub in line, you simply