Description (Part A) In this lab you will write a Queue implementation called CoolQueue, which
closely matches the java.util.Queue collection, except with fewer methods. Test code is provided
that allows you to compare your queue to the one in java.util.Queue, which you can assume is
completely correct.
Getting Started
Create a project called Lab9 and download following files:
QueueInterface.java
TestProgram.java
CoolQueue.java
Fill in the supplied CoolQueue.java to make it implement the provided QueueInterface methods.
Then use the TestProgram to verify that your code works. Here are the methods in the interface:
public interface QueueInterface {
// Add object onto rear of queue
public abstract boolean add(E item);
// Remove object from front of queue // Throws NoSuchElementException if empty
public abstract E remove();
// Peek object from front of queue // Throws NoSuchElementException if empty
public abstract E element();
// Check for empty
public abstract boolean isEmpty();
// Returns size
public abstract int size();
// Clears queue
public abstract void clear();
// Various searches
public abstract boolean contains(Object o);
// Override toString
public abstract String toString(); }
Instructions
Inherit the interface provided above.
The Queue is a generic data structure, so declare an LinkedList.
HINT: You should use the first entry or head of the LinkedList as the front of the queue.
HINT: You should use the last entry or tail of the LinkedList as the rear of the queue.
You must not use add(), remove(), or element() in the LinkedList object.
You must implement toString() first, format is the same as LinkedList.toString().
Incremental development, so test one method at a time.
Review the TestProgram to see what is tested by each test number, 1 through 7.
Below is the CoolQueue.java:
import java.util.LinkedList;
import java.util.NoSuchElementException;
class CoolQueue implements QueueInterface {
// Underlying data structure
// Default constructor
}
Below is the TestProgram.java:
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Random;
public class TestProgram {
static Random random = new Random();
public static void main(String[] args) {
// Make a Java stack
Queue javaQueue = new LinkedList<>();
// Make a student stack
CoolQueue coolQueue = new CoolQueue<>();
// Which test?
int testNumber = Integer.parseInt(args[0]);
switch (testNumber) {
case 1: // testAdd
System.err.println(\"testAdd: verifying add() method\");
pushRandom(javaQueue, 10);
pushRandom(coolQueue, 10);
System.err.println(\"Java Queue: \" + javaQueue);
System.err.println(\"Cool Queue: \" + coolQueue);
break;
case 2: // testRemove
System.err.println(\"testRemove: verifying remove() method\");
pushRandom(javaQueue, 10);
pushRandom(coolQueue, 10);
// Legal removes
for (int i = 0; i < 10; i++) {
System.err.printf(\"javaQueue.remove(): %4s, coolQueue.remove(): %4s\ \",
javaQueue.remove(), coolQueue.remove());
}
// Queue e.
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Description (Part A) In this lab you will write a Queue implementati.pdf
1. Description (Part A) In this lab you will write a Queue implementation called CoolQueue, which
closely matches the java.util.Queue collection, except with fewer methods. Test code is provided
that allows you to compare your queue to the one in java.util.Queue, which you can assume is
completely correct.
Getting Started
Create a project called Lab9 and download following files:
QueueInterface.java
TestProgram.java
CoolQueue.java
Fill in the supplied CoolQueue.java to make it implement the provided QueueInterface methods.
Then use the TestProgram to verify that your code works. Here are the methods in the interface:
public interface QueueInterface {
// Add object onto rear of queue
public abstract boolean add(E item);
// Remove object from front of queue // Throws NoSuchElementException if empty
public abstract E remove();
// Peek object from front of queue // Throws NoSuchElementException if empty
public abstract E element();
// Check for empty
public abstract boolean isEmpty();
// Returns size
public abstract int size();
// Clears queue
public abstract void clear();
// Various searches
public abstract boolean contains(Object o);
// Override toString
public abstract String toString(); }
Instructions
Inherit the interface provided above.
The Queue is a generic data structure, so declare an LinkedList.
HINT: You should use the first entry or head of the LinkedList as the front of the queue.
HINT: You should use the last entry or tail of the LinkedList as the rear of the queue.
You must not use add(), remove(), or element() in the LinkedList object.
You must implement toString() first, format is the same as LinkedList.toString().
2. Incremental development, so test one method at a time.
Review the TestProgram to see what is tested by each test number, 1 through 7.
Below is the CoolQueue.java:
import java.util.LinkedList;
import java.util.NoSuchElementException;
class CoolQueue implements QueueInterface {
// Underlying data structure
// Default constructor
}
Below is the TestProgram.java:
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Random;
public class TestProgram {
static Random random = new Random();
public static void main(String[] args) {
// Make a Java stack
Queue javaQueue = new LinkedList<>();
// Make a student stack
CoolQueue coolQueue = new CoolQueue<>();
// Which test?
int testNumber = Integer.parseInt(args[0]);
4. case 3: // testElement
System.err.println("testElement: verifying element() method");
pushRandom(javaQueue, 10);
pushRandom(coolQueue, 10);
for (int i = 0; i < 10; i++) {
System.err.printf("javaQueue.element(): %4s, coolQueue.element(): %4s ",
javaQueue.element(), coolQueue.element());
javaQueue.remove();
coolQueue.remove();
}
break;
case 4: // testSize
System.err.println("testSize: verifying size() method");
pushRandom(javaQueue, 23456);
pushRandom(coolQueue, 23456);
for (int i = 0; i < 12123; i++) {
javaQueue.remove();
coolQueue.remove();
}
System.err.printf("javaQueue.size(): %4s, coolQueue.size(): %4s ", javaQueue.size(),
coolQueue.size());
break;
case 5: // testClear
System.err.println("testClear: verifying clear() method");
pushRandom(javaQueue, 23456);
pushRandom(coolQueue, 23456);
javaQueue.clear();
coolQueue.clear();
System.err.printf("javaQueue.size(): %s, coolQueue.size(): %s ", javaQueue.size(),
coolQueue.size());
5. break;
case 6: // testIsEmpty
System.err.printf("javaQueue.isEmpty(): %4s, coolQueue.isEmpty(): %4s ",
javaQueue.isEmpty(), coolQueue.isEmpty());
javaQueue.add("1111");
coolQueue.add("2222");
System.err.printf("javaQueue.isEmpty(): %4s, coolQueue.isEmpty(): %4s ",
javaQueue.isEmpty(), coolQueue.isEmpty());
javaQueue.remove();
coolQueue.remove();
System.err.printf("javaQueue.isEmpty(): %4s, coolQueue.isEmpty(): %4s ",
javaQueue.isEmpty(), coolQueue.isEmpty());
break;
case 7: // testContains
pushRandom(javaQueue, 1234);
pushRandom(coolQueue, 1234);
System.err.println("javaQueue.contains("7449"): " +
javaQueue.contains("7449"));
System.err.println("coolQueue.contains("7449"): " +
coolQueue.contains("7449"));
System.err.println("javaQueue.contains("4444"): " +
javaQueue.contains("4444"));
System.err.println("coolQueue.contains("4444"): " +
coolQueue.contains("4444"));
break;
}
}
// Initialize stack by pushing random data
private static void pushRandom(Queue queue, int number) {
random.setSeed(1234);
for (int i = 0; i < number; i++) {
6. queue.add(Integer.toString(random.nextInt(10000)));
}
}
private static void pushRandom(CoolQueue queue, int number) {
random.setSeed(1234);
for (int i = 0; i < number; i++) {
queue.add(Integer.toString(random.nextInt(10000)));
}
}
}
Thanks! I need solution :)
Solution
import java.util.LinkedList;
import java.util.NoSuchElementException;
class CoolQueue implements QueueInterface {
// Underlying data structure
// Default constructor
}
Below is the TestProgram.java:
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Random;
public class TestProgram {
static Random random = new Random();
public static void main(String[] args) {
7. // Make a Java stack
Queue javaQueue = new LinkedList<>();
// Make a student stack
CoolQueue coolQueue = new CoolQueue<>();
// Which test?
int testNumber = Integer.parseInt(args[0]);
switch (testNumber) {
case 1: // testAdd
System.err.println("testAdd: verifying add() method");
pushRandom(javaQueue, 10);
pushRandom(coolQueue, 10);
System.err.println("Java Queue: " + javaQueue);
System.err.println("Cool Queue: " + coolQueue);
break;
case 2: // testRemove
System.err.println("testRemove: verifying remove() method");
pushRandom(javaQueue, 10);
pushRandom(coolQueue, 10);
// Legal removes
for (int i = 0; i < 10; i++) {
System.err.printf("javaQueue.remove(): %4s, coolQueue.remove(): %4s ",
javaQueue.remove(), coolQueue.remove());
}
// Queue empty
try {
javaQueue.remove();
} catch (NoSuchElementException e) {