In: Computer Science
Given the following program(Java); we are asked to do the following
1. Add a loop in the main to enqueue 12 items of your choice.
2. Be sure to implement some form of error checking that lets you know if the loop tries to add too many items to the queue. Error message: "Unexpected overflow"
3. Add a loop to dequeue items and print them on their own line with their location.
Location = ? item = ?
package Khatrijavaarrayqueue;
public class KhatriJavaArrayQueue
{
private Object[] theArray;
private int currentSize;
private int front;
private int back;
static final int DEFAULT_CAPACITY = 10;
public static void main( String[] args)
{
KhatriJAvaArrayQueue q = new
KhatriArrayJavaQueue();
}
// constructing the queue
public KhatriJavaArrayQueue()
{
this(DEFAULT_CAPACITY);
}
// constructing the queue
public KhatriJavaArrayQueue()
{
theArray = new
Object[capacity];
makeEmpty();
}
/*
* Now, let's add our functions
* these functions go below our constructor,
* but still inside the class
*/
/*
* Adding the push() function.
* this function inserts a new item into the
queue.
*/
public void enqueue(Object x) throws
IndexOutOfBoundsException
{
if(isFull())
{
throw new
IndexOutOfBoundException();
}
back = increment(back);
theArray[back] = x;
currentSize++;
}
/*
* Dequeue function; this function returns the least
recently
* inserted item from the queue.
*/
public Object dequeue()
{
if(isEmpty())
{
return
null;
}
currentSize--;
Object frontItem =
theArray[front];
theArray[front] = null;
front = increment(front);
return frontItem;
}
// adding peek() function
// this function returns the copy of the element at
the top
// of the stack to be viewed but not removed.
public long peek()
{
return stackArray[top];
}
// adding isEmpty() function
public boolean isEmpty()
{
return currentSize == 0;
}
// adding makeEmpty() function
public void makeEmpty()
{
currentSize = 0;
front = 0;
back = -1;
}
// adding getFront()
// returns the least recently inserted item in the
queue.
public Object getFront()
{
if(isEmpty())
{
return
null;
}
return theArry[front];
}
//adding isFull() function; returns true if full
public boolean isFull()
{
return currentSize ==
theArray.length;
}
//increment; internal method to increment the
wraparound.
private int increment(int x)
{
if(++x == theArray.length)
{
x = 0;
}
return x;
}
}
I have implemented above program. In main() method add "12" items to the queue but when we try to enter "11"th item in queue it wil show the error message "Unexpected OverFlow!" and then call the dequeue method which remove items from the queue and then print item with its location in main() method.
In the questioned program, you done some mistakes in some methods that I solved and attached below full implemented queue program.
Program:-
package Khatrijavaarrayqueue;
import java.util.Scanner;
public class KhatriJavaArrayQueue
{
private Object[] theArray;
private int currentSize;
private int front;
private int back;
static final int DEFAULT_CAPACITY = 10;
public static void main( String[] args)
{
// create an object of
KhatriJavaArrayOueue Class
KhatriJavaArrayQueue q = new KhatriJavaArrayQueue();
// create an object of Scanner class for user input.
Scanner sc = new Scanner(System.in);
System.out.println("---------- Add 12 Items in the QUEUE
----------\n");
// add 12 items using for loop
for(int i=0; i<12; i++){
// get the user input from the user
System.out.print("Enter item "+(i+1)+" : ");
int num = sc.nextInt();
if(i < KhatriJavaArrayQueue.DEFAULT_CAPACITY){
// add into queue by calling enqueue() method
q.enqueue(num);
}else{
// here queue is full so print the error message
System.err.println("Unexpected OverFlow!");
break;
}
}
System.out.println("\n---------- Dequeue Items from the QUEUE
----------\n");
// now add a loop to dequeue items from queue and print location
and item
while(!q.isEmpty()){
// print location of item and item value
System.out.println("Location = "+q.front+" Item = "+q.dequeue()
);
}
}
// constructing the queue
public KhatriJavaArrayQueue()
{
// intialize default capacity by calling parameterized
constructor
this(DEFAULT_CAPACITY);
}
// constructing the queue
public KhatriJavaArrayQueue(int capacity)
{
theArray = new Object[capacity];
makeEmpty();
}
/*
* Now, let's add our functions
* these functions go below our constructor,
* but still inside the class
*/
/*
* Adding the enqueue() function.
* this function inserts a new item into the queue.
*/
public void enqueue(Object x) throws
IndexOutOfBoundsException
{
if(isFull())
{
throw new IndexOutOfBoundsException();
}
back = increment(back);
theArray[back] = x;
currentSize++;
}
/*
* Dequeue function; this function returns the least recently
* inserted item from the queue.
*/
public Object dequeue()
{
if(isEmpty())
{
return null;
}
currentSize--;
Object frontItem = theArray[front];
theArray[front] = null;
front = increment(front);
return frontItem;
}
// adding peek() function
// this function returns the copy of the element at the front
// of the queue to be viewed but not removed.
public Object peek()
{
// check whether queue is empty or not?
if(isEmpty())
{
// if empty then returns -1
return null;
}
else
return theArray[front];
}
// adding isEmpty() function
public boolean isEmpty()
{
return currentSize == 0;
}
// adding makeEmpty() function
public void makeEmpty()
{
currentSize = 0;
front = 0;
back = -1;
}
// adding getFront()
// returns the least recently inserted item in the queue.
public Object getFront()
{
if(isEmpty())
{
return null;
}
return theArray[front];
}
//adding isFull() function; returns true if full
public boolean isFull()
{
return currentSize == theArray.length;
}
//increment; internal method to increment the wraparound.
private int increment(int x)
{
if(++x == theArray.length)
{
x = 0;
}
return x;
}
}
Output:-
In above ouput, we can see that when user try to enter "11"th item then program will show the error message "Unexpected Overflow!".
I hope you will understand the above program.
Do you feel needful and useful then please upvote me.
Thank you.