Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java-- All methods attempted for data structure that are needed and work corr

ID: 3889772 • Letter: I

Question

In Java--
All methods attempted for data structure that are needed and work correctly per instructions array
driver class included and more added to driver
all necessary attributes included with provate access modifier
header comments and javadoc

Implement a queue using an array in class. Make your queue size 5.

Queue() creates an empty queue, queue is new and empty.
enqueue(item) adds a new item to the queue, queue is modified.
dequeue() removes and returns an item, queue is modified.
isEmpty() returns a boolean and tests for an empty queue, queue is not modified.
size() returns the int size of the queue, queue is not modified
print() prints the queue from front to rear, queue is not modified.
peek() prints the front element, queue is not modified.
Driver should print the results of the following
enqueue("dog")
enqueue("cat")
enqueue("mouse")
enqueue("pig")
enqueue("bird")
size()
enqueue("puppy")
size()
dequeue()
dequeue()
size()
dequeue()
peek()
dequeue()
dequeue()
size()
isEmpty()

Explanation / Answer

PROGRAM CODE:

Queue.java

package queue;

public class Queue {

private String data[];

private int size;

private static int Capacity = 5;

private int current;

public Queue() {

size = 1;

data = new String[Capacity];

current = 1;

}

public void enqueue(String newData)

{

if(size<4)

data[size++] = newData;

else if(data[0] == null)

{

data[0] = newData;

size++;

}

else System.out.println("Queue is full");

}

public String dequeue()

{

if(isEmpty())

{

System.out.println("Queue is Empty");

return null;

}

String removedData = data[current];

data[current] = null;

current++;

if(current == Capacity-1)

current = 0;

size--;

return removedData;

}

public boolean isEmpty(){

return size==0;

}

public int size()

{

return size;

}

public void print()

{

for(int i=1; i<size; i++)

{

System.out.print(data[i] + " ");

}

if(data[0] != null)

System.out.print(data[0]);

System.out.println();

}

public void peek()

{

if(!isEmpty())

{

System.out.println(data[current]);

}

}

}

QueueDriver.java

package queue;

public class QueueDriver {

public static void main(String[] args) {

Queue queue = new Queue();

queue.enqueue("dog");

queue.enqueue("cat");

queue.enqueue("mouse");

queue.enqueue("pig");

queue.enqueue("bird");

System.out.println("Size: " + queue.size());

queue.enqueue("puppy");

System.out.println("Size: " + queue.size());

queue.dequeue();

queue.dequeue();

System.out.println("Size: " + queue.size());

queue.dequeue();

System.out.println("Size: " + queue.size());

queue.peek();

queue.dequeue();

queue.dequeue();

System.out.println("Size: " + queue.size());

if(queue.isEmpty())

System.out.println("Queue is empty");

else System.out.println("Queue is not empty");

}

}

OUTPUT:

Queue is full
Size: 5
Queue is full
Size: 5
Size: 3
Size: 2
pig
Size: 0
Queue is empty

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote