Final Exam Review
Tom Kelliher, CS 116
Dec. 9, 2002
Lab and postlab 7 due today.
Extra credit applet due Wednesday.
Final Friday in HS 153 3:00 to 5:00.
This example pulls together a lot of what we've done this semester.
- A stack is a fundamental data structure in CS.
- Last in, first out (LIFO) principle: like a stack of trays in a
cafeteria line.
- Stack parameter: number of items stack can hold.
- Stack state: data within the stack (array), top of stack.
- Stack operations: empty, full, push, pop.
Integer stack implementation:
import java.applet.*;
public class Driver extends Applet
{
Stack s;
public void init()
{
int tempI;
boolean tempB;
s = new Stack(10);
tempB = s.empty(); // Returns true.
s.push(12);
tempB = s.empty(); // Returns false.
s.push(9);
tempI = s.pop(); // Returns 9.
tempI = s.pop(); // Returns 12.
tempB = s.empty(); // Returns true.
}
}
class Stack
{
int topIndex;
int data[];
public Stack(int length)
{
topIndex = -1;
data = new int[length];
}
boolean empty()
{
if (topIndex == -1)
return true;
else
return false;
}
boolean full()
{
if (topIndex == data.length - 1)
return true;
else
return false;
}
int pop()
{
if (empty())
{
// Throw an exception and return.
return 0;
}
else
{
topIndex--;
return data[topIndex + 1];
}
}
void push(int newTop)
{
if (full())
{
// Throw an exception and return.
}
else
{
topIndex++;
data[topIndex] = newTop;
}
}
}
- Iterative execution: for, while, and do/while loops.
When to use?
Index variable.
- Conditional execution: if/else if/else, switch, break.
Purpose of the break within a switch case.
- Conditions --- keys to the above. Precedence, associativity,
operators.
Remember the precedence table.
- Arithmetic: Operators and their types. Type conversion: implicit and
explicit casts.
- Parameter passing: Formal and actual parameters.
- Pass by reference vs. pass by value.
- Local variables.
- Return values and the type of a method.
- Special applet methods: init, paint, interface methods for listeners.
- Special class methods: The constructor.
- Primitive types: int, double, boolean, char, etc.
- Arrays: Type, number of elements, declaring, indexing, .length.
- Objects: String, Label, TextField, etc.
Creating your own.
- Declaring and instantiating. The actual variable or a reference?
- Variable/Object scope. Naming clashes:
double average(int data[])
{
double average; // dohhh....
- What is the model?
- Which object, which action?
- extends, inheritance, sub- and super-classes. Overrides.
- Public vs. private. Why use?
- The use of instance variables
The use of the constructor.
- Types of events.
- Listener classes must implement all interface methods. Interface
methods must have code to deal with each event from each object.
- Objects must register their events and designate handler objects.
Thomas P. Kelliher
Mon Dec 9 08:44:55 EST 2002
Tom Kelliher