/* Further Recursion and Method Call Examples
Answers to Exercises: DBCBDE
Insertion Sort revisited -- How did it work?
sort() calls insert()
Cost of CatchOfFish
Totalable's getInt() method (not the total() method of the List)
is not recursive.
The total() method of the List is recursive.
Can we use total() to help us write another method such as
costOfCatch()?
Stepping through an example.
Mutation Examples
swap() vs. swapTwo()?
What do you expect for:
[Position p2 = ] p.swap()
p
Position p2 = p.swapTwo()
p
p2
int x = 1;
int y = 2;
int z = x + y;
x = z + y;
Resetable counter
class Counter {
int count = 0;
void reset() { count = 0; }
void next() { count = count + 1; }
int value() { return count; }
}
(1) Can count ever be negative?
(2) Is there a functional way to write this counter?
class Counter {
int count;
Counter(int count) {
this.count = count;
}
Counter reset() { return new Counter(0); }
Counter next() { return new Counter(this.count
+ 1); }
int value() { return count; }
}
(3) How would you use either counters?
Counter c = new Counter();
c.next()
c.next()
c.value()
c.reset()
return (boolean) directly instead of running through an extra
conditional.
*/
Personal Improvement Brain-teasers (Entirely optional, only for your
personal use)
8/11/05
1. When is it appropriate to use interfaces?
A. When we want all of a set of subclasses to use a common
set of methods
B. When we want a set of classes to share fields but not
methods
C. When we want some but not all of a set of subclasses to
use a common set of methods
D. None of the above
2. Which of the following is *not* common between interfaces and
abstract classes?
A. You can use them both as types.
B. Instances of classes that extend a certain class
or implement a certain interface can be used anywhere the
type specification names that class or interface
C. You can name methods in both.
D. You can define methods in both.
E. None of the above.
3. Which of the following is true?
A. All the methods listed in the interface must be defined
in implementing classes.
B. A single class cannot implement more than one interface.
C. Abstract classes cannot implement interfaces.
D. If abstract class A implements interface I and class B
extends A then B does not have to implement all the methods in I.
4. Given the implementation of LargerFL and Ancestor/Person from class
and that ... is correct code, which of the following code snips is
incorrect?
A. new LargerFL(..., new
LargerFL(...).append(...).append(...).append(...)).append(...)
B. this.father.withEye(c).withEye(c).withEye(c)
C. new Ancestor()
D. new EmptyFL()
E. new Person(...).withEye(c)