Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Revise the attached code Queue.java & QueueApp.java This is the code I demoed during lecture time. You know there are some running errors even

Java

Revise the attached code Queue.java & QueueApp.java

This is the code I demoed during lecture time. You know there are some running errors even though no syntax errors are popped up. The errors that you need to fix is:

1) theQueue declared in line 5 of QueueApp.javacan hold 5 items. However, line 10-15 inserts 6 items: (10, 20, 30, 40, 50, 60). After running the code, you will find that (20, 30, 40, 50, 60) are the items in the queue, which is clearly wrong. Please fix it: make sure that theQueue can only hold 5 items, and display the correct items.

The Code

// Queue.java // demonstrates queue // to run this program: C>java QueueApp //////////////////////////////////////////////////////////////// public class Queue { private int maxSize; private long[] queArray; private int front; private int rear; private int nItems; //-------------------------------------------------------------- public Queue(int s) // constructor { maxSize = s; queArray = new long[maxSize]; front = 0; rear = -1; nItems = 0; } //-------------------------------------------------------------- public void insert(long j) // put item at rear of queue { if(rear == maxSize-1) // deal with wraparound rear = -1; queArray[++rear] = j; // increment rear and insert nItems++; // one more item } //-------------------------------------------------------------- public long remove() // take item from front of queue { long temp = queArray[front++]; // get value and incr front if(front == maxSize) // deal with wraparound front = 0; nItems--; // one less item return temp; } //-------------------------------------------------------------- public long peekFront() // peek at front of queue { return queArray[front]; } //-------------------------------------------------------------- public boolean isEmpty() // true if queue is empty { return (nItems==0); } //-------------------------------------------------------------- public boolean isFull() // true if queue is full { return (nItems==maxSize); } //-------------------------------------------------------------- public int size() // number of items in queue { return nItems; } //-------------------------------------------------------------- public void displayQueue() //display all items in the queue from the front to the rear. { System.out.print("Queue has items: "); } //-------------------------------------------------------------- } // end class Queue ============================================================

public class QueueApp { public static void main(String[] args) { Queue theQueue = new Queue(5); // queue holds 5 items

theQueue.remove(); System.out.println("The first item is "+ theQueue.peekFront()); theQueue.insert(10); // insert 4 items theQueue.insert(20); theQueue.insert(30); theQueue.insert(40); theQueue.insert(50); theQueue.insert(60);

System.out.print("The queue has items: "); while( !theQueue.isEmpty() ) // remove and display { // all items long n = theQueue.remove(); // (40, 50, 60, 70, 80) System.out.print(n); System.out.print(" "); } System.out.println(""); } // end main() } // end class QueueApp ////////////////////////////////////////////////////////////////

2) Please revise the code to make sure that when the queue is full, you cannot insert new items; and when the queue is empty, you cannot delete any items.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions

Question

2. (1 point) Given AABC, tan A b b

Answered: 1 week ago

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago