Create a new project called simplequeue. The following code shows a simple Java
ID: 3639167 • Letter: C
Question
Create a new project called simplequeue.The following code shows a simple Java implementation of a Node and Queue classes (parts of the code is missing, you are to complete it). =======
Node.java
/**
* class Node
*
*/
public class Node
{
Object dataItem;
Node nextNode;
}
Queue.java
/**
* class Queue
*
*/
public class Queue
{
public Node head;
public Node tail;
/**
* Constructor for objects of class Queue
*/
public Queue()
{
// initialize head and tail references
head = null;
tail = null;
}
/**
* sets all queue entries to null
*
**/
public void destroy()
{
Node temp = new Node();
Node setNull = new Node();
temp = head;
while (temp!=null)
{
setNull = temp;
temp = temp.nextNode;
setNull = null;
}
head = null;
tail = null;
}
/**
* checks whether queue is empty
*/
public boolean isEmpty()
{
return head == null;
}
/**
* checks whether queue is full - not properly
* implemented here
*/
public boolean isFull()
{
return false;
}
/**
* add an item to the queue
*/
public void add(Object o)
{
=======
=======
}
/**
* remove an item by obeying FIFO rule
*/
public Object remove()
{
if (head == null)
return null;
else
{
Node temp = new Node();
temp = head;
head = head.nextNode;
if (head == null)
tail = null;
return temp.dataItem;
}
}
/**
* returns the number of items in the queue
*/
public int size()
{
int count = 0;
for (Nodecurrent=head;current!=null;current=current.nextNode)
count++;
return count;
}
}
Using a Queue
To use the Queue class, you need to know how to write code to call the Queue operations, for example to add data to the Queue.
Remember that the Queue can hold any kind of data.
Controller Class The following class QueueTester.java shows how to use a Queue to hold String objects. Calls to Queue operations are shown in bold.
QueueTester.java
/**
* class QueueTester
*
*/
public class QueueTester
{
private Queue queue;
public QueueTester()
{
queue = new Queue();
}
public QueueTester(Queue queue)
{
this.queue = queue;
}
/**
* add item to queue
*/
public void addString(String str)
{
queue.add(str);
System.out.println("Added new string");
}
/**
* remove item from queue
*/
public void removeString()
{
String result = (String) queue.remove();
if (result!=null)
System.out.println("String is :" + result);
else
System.out.println("Remove was unsuccessful");
}
/**
* check if queue is empty
*/
public void checkIfEmpty()
{
if (queue.isEmpty())
System.out.println("Queue empty");
else
System.out.println("Queue is not empty");
}
/**
* list the strings in queue
*/
public void listStringsInQueue()
{
if (queue.isEmpty())
System.out.println("Queue empty");
else
{
System.out.println("Strings in queue are: ");
System.out.println();
Node node = queue.head;
while (node != null)
{
String item = (String)node.dataItem;
System.out.println(item);
node = node.nextNode;
}
System.out.println();
}
}
}
Create a Driver.java class (it contains main).
1. Create a new instance of Queue called q.
2. Create a new instance of QueueTester called qt and select your Queue instance in the object bench as the parameter in the constructor. This means that you will be testing the Queue you created in the previous step.
3. Call the checkIfEmpty method of your QueueTester.
i. What was the result?
4. Call the addString method of your QueueTester to add the string “The” to the stack.
5. Repeat this to add the following strings:
6. “queue”, “gets”, “longer”
7. What result would you expect if you remove from the Queue?
8. Call the removeString method of your QueueTester and check that you got the correct result.
9. Call the add method of your QueueTester to add the strings “every” and “day” to the queue.
a. What do you expect the contents of the Queue to be now?
10. Inspect your Queue object. You should see references to the head and tail of the Queue.
11. Go through the queue one node at a time(use the Netbeans debugger), using the nextNode reference each time to get from node to node.
a. Do you see the data in the order you expected?
b. How do you know you have reached the last node?
c. Is there another way of getting to the last node?
Explanation / Answer
Queue class Update Add Method /** * add an item to the queue */ public void add(Object o) { Node oldlast = tail; tail = new Node(); tail.dataItem = o; tail.nextNode = null; if (isEmpty()) head = tail; else oldlast.nextNode = tail; } QueueTester....add the following method.. public static void main(String[] args) { QueueTester qt = new QueueTester(); qt.addString("Hello"); qt.addString("world"); qt.listStringsInQueue(); qt.removeString(); qt.listStringsInQueue(); } it works..rest methods in queue should be correct..
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.