Вы находитесь на странице: 1из 14

1.

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

A stack is a type of simple object container


Objects are inserted and removed in the last-in
first-out (LIFO) fashion
Think of a stack as a can of tennis balls - the last ball
inserted in the can is the first one taken out
Methods:
public interface Stack {
public boolean isEmpty();
public void push(Object o);
public Object pop() throws EmptyStackException;
public void clear();
}

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

A queue is another simple type of object container


Objects are inserted in and removed from the
queue in the first-in first-out (FIFO) fashion
Think of a queue as a waiting line in the toll booth
Methods
public interface Queue
{
public boolean isEmpty();

public void add(Object o);

public Object remove() throws NoSuchElementException;

public void clear();

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

Exercise 2a: ice cream cone queues

sugar waffle plain

cones cones cones

cones in Your mission: Remove cones from the


a queue random queue and put each one into
with random order the correct cone-type queue
9
Cone Class
Public class Cone {
public static final String sugar = sugar;
public static final String plain = plain;
public static final String waffle = waffle;

String coneType;
public Cone(String coneType) {
this.conType=coneType;}
}

10

2a:Write a method and a main method

public static void sortIceCreamQueue(ArrayQueue originalQ,


ArrayQueue sugarQ, ArrayQueue waffleQ,
ArrayQueue plainQ) {
//write your code here

}
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

public static void sortIceCreamQueue(ArrayQueue originalQ,


ArrayQueue sugarQ, ArrayQueue waffleQ,
ArrayQueue plainQ) {
while (!originalQ.isEmpty()) {
Cone c = (Cone)originalQ.remove();
if (c.coneType.equals(sugar))
sugarQ.add(c);
else if (c.coneType.equals(plain))
plainQ.add(c);
else
waffleQ.add(c);
}
}

12

Exercise 2b: ice cream cone stacks

sugar waffle plain

cones cones cones

cones in Your mission: Remove from


a queue random queue and put into correct
with random order cone-type stack
13
2b:Write a method and a main method
public static void sortIceCreamQueue2(ArrayQueue originalQ,
ArrayStack sugarStack, ArrayStack waffleStack,
ArrayStack plainStack) {
//write your code here

}

public static void main(String args[]) {


ArrayQueue a = new ArrayQueue();
//adding cones to a in random order
//for instance, sugar, plain, waffle, waffle, sugar

ArrayStack s = new ArrayStack();


ArrayStack w = new ArrayStack();
ArrayStack p = new ArrayStack();
sortIceCreamQueue2(a, s, w, p);
if (a.isEmpty())
System.out.println(I have finished sorting the
original queue);
}
14

Вам также может понравиться