Question

My Question is: I have to modify this program, even a small modification is fine. Can...

My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced.

import java.util.*;

class arrayQueue
{
protected int Queue[];
protected int front, rear, size, len;


public arrayQueue(int n)
{
size = n;
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}

public boolean isEmpty()
{
return front == -1;
}

public boolean isFull()
{
return front == 0 && rear ==size -1;
}

public int getSize()
{
return len;
}

public int peek()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exeption");
return Queue[front];
}

public void insert(int i)
{
if( rear ==-1)
{
front= 0;
rear=0;
Queue[rear] = i;
}
  
else if(rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else if( rear + 1 < size)
Queue[++rear] = i;
len++;
}

public int remove()
{
if (isEmpty())
throw new NoSuchElementException("underflow Exception");
else
{
len--;
int ele = Queue[front];
if(front ==rear)
{
front = -1;
rear = -1;
}
  
else
front++;
return ele;
}
}

public void display()
{
System.out.print("\nQueue=");
if(len == 0)
{
System.out.print("Empty\n");
return;
}
for(int i=front; i<=rear;i++)
System.out.print(Queue[i]+"");
System.out.println();
}
}


public class QueueImplementation
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

System.out.println("array queue Test \n");
System.out.println("enter size of integer queue");
int n = scan.nextInt();
arrayQueue q = new arrayQueue(n);
char ch;
do
{
System.out.println("\nQueue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1:
System.out.println("Enter integer element to insert");
try
{
q.insert(scan.nextInt());
}
catch(Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 2:
try
{
System.out.println("Removed Element =" + q.remove());
}
catch(Exception e)
{
System.out.println("Error:" + e.getMessage());
}
break;
case 3:
try
{
System.out.println("Peek element =" + q.peek());
}
catch(Exception e)
{
System.out.println("Error:" + e.getMessage());
}
break;
case 4:
System.out.println("Empty Status =" + q.isEmpty());
break;
case 5:
System.out.println("Full Status =" + q.isFull());
break;
case 6:
System.out.println("Size=" + q.getSize());
break;
default: System.out.println("wrong entry\n");
break;
}

q.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
}
while (ch=='Y' || ch== 'y');
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class arrayQueue {
   protected int Queue[];
   protected int front, rear, size, len;

   public arrayQueue(int n) {
       size = n;
       len = 0;
       Queue = new int[size];
       front = -1;
       rear = -1;
   }

   public boolean isEmpty() {
      if(front==-1)
           return true;
       else
           return false;

   }

   public boolean isFull() {
       if(front == 0 && rear == size - 1)
           return true;
       else
           return false;

   }

   public int getSize() {
       return len;
   }

   public int peek() {
       if (isEmpty())
           throw new NoSuchElementException("Underflow Exeption");
       return Queue[front];
   }

   public void insert(int i) {
       if (rear == -1) {
           front = 0;
           rear = 0;
           Queue[rear] = i;
       }

       else if (rear + 1 >= size){
           throw new IndexOutOfBoundsException("Overflow Exception");
       }

       else if (rear + 1 < size){
           Queue[++rear] = i;
       }
       len++;
   }

   public int remove() {
       if (isEmpty()){
           throw new NoSuchElementException("underflow Exception");
       }
       else {
           len--;
           int ele = Queue[front];
           if (front == rear) {
               front = -1;
               rear = -1;
           }

           else{
               front++;
           }
           return ele;
       }
   }

   public void display() {
       System.out.print("\nQueue=");
       if (len == 0) {
           System.out.print("Empty\n");
           return;
       }
       for (int i = front; i <= rear; i++)
           System.out.print(Queue[i] + "");
       System.out.println();
   }
}

public class QueueImplementation {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);

       System.out.println("array queue Test \n");
       System.out.println("enter size of integer queue");
       int n = scan.nextInt();
       arrayQueue q = new arrayQueue(n);
       char ch;
       do {
           System.out.println("\nQueue Operations");
           System.out.println("1. insert");
           System.out.println("2. remove");
           System.out.println("3. peek");
           System.out.println("4. check empty");
           System.out.println("5. check full");
           System.out.println("6. size");
           int choice = scan.nextInt();
           switch (choice) {
           case 1:
               System.out.println("Enter integer element to insert");
               try {
                   q.insert(scan.nextInt());
               } catch (Exception e) {
                   System.out.println("Error : " + e.getMessage());
               }
               break;
           case 2:
               try {
                   System.out.println("Removed Element =" + q.remove());
               } catch (Exception e) {
                   System.out.println("Error:" + e.getMessage());
               }
               break;
           case 3:
               try {
                   System.out.println("Peek element =" + q.peek());
               } catch (Exception e) {
                   System.out.println("Error:" + e.getMessage());
               }
               break;
           case 4:
               System.out.println("Empty Status =" + q.isEmpty());
               break;
           case 5:
               System.out.println("Full Status =" + q.isFull());
               break;
           case 6:
               System.out.println("Size=" + q.getSize());
               break;
           default:
               System.out.println("wrong entry\n");
               break;
           }

           q.display();
           System.out.println("\nDo you want to continue (Type y or n) \n");
           ch = scan.next().charAt(0);
       } while (ch == 'Y' || ch == 'y');
   }
}

Note: Changes are highlighted in bold

Add a comment
Know the answer?
Add Answer to:
My Question is: I have to modify this program, even a small modification is fine. Can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the...

    Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the ADT -----ArrayQueue.java------- public class ArrayQueue {    private static final int INITIAL_CAPACITY = 2; // to permit easier testing    private Object[] contents;    private int front, rear;       /**    * Create an empty queue with an initial capacity.    */    public ArrayQueue() {        contents = new Object[INITIAL_CAPACITY];    }       /**    * Add an element to...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • 2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority...

    2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority queue using bottom-up algorithm. The expected run time should be O(n), where n is the total number of data. BubbleDown method is provided. You may test it in this minHeap public class MinHeapPriorityQueue<E extends Comparable<? super E>>{ private E data[]; private int size; public MinHeapPriorityQueue(){ this(100); } public MinHeapPriorityQueue(int cap){ size = 0; data = (E[]) new Comparable[cap]; } public MinHeapPriorityQueue(int[] a){ } public...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • ArrayQueue

    Implement the ArrayQueue classIn the ‘Queues’ lecture, review the ‘Introduce next lab’ section.  See here that we can use a circular array to implement the queue data structure.  You must write a class named ArrayQueue that does this.ArrayQueue will be a generic class, that implements our generic QueueInterface interface.  This demonstrates the Java interface feature, where we have already implemented queue dynamically, using the LinkedQueue class covered during the lecture.Many classes are given to youDownload and unzip the Circular array project from Canvas, ‘Queues’ module, Example programs.  Open the project in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT