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 Modify the existing (methods already in the file may be modified but not deleted) the...

    JAVA Modify the existing (methods already in the file may be modified but not deleted) the code to a MaxHeap and write a driver class to do the following: 1. Insert item into a Heap 2. Delete Max from the Heap 3. Display the Heap array 4. Convert an array to Heap 1,2 & 3 must all work on the same heap array. Option 4 is independent and must take in input from the user of an integer array and...

  • 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();...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

  • 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...

  • Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the...

    Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the contents of the queue. Note that this does not mean simply displaying the contents of the underlying array. You should show the queue contents from the first item inserted to the last, without indicating to the viewer whether the sequence is broken by wrapping around the end of the array. Be careful that one item and no items display properly, no matter where front...

  • Help me solve this in C++ Rewrite the code to use array instead of linked lists....

    Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...

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