SUNY Geneseo Department of Computer Science
{Date}
CSci 141, Spring 2005
Prof. Doug Baldwin
Second hour exam is Friday (April 1)
Returning yellow tile count?
Walk through treasure hunt
if robot is on white tile
move forward
int i = hunt() // returns # yellow
move forward
return i
Class = set of (potential) objects
Subclass = subset
Are stacks conceptually a subset of lists?
Subclass as subset vs subclass for reusing code
import geneseo.cs.sc.List;
class Stack {
private List items; // holds items on stack
public Stack() {
items = new List();
}
public void push( Object x ) {
items.addItems( x );
}
public Object pop() {
return items.getAndRemove();
}
public boolean isEmpty() {
return items.isEmpty();
}
}
Adaptor Pattern
Is every element of a list the same (Java)
// In ExtendList subclass of List
public boolean allSame() {
if ( this.isEmpty() ) {
return true;
}
else if ( this.getRest().isEmpty() ) {
return true;
}
else {
return this.getFirst().equals(this.getRest().getFirst()) && ((ExtendList)this.getRest()).allSame();
}
}
(After exam)
Trees
Read Sections 13.1 and 13.2