Professional Documents
Culture Documents
00 Tutorial #8
Agenda
Quiz 2
Matrix
Queue
What is a queue
Queue methods
Stack
What is a stack
Stack methods
Quiz 2
Quiz 2: 1.5 hours
Quiz Review: 1.5 hours
Exercise 1: Matrix
3x1+2x2=13
5x1+3x2=21
Find the answer for x1, x2 and print out the result;
You can construct a 2x2 matrix A
3 2
5 3
and a 2x1matrix (vector) b
13
21
so that Ax=b, resolve x1, x2;
You can download needed classes (Lecture 22, Matrix.java,
GElim.java) from the web, but you should write your own
main class/method.
4
What is a Stack
Stack Methods
void push(Object item)
Pushes an item into the stack
Note that the item can be any object type (String, user-defined
class, etc.)
Object pop()
Pops the top item of the stack and returns it to the caller
Note that what returned is a generic Object
How do we convert it into the original data type?
void clear()
Empty the stack
boolean isEmpty()
Return true if the stack is empty and false if otherwise
What is a Queue
Queue Methods
void add(Object item)
Insert an item into the queue
Note that the item can be any object type (String, user-defined
class, etc.)
Object remove()
Remove the first item from the queue and return it to the caller
If the queue is empty, throw NoSuchElementException
Note that what is returned is a generic Object
How do we convert it into the original data type?
void clear()
Empty the queue
boolean isEmpty()
Return true if the queue is empty and false if otherwise
String coneType;
public Cone(String coneType) {
this.conType=coneType;}
}
10
}
public static void main(String args[]) {
ArrayQueue a = new ArrayQueue();
//adding cones to a in random order
//for instance, sugar, plain, waffle, waffle, sugar
a.add(new Cone(sugar)); //.
ArrayQueue s = new ArrayQueue();
ArrayQueue w = new ArrayQueue();
ArrayQueue p = new ArrayQueue();
sortIceCreamQueue(a, s, w, p);
if (a.isEmpty())
System.out.println(I have finished sorting
the original queue);
}
11
12