Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Revise the code and provide menu options such as enqueue, dequeue, display queue, and many others. package queueconsole; import java.util.Arrays; public class QueueConsole { private

Revise the code and provide menu options such as enqueue, dequeue, display queue, and many others.

package queueconsole;

import java.util.Arrays;

public class QueueConsole {

private String[] queueArray;

private int queueSize;

private int front, rear,numberOfItems = 0;

QueueConsole(int size){

queueSize = size;

queueArray = new String[size];

Arrays.fill(queueArray,"-1");

}

public void insert(String input){

if(numberOfItems <= queueSize)

{

queueArray[rear] = input;

rear++;

numberOfItems++;

System.out.println("PUSH " + input + " was inserted into the queue.");

}

else{

System.out.println("Sorry but the queue is full.");

}

}

//insert elements using priority options (priority queues)

//optional whether use simple insert or priority insert function

public void priorityInsert(String input) {

int i;

if(numberOfItems == 0) {

insert(input);

}

else {

for(i=numberOfItems-1; i>=0; i--){

if(Integer.parseInt(input) > Integer.parseInt(queueArray[i])){

queueArray[i+1] = queueArray[i];

} else break;

}

queueArray[i+1] = input;

rear++;

numberOfItems++;

}

}

public void remove(){

if(numberOfItems > 0) {

System.out.println("REMOVE " + queueArray[front] + " was removed from the queue.");

queueArray[front] = "-1";

front++;

numberOfItems--;

} else

{

System.out.println("Sorry but the queue is empty.");

}

}

public void peek(){

System.out.println("The first element is " +queueArray[front]);

}

public void displayTheQueue(){

for(int i=0; i<45;i++) { System.out.print("=");} System.out.print(" ");

for(int i = 0; i < 10; i++){

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

}

System.out.print("| ");

for(int i=0; i<45;i++) { System.out.print("=");} System.out.print(" ");

}

public static void main(String[] args) {

QueueConsole thequeue = new QueueConsole(10);

//priority insert - priority queues

//thequeue.priorityInsert("10");

//thequeue.priorityInsert("19");

//thequeue.priorityInsert("15");

//thequeue.priorityInsert("11");

thequeue.insert("1");

thequeue.insert("10");

thequeue.insert("20");

thequeue.insert("30");

thequeue.insert("40");

thequeue.priorityInsert("500");

thequeue.priorityInsert("1000");

thequeue.priorityInsert("1500");

thequeue.priorityInsert("2500");

thequeue.displayTheQueue();

thequeue.peek();

thequeue.remove();

thequeue.displayTheQueue();

thequeue.peek();

}

}

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

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

Current position analysis 17.6 $249,600 162,000 1,306,800 156,000

Answered: 1 week ago