In: Computer Science
write the code based on this pseudocode
//Create Class any name (ex: aQ)
//Declaring private integer array empty one ex: name arrQ[]
//Declare private integers front, rear, size and len (example, but
you call any other names)
//Write constructors
Called public and same name of the class (parameters integer
n)
Inside Declare where:
size is equal to n
len is equal to zero
arrQ is equal to new integer[size]
front is equal minus 1
rear is equal minus 1
//Declare function to check if Queue is empty
In this public you should declare boolean (any name you want)
Which return front is equal minus 1
//Declare function to check if queue is full
Called public boolean (any name you want)
which
return front is equal to zero and rear is equal to size minus
1
// Declare function to get the size of the queue
Should be public integer (any name to get size)
which
return len
//Declare function to check the front element of the queue
Should be public integer peek
then
condition
if (call method that you created above to check if is empty)
//then you can write:
throw new NoSuchElementException("Underflow Exception");
then
return name of your array that you declared in first line
[front]
//Declare function to insert an element to the queue
Create public void insert or enque with parameters integer i
then condition
if rear is equal minus 1
front is equal to zero
rear is equal to zero
name of array[rear] is equal to i
//else if rear plus 1 is greater equal to size
//you can write:
throw new IndexOutOfBoundsException("Overflow Exception");
else if rear plus 1 is less then size
name of array[increment rear] is equal to i
increment len
//Declare function to remove front element from the queue
Create public integer remove or deque
then condition
if to call method that you created above to check if is empty
//then you can write:
throw new IndexOutOfBoundsException("Overflow Exception");
// condition else
decrement len
declare variable integer (any name, ex: element) which is equal to
name of array[front]
condition if front is equal to rear
front equal minus 1
rear equal minus 1
condition else
increment front
and
return element
//Declare function to display the queue
Should be public void display
then
sys.out.print Queue:
condition if len is equal to zero
sys.out.print no values
//then just:
return
then create for loop where i equal to front and i is less equal
rear and increment i
then:
sys.out.print name of array[i] + " "
//Scanner scan = new Scanner(System.in);
System.out.println("Enter the Size of Queue ");
int n = scan.nextInt();
/* creating object of class aQ */
aQ q = new aQ(n);
char ch;
do{
System.out.println("\n Queue Menu");
System.out.println("1. Insert Value”);
System.out.println("2. Remove Value”);
System.out.println("3. Peek");
System.out.println("4. Check if is empty");
System.out.println("5. Check if is full");
System.out.println("6. Check the size of Queue”);
int choice = scan.nextInt();
switch (choice)
for java
for java. create code from this pseudo
step 1. reate Class any name (ex: aQ)
step 2. Declaring private integer array empty one ex:
name arrQ
step 3. Declare private integers front, rear, size and len
(example, but you call any other names)
step 4Write constructors
Called public and same name of the class (parameters
integer n)
Inside Declare where:
size is equal to n
len is equal to zero
arrQ is equal to new integer[size]
front is equal minus 1
rear is equal minus 1
step 5. Declare function to check if Queue is empty
In this public you should declare boolean (any name
you want)
Which return front is equal minus 1
step 6. Declare function to check if queue is full
Called public boolean (any name you want)
which
return front is equal to zero and rear is equal to
size minus 1
step 7. Declare function to get the size of the queue
Should be public integer (any name to get size)
which
return len
step 8. Declare function to check the front element of the
queue
Should be public integer peek
then
condition
if (call method that you created above to check if is
empty)
//then you can write:
throw new NoSuchElementException("Underflow
Exception");
then
return name of your array that you declared in first
line [front]
step 9. Declare function to insert an element to the queue
Create public void insert or enque with parameters integer i
then condition
if rear is equal minus 1
front is equal to zero
rear is equal to zero
name of array[rear] is equal to i
step 10. else if rear plus 1 is greater equal to size
you can write:
throw new IndexOutOfBoundsException("Overflow Exception");
else if rear plus 1 is less then size
name of array[increment rear] is equal to i
increment len
step 11. Declare function to remove front element from the
queue
Create public integer remove or deque
then condition
if to call method that you created above to check if
is empty
then you can write:
throw new IndexOutOfBoundsException("Overflow
Exception");
step 12. condition else
decrement len
declare variable integer (any name, ex: element)
which is equal to name
of
array[front]
condition if front is equal to rear
front equal minus 1
rear equal minus 1
condition else
increment front
and
return element
step 13. Declare function to display the queue
Should be public void display
then
sys.out.print Queue:
condition if len is equal to zero
sys.out.print no values
step 14. then just:
return
then create for loop where i equal to front and i is less equal
rear and increment i
then:
sys.out.print name of array[i] + " "
step 15. Scanner scan = new Scanner(System.in);
System.out.println("Enter the Size of Queue ");
int n = scan.nextInt();
/* creating object of class aQ */
aQ q = new aQ(n);
char ch;
do{
System.out.println("\n Queue Menu");
System.out.println("1. Insert Value”);
System.out.println("2. Remove Value”);
System.out.println("3. Peek");
System.out.println("4. Check if is empty");
System.out.println("5. Check if is full");
System.out.println("6. Check the size of Queue”);
int choice = scan.nextInt();
switch (choice)
Q.java:
Driver.java:
raw_code:
Q.java:
// importing NoSuchElementException
import java.util.NoSuchElementException;
// Q class
public class Q{
// private data variable arrQ is an array of integers
private int arrQ[];
// private data variables of type int
private int front, rear, size, len;
// Q class constructor
public Q(int n){
this.size = n;
this.len = 0;
this.arrQ = new int[size];
this.front = -1;
this.rear = -1;
}
// isEmpty method, checks whether a Q object is empty
public boolean isEmpty(){
if (front == -1)
return true;
else
return false;
}
// isFull method, checks whether a Q object is full
public boolean isFull(){
if (front == 0 && rear == -1)
return true;
else
return false;
}
// getSize() function to get the size of Queue
public int getSize(){
return len;
}
// peek() to get teh first element of Queue
public int peek(){
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
else
return arrQ[front];
}
//insert() function to insert an element to the Queue
public void insert(int i){
if (rear == -1){
front = 0;
rear = 0;
arrQ[rear] = i;
len++;
}
else{
if (rear+1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else
if (rear+1 < size){
arrQ[++rear] = i;
len++;
}
}
}
// remover function to remover front element from the Queue
public int remove(){
if (isEmpty())
throw new IndexOutOfBoundsException("Overflow Exception");
else{
len--;
int element = 0;
if (front == rear){
front = -1;
rear = -1;
}
else{
element = arrQ[front];
front++;
}
return element;
}
}
// display() function to display Queue
public void display(){
System.out.print("Queue: ");
if (len == 0)
System.out.print("no values\n");
else{
for(int i = front; i <= rear; i++)
System.out.print(arrQ[i] + " ");
}
}
}
Driver.java:
// import Scanner class
import java.util.Scanner;
public class Driver{
// main method
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Size of Queue");
int n = scan.nextInt();
Q q = new Q(n);
char ch;
do{
System.out.println("\nQueue Menu");
System.out.println("1. Insert Value");
System.out.println("2. Remove Value");
System.out.println("3. Peek");
System.out.println("4. Check if is empty");
System.out.println("5. Check if is full");
System.out.println("6. Check the size of Queue");
int choice = scan.nextInt();
// switch statement to check user option
switch(choice){
// user choice is 1
case (1): {
int num = 0;
// prompting number to add
System.out.print("Enter a number: ");
num = scan.nextInt();
// calling insert() method with num as parameter
q.insert(num);
break;
}
// user choice is 2, calling remove method and printing return
value
case (2): {
System.out.println(q.remove());
break;
}
// user choice is 3, calling peek method and printing return
value
case (3): {
System.out.println(q.peek());
break;
}
// user choice is 4, calling isEmpty method and printing return
value
case (4): {
System.out.println(q.isEmpty());
break;
}
// user choice is 5, calling isFull method and printing return
value
case (5): {
System.out.println(q.isFull());
break;
}
// user choice is 6, calling getSize method and printing return
value
case (6): {
System.out.println(q.getSize());
break;
}
}
// prompting user to quit or not
System.out.print("Enter q to quit: ");
ch = scan.next().charAt(0); // taking input and storing in ch
}
while(ch != 'q');
// calling display method for debugging purpose
q.display();
}
}