Download Queue

Document related concepts
no text concepts found
Transcript
Queue Definition
A queue is a data structure with elements of
the same type. Queue elements can only be
entered at one end of the queue, called the
back, and removed from the other end of the
queue, called the front, in a First In First Out
(FIFO) manner.
A Print Queue stores print jobs until
the printer is ready to print them.
It gets its name from the fact that the
print jobs are printed in a FIFO manner.
enQueue or add
Operation
enQueue, or add in Java, is a queue
operation that adds a new element at
the back end of a queue, which is the
opposite end from where queue
elements are removed.
deQueue or remove
Operation
deQueue, or remove in Java, is a
queue operation that removes an
existing element from the front end of
a queue, which is the opposite end
from where queue elements are added.
peek Operation
peek is a queue operation that returns an
existing element from the front end of a
queue without removing the element.
isEmpty Operation
isEmpty is a queue operation that
determines if a queue is empty.
Constructing a Queue – 1
New integer queue
Front
Back
Constructing a Queue – 2
enQueue(157)
Front
157
Back
Constructing a Queue – 3
enQueue(999)
Front
157
999
Back
Constructing a Queue – 4
enQueue(500)
Front
157
999
500
Back
Constructing a Queue – 4
x = deQueue()
Front
999
x
157
500
Back
Constructing a Queue – 5
enQueue(x)
Front
999
x
157
500
157
Back
Constructing a Queue – 6
x = deQueue()
Front
500
x
999
157
Back
Constructing a Queue – 7
enQueue(x)
Front
500
x
999
157
999
Back
Constructing a Queue – 8
enQueue(x)
Front
500
x
999
157
999
999
Back
Constructing a Queue – 9
x = deQueue()
Front
157
x
500
999
999
Back
// Java3001.java
// This file is not a program, but the method headings of the <Queue> interface.
public boolean isEmpty()
// Returns true if queue is empty, false otherwise
{
}
public boolean add (E item)
// used to be called enQueue
// Adds variable item to the back of the queue; returns true
{
}
public E remove()
// used to be called deQueue
// Returns and removes the front element from the queue
{
}
public E peek()
// Returns the front element from the queue without removal
{
}
// Java3002.java
// This program uses a <Queue> object to store students names.
// It demonstrates the use of the <add> and <peek> methods.
import java.util.*;
public class Java3002
{
public static void main (String args[])
{
System.out.println("JAVA3002.JAVA\n");
Queue students = new LinkedList();
students.add("Luke Watts");
System.out.println("Enqueueing Luke Watts");
System.out.println("Queue front contains " + students.peek());
students.add("Brian Sims");
System.out.println("\nEnqueueing Brian Sims");
System.out.println("Queue front contains " + students.peek());
students.add("Mike Lewis");
System.out.println("\nEnqueueing Mike Lewis");
System.out.println("Queue front contains " + students.peek());
students.add("Jamie Singbush");
System.out.println("\nEnqueueing Jamie Singbush");
System.out.println("Queue front contains " + students.peek());
System.out.println();
}
}
Using the Queue interface
Identifier Queue is an interface and has no constructor.
Objects of the Queue interface must be instantiated with an
implementing class.
In Java the LinkedList class and the PriorityQueue class both
implement the Queue interface.
In this chapter some Queue implementations will use the
LinkedList class in the following "older" manner :
Queue students = new LinkedList();
You will also see some Queue implementations with the same
LinkedList class, along with the identifier of the class object that
will be stored in the queue, using the "generic" class approach:
Queue<String> temp = new LinkedList<String>();
// Java3003.java
This program introduces the <remove> and <isEmpty> methods.
//
This program uses "class casting" with the <remove> method.
import java.util.*;
public class Java3003
{
public static void main (String args[])
{
System.out.println("JAVA3003.JAVA\n");
Queue students = new LinkedList();
students.add("Luke Watts");
System.out.println("Enqueueing Luke Watts");
students.add("Brian Sims");
System.out.println("Enqueueing Brian Sims");
students.add("Mike Lewis");
System.out.println("Enqueueing Mike Lewis");
students.add("Jamie Singbush");
System.out.println("Enqueueing Jamie Singbush");
System.out.println();
while (!students.isEmpty())
{
String student = (String) students.remove();
System.out.println("Dequeueing " + student);
}
System.out.println();
if (students.isEmpty())
System.out.println("The queue is empty");
else
System.out.println("Queue front contains " + students.peek());
System.out.println();
}
}
// Java3004.java
// This program shows how to display the elements in a queue with a temporary queue.
// This also demonstrates how a queue accesses elements as a FIFO.
// This program uses the Java 5.0 "generics" feature.
import java.util.*;
public class Java3004
{
public static void main (String args[])
{
System.out.println("JAVA3004.JAVA\n");
Queue<String> students = new LinkedList<String>();
Queue<String> temp = new LinkedList<String>();
students.add("Luke Watts");
students.add("Brian Sims");
students.add("Mike Lewis");
students.add("Jamie Singbush");
while (!students.isEmpty())
{
String student = students.remove();
System.out.println("deQueueing " + student +
" from student queue; enQueueing on temp queue");
temp.add(student);
}
System.out.println();
while (!temp.isEmpty())
{
String student = temp.remove();
System.out.println("deQueueing " + student + " from temp queue");
}
System.out.println();
}
}
Using a Temporary Queue
A queue can only be dequeued at one location.
Access to any element besides the front element,
requires that the front element and possibly
additional elements are dequeued from the
queue.
A temporary queue needs to store dequeued
elements so that all the necessary elements can
be returned to the original queue.
// Java3005.java
This program shows how to remove an element from a <Queue> object
//
and "supposedly" preserve the remaining object sequence.
import java.util.*;
public class Java3005
{
public static void main (String args[])
{
System.out.println("JAVA3005.JAVA\n");
Queue<String> students = new LinkedList<String>();
Queue<String> temp = new LinkedList<String>();
students.add("Luke Watts");
students.add("Brian Sims");
students.add("Mike Lewis");
students.add("Jamie Singbush");
System.out.println(students);
boolean found = false;
while (!students.isEmpty() && !found)
{
String student = students.remove();
if (student.equals("Mike Lewis"))
{
System.out.println(student + " is found and removed from the queue");
found = true;
}
else temp.add(student);
}
System.out.println();
while (!temp.isEmpty())
{
String student = temp.remove();
students.add(student);
}
System.out.println(students);
System.out.println();
}
}
Java3005 Logic Error Demo
Step 1
Luke Brian Mike Jamie
Front
Front
Back
Back
Java3005 Logic Error Demo
Step 2
Mike Jamie
Front
Back
Luke Brian
Front
Back
Java3005 Logic Error Demo
Step 3 - Mike is deleted
Jamie
Front
Back
Luke Brian
Front
Back
Java3005 Logic Error Demo
Step 4 - temp items returned out of order
Jamie Luke Brian
Front
Front
Back
Back
// Java3006.java
// This program shows how to remove an element from a <MyQueue> object
// and preserve the remaining object sequence correctly.
import java.util.*;
public class Java3006
{
public static void main (String args[])
{
System.out.println("JAVA3006.JAVA\n");
Queue<String> students = new LinkedList<String>();
Queue<String> temp = new LinkedList<String>();
students.add("Luke Watts");
students.add("Brian Sims");
students.add("Mike Lewis");
students.add("Jamie Singbush");
System.out.println(students);
while (!students.isEmpty())
{
String student = students.remove();
if (student.equals("Mike Lewis"))
System.out.println(student + " is found and removed from the queue");
else
temp.add(student);
}
System.out.println();
while (!temp.isEmpty())
{
String student = temp.remove();
students.add(student);
}
System.out.println(students);
System.out.println();
}
}
Java3006 No Error Demo
Step 1
Luke Brian Mike Jamie
Front
Front
Back
Back
Java3006 No Error Demo
Step 2
Mike Jamie
Front
Back
Luke Brian
Front
Back
Java3005 Logic Error Demo
Step 3 - Mike is deleted
Jamie
Front
Back
Luke Brian
Front
Back
Java3005 Logic Error Demo
Step 4 - students queue is emptied
Front
Back
Luke Brian Jamie
Front
Back
Java3005 Logic Error Demo
Step 5 - All temp items returned in order
Luke Brian Jamie
Front
Front
Back
Back
// Java3007.java
// This program demonstrates how to use a primitive type like <int> with a queue.
// This program uses Java 5.0 "autoboxing" and "generics" features.
import java.util.*;
public class Java3007
{
public static void main (String args[])
{
System.out.println("JAVA3007.JAVA\n");
Queue<Integer> numbers = new LinkedList<Integer>();
for (int k = 1000; k <= 1008; k++)
{
System.out.println("Enqueueing " + k + " on the queue.");
numbers.add(k);
}
System.out.println();
while (!numbers.isEmpty())
{
int number = numbers.remove();
System.out.println("Dequeueing " + number + " from the queue.");
}
System.out.println();
}
}
// Java3008.java
// This program creates a <Queue> object of <Person> objects that are retrieved from an external file.
// Java "generics" is not limited to existing classes.
// In this program the queue object is declared to store <Person> objects.
import java.io.*;
import java.util.*;
public class Java3008
{
public static void main(String args[]) throws IOException
{
System.out.println("JAVA3008.JAVA\n");
BufferedReader inStream = new BufferedReader(new FileReader("Students.txt"));
Person student;
Queue<Person> students = new LinkedList<Person>();
String s1,s2,s3;
while( ((s1 = inStream.readLine()) != null) &&
((s2 = inStream.readLine()) != null) && ((s3 = inStream.readLine()) != null) )
{
String name = s1;
int age = Integer.parseInt(s2);
double gpa = Double.parseDouble(s3);
student = new Person(name,age,gpa);
System.out.println("Enqueueing " + student.getName() + " \t\t" + student.getAge() +
"\t\t" + student.getGPA());
students.add(student);
}
inStream.close();
System.out.println();
while (!students.isEmpty())
{
student = students.deQueue();
System.out.println("Dequeueing " + student.getName() + " \t\t" + student.getAge() +
"\t\t" + student.getGPA());
}
System.out.println();
}
}
class Person
{
private String name;
private int age;
private double gpa;
Person(String n,int a,double g)
{
name = n;
age = a;
gpa = g;
}
public String getName() { return name; }
public int getAge()
{ return age; }
public double getGPA() { return gpa; }
}
// Java3009.java
// This program demonstrates how it is possible to stores elements of different data
// types on a queue. Every queue element is technically the same <Object> type, but
// in reality it is a reference to four different data type elements.
// This is not a proper way to use a queue data structure.
import java.util.*;
public class Java3009
{
public static void main (String args[])
{
System.out.println("JAVA3009.JAVA\n");
Queue queueObj = new LinkedList();
int element1 = 1234;
double element2 = 3.14159;
String element3 = "Grace Hopper";
Person element4 = new Person("John Doe",25,3.475);
queueObj.add(new Integer(element1));
System.out.println("Enqueueing 1234 on the stack");
queueObj.add(new Double(element2));
System.out.println("Enqueueing 3.14159 on the stack");
queueObj.add(element3);
System.out.println("Enqueueing Grace Hopper on the stack");
queueObj.add(element4);
System.out.println("Enqueueing John Doe (25 & 3.475) on the stack");
}
}
Queue Data Note
Every element in a queue is the same data type.
If this data type is a reference, like it is was with
implementation of the Queue interface used in
the previous program example, then it is
possible to store different types of data at the
referenced memory location.
// Java3010.java
// This program implements an <int> queue data structure with a static Java array.
public class Java3010
{
public static void main (String args[])
{
System.out.println("JAVA3010.JAVA\n");
MyQueue1 queue1 = new MyQueue1();
queue1.add(1111);
queue1.add(2222);
queue1.add(3333);
queue1.add(4444);
System.out.println();
if (queue1.isEmpty())
System.out.println("The queue is empty");
else
System.out.println("Queue front contains " + queue1.peek());
System.out.println();
while (!queue1.isEmpty())
{
int number = queue1.remove();
System.out.println(number);
}
System.out.println();
}
}
class MyQueue1
{
private int data[];
private int front;
private int back;
// stores queue data
// keeps index of the queue front
// keeps index of the queue back
public MyQueue1()
// Initializes an empty array object.
{
data = new int[100];
back = front = 0;
}
public void add (int x)
// Adds variable x to the top of the queue
{
data[back] = x;
back++;
}
public boolean isEmpty()
// Returns true if data is empty,
// false otherwise
{
return front == back;
}
public int peek() { return data[front]; }
// Returns the front element from
// the queue without removal
public int remove()
// Returns and removes the front element
// from the queue
{
int temp = data[front];
front++;
return temp;
}
}
// Java3011.java
// This program shows that this queue implementation can have problems even if there
// should be sufficient storage space.
public class Java3011
{
public static void main (String args[])
{
System.out.println("JAVA3011.JAVA\n");
MyQueue2 queue2 = new MyQueue2();
for (int k = 10; k <= 99; k++)
queue2.add(k);
for (int k = 10; k <= 60; k++)
{
int number = queue2.remove();
System.out.print(number + " ");
}
for (int k = 100; k <= 150; k++)
queue2.add(k);
System.out.println();
}
}
enQueue 90 integers
deQueue 51 integers
enQueue 51 more integers
Why did the previous
program crash?
Front
157
In this example, the queue can hold
6 integers. Even though there are
only 3 integers in the queue, any
attempt to enqueue another integer
will cause the program to crash.
999
999
Back
// Java3012.java
// This program solves the problem of insufficient storage by using
// "wrap-around" logic with the static array data structure.
public class Java3012
{
public static void main (String args[])
{
System.out.println("JAVA3012.JAVA\n");
MyQueue3 queue3 = new MyQueue3();
for (int k = 0; k < 90; k++)
queue3.add(k);
for (int k = 0; k < 60; k++)
{
int number = queue3.remove();
System.out.print(number + " ");
}
for (int k = 60; k < 120; k++)
queue3.add(k);
System.out.println("\n\n");
while (!queue3.isEmpty())
{
int number = queue3.remove();
System.out.print(number + " ");
}
System.out.println("\n\n");
}
}
enQueue 90 integers
deQueue 60 integers
enQueue 60 more integers
class MyQueue3
{
private int data[];
// stores queue data
private int front;
// keeps index of the queue front
private int back;
// keeps index of the queue back
public void add (int x)
// Adds variable x to the "back" of the queue
{
if (back == data.length)
back = 0;
data[back] = x;
back++;
}
public int remove()
// Returns and removes the "front" element
// from the queue
{
if (front == data.length)
front = 0;
int temp = data[front];
front++;
return temp;
}
public MyQueue3()
// Initializes an empty array object.
{
data = new int[100];
back = 0;
front = 0;
}
public boolean isEmpty()
// Returns true if data is empty, false otherwise
{
return back == front;
}
public int peek() { return data[front]; }
// Returns the front element from the queue
// without removal
}
Wrap-Around Logic - 1
Front
157
This queue is NOT full, but the
back pointer is at the last index
of the static array.
999
999
Back
Wrap-Around Logic - 2
enQueue(500)
Front
500
Back
157
999
999
Wrap-Around Logic - 3
enQueue(747)
Front
500
747
Back
157
999
999
Wrap-Around Logic - 4
deQueue()
Front
500
747
Back
999
999
Wrap-Around Logic - 5
enQueue(365)
Front
500
747
365
Back
999
999
Wrap-Around Logic - 6
deQueue()
Front
500
747
365
Back
999
Wrap-Around Logic - 7
deQueue()
Front
500
747
365
Back
Wrap-Around Logic - 8
enQueue(121)
Front
500
747
365
121
Back
Wrap-Around Logic - 9
enQueue(316)
Front
500
747
365
121
316
Back
Wrap-Around Logic - 10
deQueue()
Front
747
365
121
316
Back
Wrap-Around Logic - 11
enQueue(441)
Front
747
365
121
316
441
Back
Wrap-Around Logic - 12
enQueue(555)
Front
555
Back
747
365
121
316
441
NOTE: Even with Wrap-Around Logic, a
Queue implemented as a Java Static
Array can still run out of space.
// Java3013.java
// This program shows the implementation of a dynamic queue class.
// MyQueue4 is also declared as a generic class.
import java.util.ArrayList;
public class Java3013
{
public static void main (String args[])
{
System.out.println("JAVA3013.JAVA\n");
MyQueue4<String> students = new MyQueue4<String>();
students.add("Luke Watts");
students.add("Brian Sims");
students.add("Mike Lewis");
students.add("Jamie Singbush");
System.out.println();
if (students.isEmpty())
System.out.println("The queue is empty");
else
System.out.println("Queue front contains " + students.peek());
System.out.println();
while (!students.isEmpty())
{
String student = (String) students.remove();
System.out.println(student);
}
System.out.println();
}
class MyQueue4<E>
{
private ArrayList<E> data;
// stores queue data
private int front;
// keeps index of the queue front
public MyQueue4()
// Initializes an empty queue object.
{
data = new ArrayList<E>();
front = 0;
}
public boolean isEmpty()
// Returns true if data is empty, false otherwise
{
return data.size() == 0;
}
public void add(E x)
// Adds variable x to the back of the queue
{
data.add(x);
}
public E remove()
// Returns and removes the front element
from the queue
{
return data.remove(front);
}
public E peek()
// Returns the front element from the
queue without removal
{
return data.get(front);
}
}
// Java3014.java
// This program shows how the queue implementation with an <ArrayList> object
// works correctly without using the "wrap-around" data storage.
import java.util.ArrayList;
public class Java3014
{
public static void main (String args[])
{
System.out.println("JAVA3014.JAVA\n");
MyQueue4<Integer> queue = new MyQueue4<Integer>();
for (int k = 0; k < 90; k++)
queue.add(new Integer(k));
for (int k = 0; k < 60; k++)
{
int number = queue.remove();
System.out.print(number + " ");
}
for (int k = 60; k < 120; k++)
queue.add(new Integer(k));
System.out.println("\n\n");
while (!queue.isEmpty())
{
int number = queue.remove();
System.out.print(number + " ");
}
System.out.println("\n\n");
}
}
enQueue 90 integers
deQueue 60 integers
enQueue 60 more integers
// Java3015.java
// This program demonstrates <Queue> interface capabilities that should NOT be
// used in an AP course. There are methods available due to inheritance features
// that do not follow queue ADT properties. In an AP course access to a queue is
// strictly meant to add at the end and remove from the front. Queue methods
// should be limited to using <add>, <remove()>, <isEmpty>, <peek> only.
import java.util.*;
public class Java3015
{
public static void main(String args[])
{
System.out.println("Java3015.java\n\n");
Queue<Integer> queue = new LinkedList<Integer>();
for (int k = 1; k <= 10; k++)
queue.add(k);
System.out.println("Removing number 3");
queue.remove(3);
System.out.println(queue);
System.out.println("\n\n");
}
}
APCS Exam Alert
Only use methods add, remove, peek
and isEmpty with any Queue objects.
Use of other methods like get or
remove(index) violates the abstract
data definition of a queue, and may result
in point deductions if used on solutions
with the free response segment of the
exam.
// Java3016.java
// Complete method <delete> so that it removes the parameter number, but
// leaves the remaining queue intact.
import java.util.*;
public class Java3016
{
public static void main(String args[])
{
System.out.println("Java3016.java\n\n");
Queue<Integer> queue = new LinkedList<Integer>();
for (int k = 1; k <= 10; k++)
queue.add(k);
System.out.println(queue);
delete(queue,5);
System.out.println(queue);
System.out.println("\n\n");
}
public static void delete(Queue<Integer> q, int number)
{
}
}
// Java3017.java
// Complete method <reverse> so that it reverses the queue members.
import java.util.*;
public class Java3017
{
public static void main(String args[])
{
System.out.println("Java3017.java\n\n");
Queue<Integer> queue = new LinkedList<Integer>();
for (int k = 1; k <= 10; k++)
queue.add(k);
System.out.println(queue);
reverse(queue);
System.out.println(queue);
System.out.println("\n\n");
}
public static void reverse(Queue<Integer> q)
{
}
}
// Java3018.java
// This program is intended to demonstrate how to make
// a copy of an existing queue. The output appears correct.
// Are you satisfied that this is correct?
import java.util.*;
public class Java3018
{
public static void main(String args[])
{
System.out.println("Java3018.java\n\n");
Queue<Integer> queue1 = new LinkedList<Integer>();
Queue<Integer> queue2 = new LinkedList<Integer>();
for (int k = 1; k <= 10; k++)
queue1.add(k);
queue2 = queue1;
System.out.println(queue1);
System.out.println(queue2);
System.out.println("\n\n");
}
}
// Java3019.java
// This program shows that simple assignment does not work for copying
// objects. This is known as an "aliasing" problem.
import java.util.*;
public class Java3019
{
public static void main(String args[])
{
System.out.println("Java3019.java\n\n");
Queue<Integer> queue1 = new LinkedList<Integer>();
Queue<Integer> queue2 = new LinkedList<Integer>();
for (int k = 1; k <= 10; k++)
queue1.add(k);
queue2 = queue1;
queue2.remove();
System.out.println(queue1);
System.out.println(queue2);
System.out.println("\n\n");
}
}
// Java3020.java
// Complete method <copy> so that the second parameter becomes
// a correct copy of the first parameter.
import java.util.*;
public class Java3020
{
public static void main(String args[])
{
System.out.println("Java3020.java\n\n");
Queue<Integer> queue1 = new LinkedList<Integer>();
Queue<Integer> queue2 = new LinkedList<Integer>();
for (int k = 1; k <= 10; k++)
queue1.add(k);
copy(queue1,queue2);
queue2.remove();
System.out.println(queue1);
System.out.println(queue2);
System.out.println("\n\n");
}
public static void copy(Queue<Integer> q1, Queue<Integer> q2)
{
}
}
// Java3021.java
// This program demonstrates how to declare a <PriorityQueue> object
// and add new elements with the <add> method.
import java.util.*;
public class Java3021
{
public static void main(String args[])
{
System.out.println("Java3021.java\n\n");
PriorityQueue<String> pqueue = new PriorityQueue<String>();
pqueue.add("Ann");
pqueue.add("Bob");
pqueue.add("Dee");
pqueue.add("Eve");
pqueue.add("Joe");
System.out.println(pqueue);
System.out.println("\n\n");
}
}
// Java3022.java
// This program demonstrates the <isEmpty> and <remove> methods of the
// <PriorityQueue> class.
// At this stage the <PriorityQueue> class appears no different from the <Queue> class.
import java.util.*;
public class Java3022
{
public static void main(String args[])
{
System.out.println("Java3022.java\n\n");
PriorityQueue<String> pqueue = new PriorityQueue<String>();
pqueue.add("Ann");
pqueue.add("Bob");
pqueue.add("Dee");
pqueue.add("Eve");
pqueue.add("Joe");
while(!pqueue.isEmpty())
System.out.println(pqueue.remove());
System.out.println("\n\n");
}
}
// Java3023.java
// This program demonstrates the <peek> method, which displays the value at the "head"
// of the queue. This priority queue appears to base priority on ascending order of values.
import java.util.*;
public class Java3023
{
public static void main(String args[])
{
System.out.println("Java3023.java\n\n");
PriorityQueue<String> pqueue = new PriorityQueue<String>();
pqueue.add("Bob");
System.out.println("Head value is " + pqueue.peek());
pqueue.add("Dee");
System.out.println("Head value is " + pqueue.peek());
pqueue.add("Eve");
System.out.println("Head value is " + pqueue.peek());
pqueue.add("Ann");
System.out.println("Head value is " + pqueue.peek());
pqueue.add("Joe");
System.out.println("Head value is " + pqueue.peek());
System.out.println("\n\n");
}
}
// Java3024.java
// This program demonstrates that the <toString> implementation of the <PriorityClass>
// is only a representation of stored values, but provides no indication about the
// priority of the data.
import java.util.*;
public class Java3024
{
public static void main(String args[])
{
System.out.println("Java3024.java\n\n");
PriorityQueue<String> pqueue = new PriorityQueue<String>();
pqueue.add("Bob");
System.out.println(pqueue);
pqueue.add("Dee");
System.out.println(pqueue);
pqueue.add("Eve");
System.out.println(pqueue);
pqueue.add("Ann");
System.out.println(pqueue);
pqueue.add("Joe");
System.out.println(pqueue);
System.out.println("\nActual priority order");
while(!pqueue.isEmpty())
System.out.print(pqueue.remove() + " ");
System.out.println("\n\n");
}
}
// Java3025.java
// This program demonstrates that priority ordering also applies to numeric values in
// ascending order. This program also demonstrates again that using <println> gives
// no indication to any priority ordering.
import java.util.*;
public class Java3025
{
public static void main(String args[])
{
System.out.println("Java3025.java\n\n");
PriorityQueue<Integer> pqueue = new PriorityQueue<Integer>();
Random rnd = new Random(1234);
for (int k = 1; k <= 48; k++)
{
int rndInt = rnd.nextInt(900) + 100;
pqueue.add(rndInt);
}
System.out.println(pqueue);
System.out.println("\n\n");
while(!pqueue.isEmpty())
System.out.print(pqueue.remove() + " ");
System.out.println("\n\n");
}
}
// Java3026.java
// This program investigates using a priority queue with a user-defined class.
// The program compiles, but it crashes with a "ClassCastException" runtime error.
import java.util.*;
public class Java3026
{
public static void main(String args[])
{
System.out.println("Java3026.java\n\n");
PriorityQueue pqueue = new PriorityQueue();
pqueue.add(new Student("Tom",21,2.785));
pqueue.add(new Student("Ann",40,3.555));
pqueue.add(new Student("Joe",61,2.000));
pqueue.add(new Student("Bob",35,3.275));
pqueue.add(new Student("Dee",55,3.999));
while(!pqueue.isEmpty())
System.out.print(pqueue.remove() + " ");
System.out.println("\n\n");
}
}
class Student
{
private String name;
private int age;
private double gpa;
public Student(String n, int a, double g) { name = n; age = a; gpa = g; }
public String toString() { return name; }
}
// Java3027.java
// A <PriorityQueue> object can only stores objects that have implemented <Comparable>.
// There is no problem using a user-defined class, but it must define the <compareTo> method.
// This program uses the priority queue ordered according to student name.
import java.util.*;
public class Java3027
{
public static void main(String args[])
{
System.out.println("Java3027.java\n\n");
PriorityQueue<Student> pqueue = new PriorityQueue<Student>();
pqueue.add(new Student("Tom",21,2.785));
pqueue.add(new Student("Ann",40,3.555));
pqueue.add(new Student("Joe",61,2.000));
pqueue.add(new Student("Bob",35,3.275));
pqueue.add(new Student("Dee",55,3.999));
while(!pqueue.isEmpty())
System.out.print(pqueue.remove() + " ");
System.out.println("\n\n");
}
}
class Student implements Comparable
{
private String name;
private int age;
private double gpa;
public Student(String n, int a, double g) { name = n; age = a; gpa = g; }
public String toString() { return name; }
// Note how defining <compareTo> use the <String> class implementation of <compareTo>
}
public int compareTo (Object source)
{
Student temp = (Student) source;
return name.compareTo(temp.name);
}
// Java3028.java
// This program is almost identical to the previous program. The implementation of <compareTo>
// is slightly different and results in a descending order priority.import java.util.*;
public class Java3028
{
public static void main(String args[])
{
System.out.println("Java3028.java\n\n");
PriorityQueue<Student> pqueue = new PriorityQueue<Student>();
pqueue.add(new Student("Tom",21,2.785));
pqueue.add(new Student("Ann",40,3.555));
pqueue.add(new Student("Joe",61,2.000));
pqueue.add(new Student("Bob",35,3.275));
pqueue.add(new Student("Dee",55,3.999));
while(!pqueue.isEmpty())
System.out.print(pqueue.remove() + " ");
System.out.println("\n\n");
}
}
class Student implements Comparable
{
private String name;
private int age;
private double gpa;
public Student(String n, int a, double g) { name = n; age = a; gpa = g; }
public String toString() { return name; }
public int compareTo (Object source)
{
Student temp = (Student) source;
return temp.name.compareTo(name);
}
}
// Java3029.java
// This program creates a priority queue that is ordered according to student age.
// The <compareTo> method is implemented differently, because <int> cannot be used
// with <compareTo>.
// The <toString> method is redefined to display age.
import java.util.*;
public class Java3029
{
public static void main(String args[])
{
System.out.println("Java3029.java\n\n");
PriorityQueue<Student> pqueue = new PriorityQueue<Student>();
pqueue.add(new Student("Tom",21,2.785));
pqueue.add(new Student("Ann",40,3.555));
pqueue.add(new Student("Joe",61,2.015));
pqueue.add(new Student("Bob",35,3.275));
pqueue.add(new Student("Dee",55,3.999));
while(!pqueue.isEmpty())
System.out.println(pqueue.remove());
System.out.println("\n\n");
}
}
class Student implements Comparable
{
private String name;
private int age;
private double gpa;
public Student(String n, int a, double g) { name = n; age = a; gpa = g; }
public int compareTo (Object source)
{
Student temp = (Student) source;
if (age < temp.age)
return -1;
else if (age == temp.age)
NOTE:
return 0;
return
else
will also
return 1;
}
age - temp.age;
work.
public String toString() { return String.valueOf(age); }
}
// Java3030.java
// Complete the <compareTo> method below to order the priority queue from highest to lowest gpa.
// Redefine the <toString> method to display student records in the format [Tom, 21, 2.785];
import java.util.*;
public class Java3030
{
public static void main(String args[])
{
System.out.println("Java3030.java\n\n");
PriorityQueue<Student> pqueue = new PriorityQueue<Student>();
pqueue.add(new Student("Tom",21,2.785));
pqueue.add(new Student("Ann",40,3.555));
pqueue.add(new Student("Joe",61,2.015));
pqueue.add(new Student("Bob",35,3.275));
pqueue.add(new Student("Dee",55,3.999));
while(!pqueue.isEmpty())
System.out.println(pqueue.remove());
System.out.println("\n\n");
}
}
class Student implements Comparable
{
private String name;
private int age;
private double gpa;
public Student(String n, int a, double g) { name = n; age = a; gpa = g; }
public int compareTo (Object source) { }
public String toString() { }
}
Related documents