CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday...
-
Upload
madlyn-felicia-mccarthy -
Category
Documents
-
view
212 -
download
0
Transcript of CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday...
CS 2430
Day 24
Announcements
• Quiz this Friday
• Program 5 posted on Monday
• Program 4 due date: Friday at 10pm
• Program 4 grace date: Wednesday at 10pm (don’t worry about what the WWW says)
Agenda
• Finish Queue examples
• Generics
• A generic Queue
The Queue class
public class Queue
{
private static final int DEFAULT_SIZE = 10;
private Object[] items;
private int count, front, rear;
public Queue() { . . . }
public Queue(int inSize) { . . . }
public void enqueue(Object obj) { . . . }
public Object dequeue() { . . . }
public boolean isFull() { . . . }
public boolean isEmpty() { . . . }
public int size() { . . . }
public void clear() { . . . }
}
Implementation of Queue methods…
…you do it as part of Program 5
Example
How many items are currently stored in the Queue?
As implementerpublic int size()
{
return count;
}
As userpublic static int size(Queue inQueue, int qSize)
{
int num = 0;
while (!inQueue.isEmpty())
{
inQueue.enqueue(inQueue.dequeue());
num++;
}
return num;
}
What’s wrong?
Infinite loop!
As userpublic static int size(Queue inQueue, int qSize)
{
int num = 0;
Queue tq = new Queue(qSize);
while (!inQueue.isEmpty())
{
tq.enqueue(inQueue.dequeue());
num++;
}
while (!tq.isEmpty())
inQueue.enqueue(tq.dequeue());
return num;
}
All good!
Example
How to reverse a given Queue?
Use a Stack!
Another Example
How do you swap the back two elements of a Queue?
A B C D A B D C
Any questions?
Generic types
How to make a generic container class in Java?
First try
• Make a different class to hold each type of Object
• We would have BagOfDate, BagOfString, etc.
• Problem?
Too much similarity!
• Why write all that code?
Second try
• Make containers backed by array of Object• Can hold any type of Object• Problem?
Casting required to “get” items
• Example:
Rational rat = (Rational)operands.pop();
• Awkward: Container could hold Rationals, Strings, Dates, etc.
Java generics
• Can make a generic container class that is instantiated with a particular type (of Object)
• The “type” is a parameter to the class
• The “type” parameter MUST be a class, NOT a primitive type
public class Queue
{
private static final int DEFAULT_MAX = 10;
private Object[] items;
private int front, rear, count;
public Queue() { . . . }
public Queue(int inSize) { . . . }
public boolean isEmpty() { . . . }
public boolean isFull() { . . . }
public void enqueue(Object obj) { . . . }
public Object dequeue() { . . . }
}
Not generic!
The $5 Queue
public class Queue<E> // E must be a class, not a primitive
{
private static final int DEFAULT_MAX = 10;
private E[] items;
private int front, rear, count;
public Queue() { . . . }
public Queue(int inSize) { . . . }
public boolean isEmpty() { . . . }
public boolean isFull() { . . . }
public void enqueue(E obj) { . . . }
public E dequeue() { . . . }
}
Generic Queue
public class Queue<E> // E must be a class, not a primitive
{
private static final int DEFAULT_MAX = 10;
private E[] items;
private int front, rear, count;
public Queue() { . . . }
public Queue(int inSize) { . . . }
public boolean isEmpty() { . . . }
public boolean isFull() { . . . }
public void enqueue(E obj) { . . . }
public E dequeue() { . . . }
}
Same as before, but with Object replaced by E
Generic Queue
Implementation
Constructorpublic Queue()
{
items = ???
}
Does this work?public Queue()
{
items = new E[DEFAULT_MAX];
}
Java doesn’t allow this!
We need to create an array of Object and cast it to (E[]).
Constructor fixed!public Queue()
{
items = (E[]) new Object[DEFAULT_MAX];
}
We might get a “warning” from the compiler.
We’ll just live with it.
Constructorspublic Queue()
{
items = (E[]) new Object[DEFAULT_MAX];
}
public Queue(int inSize)
{
if (inSize < 0)
items = (E[]) new Object[DEFAULT_MAX];
else
items = (E[]) new Object[inSize];
}
The rest is the same, but just replace Object with E
Client codeQueue<Date> dq = new Queue<Date>(365);
Queue<Rational> rq = new Queue<Rational>(40);
Date date = dq.dequeue(); // no cast
Rational rat = rq.dequeue();
rq.enqueue(new Rational(1, 2)); // same as before
Can also do thisQueue<Date> dq = new Queue(365);
Queue<Rational> rq = new Queue(40);
Date date = dq.dequeue(); // no cast
Rational rat = rq.dequeue();
rq.enqueue(new Rational(1, 2)); // same as before
Can’t do this!Queue<Date> dq = new Queue(365);
Queue<Rational> rq = new Queue(40);
Date date = dq.dequeue();
Rational rat = rq.dequeue();
rq.enqueue(new Rational(1, 2));
dq.enqueue(rat); // this is a syntax error!
Or this!Queue<Date> dq = new Queue(365);
Queue<Rational> rq = new Queue(40);
Date date = dq.dequeue();
Rational rat = rq.dequeue();
rq.enqueue(new Rational(1, 2));
rat = dq.dequeue(); // this is a syntax error!
Next: multiple inheritance