COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue - - PowerPoint PPT Presentation

comp 213
SMART_READER_LITE
LIVE PREVIEW

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue - - PowerPoint PPT Presentation

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.) Recall: The Queue ADT A data structure in which elements enter at one end and are removed from the opposite end is called a queue . 2 main operations:


slide-1
SLIDE 1

COMP 213

Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

slide-2
SLIDE 2

Recall: The Queue ADT

  • A data structure in which elements enter at one end and are removed from

the opposite end is called a queue.

  • 2 main operations:
  • enqueue
  • dequeue
slide-3
SLIDE 3

Recall: Formal Specification

  • Our queues are generic – the type of object held by a particular queue is indicated by the

client.

  • We provide observer operations isEmpty and isFull.
  • We create a QueueInterface that defines the signatures of the queue methods that do

not depend on the boundedness of the queue.

  • We create a BoundedQueueInterface and an UnboundedQueueInterface, which

extend QueueInterface.

slide-4
SLIDE 4

Array-Based Implementation for unbounded queue

  • Class ArrayUnbQueue that implements the

UnboundedQueueInterface.

  • The trick here is to create a new larger array, when needed, and copy the

structure into the new array.

  • No need for isFull method.
slide-5
SLIDE 5

Array-Based Implementation for unbounded queue

  • Class ArrayUnbQueue that implements the

UnboundedQueueInterface.

  • The trick here is to create a new larger array, when needed, and copy the

structure into the new array.

  • No need for isFull method.
slide-6
SLIDE 6

Array-Based Implementation for unbounded queue

  • Class ArrayUnbQueue that implements the

UnboundedQueueInterface.

  • The trick here is to create a new larger array, when needed, and copy the

structure into the new array.

  • No need for isFull method.
slide-7
SLIDE 7

Unbounded vs. bounded implementation: what changes?

  • enqueue method – to increase the capacity of the array if it has run out of

space.

  • we implement it as a separate method named enlarge.
  • Then, the enqueue method can start with:

if (numElements == queue.length) enlarge();

slide-8
SLIDE 8

Unbounded vs. bounded implementation: what changes?

  • enqueue method – to increase the capacity of the array if it has run out of

space.

  • we implement it as a separate method named enlarge.
  • Then, the enqueue method can start with:

if (numElements == queue.length) enlarge();

slide-9
SLIDE 9

Unbounded vs. bounded implementation: what changes?

  • enqueue method – to increase the capacity of the array if it has run out of

space.

  • we implement it as a separate method named enlarge.
  • Then, the enqueue method can start with:

if (numElements == queue.length) enlarge();

slide-10
SLIDE 10

Options for the enlarge method

  • We could set a constant increment value or multiplying factor within the

class.

  • We could allow the application to specify an increment value or multiplying

factor when the queue is instantiated.

  • We could use the original capacity as the increment value.
slide-11
SLIDE 11

Options for the enlarge method

  • We could set a constant increment value or multiplying factor within the

class.

  • We could allow the application to specify an increment value or multiplying

factor when the queue is instantiated.

  • We could use the original capacity as the increment value.
slide-12
SLIDE 12

Options for the enlarge method

  • We could set a constant increment value or multiplying factor within the

class.

  • We could allow the application to specify an increment value or multiplying

factor when the queue is instantiated.

  • We could use the original capacity as the increment value.
slide-13
SLIDE 13

Brainstorming

  • The enlarge operation is costly ⇒ increment by large amount.
  • Increment too much ⇒ waste of both time & space.
  • Let us use henceforth the original capacity as the increment value:
  • Instantiate an array with a size equal to the current capacity plus the original capacity.
  • Remember the original capacity using an instance variable origiCap.
slide-14
SLIDE 14

Brainstorming

  • The enlarge operation is costly ⇒ increment by large amount.
  • Increment too much ⇒ waste of both time & space.
  • Let us use henceforth the original capacity as the increment value:
  • Instantiate an array with a size equal to the current capacity plus the original capacity.
  • Remember the original capacity using an instance variable origiCap.
slide-15
SLIDE 15

Brainstorming

  • The enlarge operation is costly ⇒ increment by large amount.
  • Increment too much ⇒ waste of both time & space.
  • Let us use henceforth the original capacity as the increment value:
  • Instantiate an array with a size equal to the current capacity plus the original capacity.
  • Remember the original capacity using an instance variable origiCap.
slide-16
SLIDE 16

Brainstorming

  • The enlarge operation is costly ⇒ increment by large amount.
  • Increment too much ⇒ waste of both time & space.
  • Let us use henceforth the original capacity as the increment value:
  • Instantiate an array with a size equal to the current capacity plus the original capacity.
  • Remember the original capacity using an instance variable origiCap.
slide-17
SLIDE 17

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1;

  • rigiCap = DEFAULTCAP;

} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

slide-18
SLIDE 18

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1;

  • rigiCap = DEFAULTCAP;

} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

slide-19
SLIDE 19

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1;

  • rigiCap = DEFAULTCAP;

} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

slide-20
SLIDE 20

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1;

  • rigiCap = DEFAULTCAP;

} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

slide-21
SLIDE 21

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1;

  • rigiCap = DEFAULTCAP;

} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

slide-22
SLIDE 22

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1;

  • rigiCap = DEFAULTCAP;

} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

slide-23
SLIDE 23

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1;

  • rigiCap = DEFAULTCAP;

} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

slide-24
SLIDE 24

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1;

  • rigiCap = DEFAULTCAP;

} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

slide-25
SLIDE 25

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1;

  • rigiCap = DEFAULTCAP;

} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

slide-26
SLIDE 26

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origiCap]; // Copy the elements from the smaller to the larger array. int currSmaller = front; for (int currLarger = 0; currLarger < numElements; currLarger++){ larger[currLarger] = queue[currSmaller]; currSmaller = (currSmaller+1)%queue.length; } // Update the instance variables. queue = larger; front = 0; rear = numElements - 1; } ... }

slide-27
SLIDE 27

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origiCap]; // Copy the elements from the smaller to the larger array. int currSmaller = front; for (int currLarger = 0; currLarger < numElements; currLarger++){ larger[currLarger] = queue[currSmaller]; currSmaller = (currSmaller+1)%queue.length; } // Update the instance variables. queue = larger; front = 0; rear = numElements - 1; } ... }

slide-28
SLIDE 28

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origiCap]; // Copy the elements from the smaller to the larger array. int currSmaller = front; for (int currLarger = 0; currLarger < numElements; currLarger++){ larger[currLarger] = queue[currSmaller]; currSmaller = (currSmaller+1)%queue.length; } // Update the instance variables. queue = larger; front = 0; rear = numElements - 1; } ... }

slide-29
SLIDE 29

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origiCap]; // Copy the elements from the smaller to the larger array. int currSmaller = front; for (int currLarger = 0; currLarger < numElements; currLarger++){ larger[currLarger] = queue[currSmaller]; currSmaller = (currSmaller+1)%queue.length; } // Update the instance variables. queue = larger; front = 0; rear = numElements - 1; } ... }

slide-30
SLIDE 30

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origiCap]; // Copy the elements from the smaller to the larger array. int currSmaller = front; for (int currLarger = 0; currLarger < numElements; currLarger++){ larger[currLarger] = queue[currSmaller]; currSmaller = (currSmaller+1)%queue.length; } // Update the instance variables. queue = larger; front = 0; rear = numElements - 1; } ... }

slide-31
SLIDE 31

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origiCap]; // Copy the elements from the smaller to the larger array. int currSmaller = front; for (int currLarger = 0; currLarger < numElements; currLarger++){ larger[currLarger] = queue[currSmaller]; currSmaller = (currSmaller+1)%queue.length; } // Update the instance variables. queue = larger; front = 0; rear = numElements - 1; } ... }

slide-32
SLIDE 32

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origiCap]; // Copy the elements from the smaller to the larger array. int currSmaller = front; for (int currLarger = 0; currLarger < numElements; currLarger++){ larger[currLarger] = queue[currSmaller]; currSmaller = (currSmaller+1)%queue.length; } // Update the instance variables. queue = larger; front = 0; rear = numElements - 1; } ... }

slide-33
SLIDE 33

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }

slide-34
SLIDE 34

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }

slide-35
SLIDE 35

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }

slide-36
SLIDE 36

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }

slide-37
SLIDE 37

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }

slide-38
SLIDE 38

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }

slide-39
SLIDE 39

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public Object dequeue() { if (isEmpty()){ System.out.println("Dequeue attempted on an empty queue!"); return null; } else { Object itemToReturn = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numElements = numElements - 1; return itemToReturn; } } ... }

slide-40
SLIDE 40

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public Object dequeue() { if (isEmpty()){ System.out.println("Dequeue attempted on an empty queue!"); return null; } else { Object itemToReturn = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numElements = numElements - 1; return itemToReturn; } } ... }

slide-41
SLIDE 41

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public Object dequeue() { if (isEmpty()){ System.out.println("Dequeue attempted on an empty queue!"); return null; } else { Object itemToReturn = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numElements = numElements - 1; return itemToReturn; } } ... }

slide-42
SLIDE 42

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public Object dequeue() { if (isEmpty()){ System.out.println("Dequeue attempted on an empty queue!"); return null; } else { Object itemToReturn = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numElements = numElements - 1; return itemToReturn; } } ... }

slide-43
SLIDE 43

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public boolean isEmpty() { return (numElements == 0); } }

slide-44
SLIDE 44

Implementation of our queue

public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public boolean isEmpty() { return (numElements == 0); } }

slide-45
SLIDE 45

The Java Library Collection Framework Queue

A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the “front”). However, it also has several differences:

  • It does not require elements to be inserted into the “rear” of the queue. For

example, inserted elements might be ordered based on a priority value.

slide-46
SLIDE 46

The Java Library Collection Framework Queue

A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the “front”). However, it also has several differences:

  • It does not require elements to be inserted into the “rear” of the queue. For

example, inserted elements might be ordered based on a priority value.

slide-47
SLIDE 47

The Java Library Collection Framework Queue

A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the “front”). However, it also has several differences:

  • It extends the Collection interface, so any class that implements it must

also implement eight predefined abstract methods (e.g., to observe the size

  • f the queue and to turn the queue into an array).
slide-48
SLIDE 48

The Java Library Collection Framework Queue

A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the “front”). However, it also has several differences:

  • No separate interfaces for bounded and unbounded queues exist. Instead,

the programmer chooses whether to place a limit on the number of elements a queue holds.

slide-49
SLIDE 49

The Java Library Collection Framework Queue

A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the “front”). However, it also has several differences:

  • It provides 2 operations for enqueuing:
  • add (throws an exception if invoked on a full queue)
  • offer (returns a boolean value of false if invoked on a full queue).
slide-50
SLIDE 50

The Java Library Collection Framework Queue

A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the “front”). However, it also has several differences:

  • It provides two operations for dequeuing:
  • remove (throws an exception)
  • poll (returns false, when invoked on an empty queue).
slide-51
SLIDE 51

The Java Library Collection Framework Queue

A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the “front”). However, it also has several differences:

  • Operations for obtaining the front element, without removing it, are also

included.

The Java library Collections Framework includes 9 classes that implement its Queue interface.

slide-52
SLIDE 52

The Java Library Collection Framework Queue

A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the “front”). However, it also has several differences:

  • Operations for obtaining the front element, without removing it, are also

included.

The Java library Collections Framework includes 9 classes that implement its Queue interface.

slide-53
SLIDE 53

Interesting applications

  • Identifying palindromes.

A palindrome is a string that reads the same forward and backward:

  • “Madam, I’m Adam.”
  • “Eve”
  • Implementing decks in card games 
slide-54
SLIDE 54

Interesting applications

  • Identifying palindromes.

A palindrome is a string that reads the same forward and backward:

  • “Madam, I’m Adam.”
  • “Eve”
  • Implementing decks in card games 
slide-55
SLIDE 55

The List ADT

  • Lists are one of the most widely used ADTs in computer science, which is
  • nly natural given how often we use them in daily life.
  • The list of the kinds of lists that we make is endless!
  • With a list, unlike with queues & stacks, we access elements within the

structure:

  • check if element is contained, insert name in alphabetical order, delete an entry
slide-56
SLIDE 56

The List ADT

  • Lists are one of the most widely used ADTs in computer science, which is
  • nly natural given how often we use them in daily life.
  • The list of the kinds of lists that we make is endless!
  • With a list, unlike with queues & stacks, we access elements within the

structure:

  • check if element is contained, insert name in alphabetical order, delete an entry
slide-57
SLIDE 57

The List ADT

  • Lists are one of the most widely used ADTs in computer science, which is
  • nly natural given how often we use them in daily life.
  • The list of the kinds of lists that we make is endless!
  • With a list, unlike with queues & stacks, we access elements within the

structure:

  • check if element is contained, insert name in alphabetical order, delete an entry
slide-58
SLIDE 58

The List ADT

  • We only need a few variations of the List ADT to represent the majority of real-

world lists:

  • unsorted, sorted, indexed
  • Most differences in list types relate to the types of values that they store, rather

than to their structure or operations.

  • An important structural difference is how the values are ordered with respect to
  • ne another within each type of list.
  • List operations sometimes depend on the content of the list elements → let us see

how we can compare objects.

slide-59
SLIDE 59

The List ADT

  • We only need a few variations of the List ADT to represent the majority of real-

world lists:

  • unsorted, sorted, indexed
  • Most differences in list types relate to the types of values that they store, rather

than to their structure or operations.

  • An important structural difference is how the values are ordered with respect to
  • ne another within each type of list.
  • List operations sometimes depend on the content of the list elements → let us see

how we can compare objects.

slide-60
SLIDE 60

The List ADT

  • We only need a few variations of the List ADT to represent the majority of real-

world lists:

  • unsorted, sorted, indexed
  • Most differences in list types relate to the types of values that they store, rather

than to their structure or operations.

  • An important structural difference is how the values are ordered with respect to
  • ne another within each type of list.
  • List operations sometimes depend on the content of the list elements → let us see

how we can compare objects.

slide-61
SLIDE 61

The List ADT

  • We only need a few variations of the List ADT to represent the majority of real-

world lists:

  • unsorted, sorted, indexed
  • Most differences in list types relate to the types of values that they store, rather

than to their structure or operations.

  • An important structural difference is how the values are ordered with respect to
  • ne another within each type of list.
  • List operations sometimes depend on the content of the list elements → let us see

how we can compare objects.

slide-62
SLIDE 62

The equals method

  • The equals method, as defined in the Object class, acts in much the same

way as the comparison operator. It returns true if and only if the two variables reference the same object.

  • We can redefine the method within a class to fit the goals of the class.
  • We can then use the method to, e.g., check if a particular element is on a

particular list.

slide-63
SLIDE 63

The equals method

  • The equals method, as defined in the Object class, acts in much the same

way as the comparison operator. It returns true if and only if the two variables reference the same object.

  • We can redefine the method within a class to fit the goals of the class.
  • We can then use the method to, e.g., check if a particular element is on a

particular list.

slide-64
SLIDE 64

The equals method

  • The equals method, as defined in the Object class, acts in much the same

way as the comparison operator. It returns true if and only if the two variables reference the same object.

  • We can redefine the method within a class to fit the goals of the class.
  • We can then use the method to, e.g., check if a particular element is on a

particular list.

slide-65
SLIDE 65

The equals method

8 8

int A int B “int A == int B” evaluates to true.

slide-66
SLIDE 66

The equals method

8 5

int A int B “int A == int B” evaluates to false.

slide-67
SLIDE 67

The equals method

Circle c1 Circle c2 “c1 == c2” evaluates to false.

slide-68
SLIDE 68

The equals method

Circle c1 Circle c2 “c1 == c2” evaluates to true.

slide-69
SLIDE 69

The equals method

public class Circle { int radius; Circle(int someRadius){ this.radius = someRadius; } public boolean equals(Circle circle){ if(this.radius == circle.radius) return true; else return false; } public static void main(String[] args) { Circle c1 = new Circle(3); System.out.println(c1.radius); Circle c2 = new Circle(3); System.out.println(c2.equals(c1)); } }

We redefine the equals method to fit the goals of the class.

slide-70
SLIDE 70

Summary

  • The Queue ADT
  • Implementation of unbounded queues
  • The Java Library Collection Framework Queue
  • Introduction to Lists and the equals method
  • Next:The List ADT