odds and ends · 1 odds and ends wed department dinner, 5:30 monroe student research symposium, –...
TRANSCRIPT
1
Odds and Ends
● Wed department dinner, 5:30 Monroe● Student research symposium,
– Thur Sept 7, 46pm, Founders● Fri: 3:30 NBS seminar
– Professor Stan Anderson● Fri: 7:00 ReelTalk
– “Paradise Now”, Porter Theater● Sat: Math/CS Department Retreat
– Meet 2:45 in front of Gym
2
Exercise 1.4
● Prove, by mathematical induction, that the sum of powers of 2 is one less than the nexthigher power.
3
PrintInt Class
4
Abstract Data Types
Chapter 2
5
What Is a Type?
● ....?
6
What Is a Type?
● A set– Integers– Real numbers– Characters
● Operators– *,+,,/, ....
● Properties– 2,147,483,646 + 5 = 2,147,483,645
7
Class Types
● Defining class creates new type– Declaration syntax combines preexisting types– Implicitly creates set of possible instances
● Operators– Class methods define how instances may be manipulated
● Properties– ....?
8
Class Types
Class NewClass{private OldClass part1 = new OldClass();private AnotherOldC part2 = new AnotherOldC();private int x = 0;private int y = 0;
public void newOp1(){ ... do something to part1 ... }
public void newOp2(double z){ ... do something else ...}
public String moreOps() {return new String(“some string”); }
9
Interfaces
● What is the difference between a class and interface?
10
Polymorphism
● A variable declared as an interface type● A variable declared to be of a class but assigned an
object value from a subclass of the declared class
11
Type Signatures
Interface StringBox{public void insert(int v); // place one intpublic void insert(int v1, int v2); // or twopublic void insert(double d); // or a doublepublic void insert(String s); // or a stringpublic String extract ();
}
12
Abstract Data Types
● Hierarchy of abstract data structures
13
More Interfaces
Public interface Bag extends Collection {public void addElement(Object newElement);public boolean containsElement(Object testE);public Object findElement(Object testE);public void removeElement(Object oldE);
}
public interface Collection extends Serializable{public Enumeration elements();public boolean isEmpty();public int size();
}
14
Properties of ADT
● Given bag interface, what properties to expect?
15
Properties of ADT
● Given bag interface, what properties to expect?● After addElement(newElement):
– containsElement(newElement) should be true– isEmpty() should be false– size() should be one greater than before– findElement(newElement) should return object– elements() should return enumeration containing
newElement● These properties should be written into postconditions
(and associated preconditions)
16
The Fundamental ADT's
● Collection● Bag● Set● Sorted, Comparator, & Comparable● Stack, Queue, Deque● FindMin & FindNth● Indexed Collections & Sorting Algorithms● Map● Matrix
17
Collection
public interface Collection extends Serializable{public Enumeration elements();public boolean isEmpty();public int size();
}
18
Bag
public interface Bag extends Collection{
public void addElements(Object newElement);public boolean contains(Object testElement);public Object findElement(Object testElmnt);public void removeElement(Object oldElement);
}
19
Set
public interface Set extends Collection {// element operationspublic void addElement(Object newElement);public boolean containsElement(Object testElem);public Object findElement(Object testElem);public void removeElement(Object oldElement);// setset operationspublic void unionWith(Set aSet);public void intersectWith(Set aSet);public void differenceWith(Set aSet);public boolean subsetOf(Set aSet);
}
20
Sorted, Comparator, & Comparable
public interface Sorted extends Collection {
// no new methods}
public interface Comparator extends Serializable{
int compare(Object left, Object right);// returns 1 if l<r, 0 if ==, 1 l>r
}
public interface Comparable extends Serializable {public int compareTo(Object value);
}
21
Stack, Queue, Deque
public interface Stack extends Collection {public void addLast(Object newElement);public Object getLast();public void removeLast();
}public interface Queue extends Collection {
public void addLast(Object newElement);public Object getFirst();public void removeFirst();
}public interface Deque extends Stack, Queue {
public void addFirst(Object newElement);}
22
FindMin & FindNth
public interface FindMin extends Collection {public void addElement(Object newElement);public Object getFirst();public removeFirst();
}
public interface FindNth extends Collection {public Object findNth(int index);
}
23
Indexed Collections & Sorting Algorithms
public interface Indexed extends Collection {public void setSize(int size);public Object elementAt(int index);public void setElementAt(Object val, int index);public void addElementAt(Object val, int index);public void removeElementAt(int index);// elements() provides items in indexed order
}
public interface SortAlgorithm {public void sort(Indexed data)
24
Map
public interface Map extends Collection {public boolean containsKey(Object key);public Object get(Object key);public void removeKey(Object key);public void set(Object key, Object
newValue);}
25
Matrix
public interface Matrix extends Serializable {public void setSize(int rows, int columns);public int numberRows();public int numberColumns();public Object elementAt(int row, int column);public void setElementAt(Object v, int r, int c);
}
26