Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CS 2430 Day 24

2 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)

3 Agenda Finish Queue examples Generics A generic Queue

4 Go to THE THING: https://xray.ion.uwplatt.edu/summerss

5 The Queue class

6 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() {... } }

7 Implementation of Queue methods…

8 …you do it as part of Program 5

9 Example How many items are currently stored in the Queue ?

10 As implementer public int size() { return count; }

11 As user public 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!

12 As user public 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!

13 Example How do you delete all occurrences of a specified item from a Queue and maintain relative order?

14 As user public static void removeAll(Queue q, Object x, int qSize) { Queue tq = new Queue(qSize); while (!q.isEmpty()) { Object o = q.poll(); if (!o.equals(x)) tq.offer(o); } q = tq; } This does not work!

15 Java: no such thing as “pass by reference”

16 main() public static void main(String args[]) { Queue q = new Queue(5); Object x;... removeAll(q, x, 5); } public static void removeAll(Queue q, Object x, int qSize) { Queue tq = new Queue(qSize); while (!q.isEmpty()) { Object o = q.poll(); if (!o.equals(x)) tq.offer(o); } q = tq; } q removeAll() q tq x ► ► ► ► ► ► ► ► ► ► uoiea x e uoia

17 UOIE public static void removeAll(Queue q, Object x, int qSize) { Queue tq = new Queue(qSize); while (!q.isEmpty()) { Object o = q.poll(); if (!o.equals(x)) tq.offer(o); } while (!tq.isEmpty()) q.offer(tq.poll()); } A How about this? qtq EIOUA E ► ► ► ► ► ► AAIIOOUU x

18 UOIAUOIE public static void removeAll(Queue q, Object x, int qSize) { Queue tq = new Queue(qSize); while (!q.isEmpty()) { Object o = q.poll(); if (!o.equals(x)) tq.offer(o); } while (!tq.isEmpty()) q.offer(tq.poll()); } A How about this? qtq ► ► ► AIOU AA A II I OO O UU U

19 Example How to reverse a given Queue ? Use a Stack !

20 Another Example How do you swap the back two elements of a Queue ? A B C D  A B D C

21 Any questions?

22 Go to THE THING: https://xray.ion.uwplatt.edu/summerss

23 Generic types

24 How to make a generic container class in Java?

25 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?

26 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 Rational s, String s, Date s, etc.

27 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

28 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

29 public class Queue // 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

30 public class Queue // 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

31 Implementation

32 Constructor public Queue() { items = ??? }

33 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[] ).

34 Constructor fixed! public Queue() { items = (E[]) new Object[DEFAULT_MAX]; } We might get a “warning” from the compiler. We’ll just live with it.

35 Constructors public 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]; }

36 The rest is the same, but just replace Object with E

37 Client code Queue dq = new Queue (365); Queue rq = new Queue (40); Date date = dq.dequeue(); // no cast Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); // same as before

38 Can also do this Queue dq = new Queue(365); Queue rq = new Queue(40); Date date = dq.dequeue(); // no cast Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); // same as before

39 Can’t do this! Queue dq = new Queue(365); Queue 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!

40 Or this! Queue dq = new Queue(365); Queue 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!

41 Next: multiple inheritance


Download ppt "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."

Similar presentations


Ads by Google