Solutions to other chapters:
Java Methods A & AB:
Object-Oriented Programming and Data Structures
Answers and Solutions to Exercises
Chapter 19
2. |
|
public void append(List list1, List list2)
{
for (int i = 0; i < list2.size(); i++)
list1.add(list2.get(i));
}
|
6. |
|
public double sum2(List list)
{
double sum = 0;
ListIterator iter1 = list.listIterator();
while (iter1.hasNext())
{
double a = iter1.next().doubleValue();
ListIterator iter2 = list.listIterator(iter1.nextIndex());
while (iter2.hasNext())
{
sum += a * iter2.next().doubleValue();
}
}
return sum;
}
|
8. |
|
Three-Two
Three-Two-One
Three-Two-One |
10. |
(b) |
The following simplification uses Point 's
copy constructor:
Point cursor;
Stack stk = new Stack();
...
// Save cursor position:
stk.push(new Point(cursor));
show(new LoginWindow());
...
// Restore cursor position:
cursor = stk.pop();
Recall that a stack holds references to objects.
It is necessary to make and push a copy
of cursor because subsequent code may change
the original. |
|