a precondition must be true before a method is called
the code in the method may (and usually will) assume that the
precondition is true
the caller of a method must guarantee (be sure) that the
precondition holds
a postcondition will be true after a method is called
the code in the method must guarantee that the postcondition
is true
preconditions and postconditions are a little bit like a
contract or any other agreement: if the caller provides the
preconditions, the method will provide the postconditions
preconditions and postconditions are documented in Javadoc
invariants are postconditions of every method, including the
constructors
invariants are preconditions of every method except the constructors
invariants are usually documented for the entire class rather than
for each method
Stacks
stacks of dishes or trays in a cafeteria
on a spring-loaded mechanism so only the top one is accessible
adding more dishes pushes down the stack, so only the new top
is still accessible
Last In First Out discipline (LIFO -- FIFO will be discussed at
a later lecture)
Stacks in computer science
a data structure to hold a variable number of elements
of which only the top one is accessible at any given time
the stack may be empty if it holds no elements
the stack keeps track of the number of elements it has,
as well as the individual elements
some stack implementations set a fixed maximum size for
the stack, so the stack overflows if more data is
added to a full stack
Stacks vs. arrays
an array has a fixed number of elements
an array does not keep track of which elements have been
initialized
any element of an array is equally easy to access
Stacks vs. linked lists
a linked list can access any element of the list
even though accessing some (esp. the head) is faster than
accessing others
in-class exercise (everyone together): what is the runtime (big O) of
empty(), push(), and pop()?
Other stack implementations, using a java Vector or List
any extensible data structure that has access at one end can be
used to implement a stack
this includes java Vectors and java Lists
new data is added to or removed from the end of the Vector or
ArrayList in O(1) time (when the array doesn't have to grow)
in the code in the book (p. 164/272), note that the data is
stored in an object of type List<E>, that is created
as an ArrayList<E>: this is an example of polymorphism,
and makes it easy to switch to a different kind of list
new data is added to or removed from the front of the Linked List,
in O(1) time
in the code on p. 162/270, instead of having an instance variable
of type Vector, the stack class itself is a subclass of vector
-- this kind of stack is also a vector!
Stack Applications: Palindromes
a palindrome is a string that is the same when read backwards
or forwards: "radar", "level", "racecar"
there are many algorithms for recognizing palindromes, and most
are equivalent
one such algorithm uses a stack:
the characters of the string are pushed onto the stack, one by one
as they are removed from the stack in LIFO order, they are removed
in reverse order
they can be compared to the characters in the string
if the characters from the stack match the characters from the string,
the string is a palindrome
Stack Applications: Matching Parentheses
balanced parenteses: "(a (b c) [d (e)] f g)"
unbalanced parenteses: "(a (b c {d (e)) f g]"
algorithm to check for balanced parentheses:
when encountering an open parenthesis, put it on the stack
when encountering a closed parenthesis, remove the matching
one from the top of the stack, or declare an error (if the top
of stack does not match, or if the stack was empty)
at the end of the string, should have an empty stack
if the stack is not empty at the end of the string, the parentheses
are not balanced