In: Computer Science
Write the Java program:
In this assignment, you will create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter).
The Queue3503 class will contain:
a. An int[] data filed named elements to store the int
values in the queue.
b. An int data field named size that stores the number of
elements in the queue.
c. A no-arg constructor that creates a Queue object with default
capacity 0.
d. A constructor that takes an int argument representing the
capacity of the queue.
e. A method with signature enqueue(int v) that adds the
int element v into the queue.
f. A method with signature dequeue() that removes and
returns the first element of the
queue.
g. A method with signature empty() that returns true if
the queue is empty.
h. A method with signature getSize() that returns the size
of the queue (return type is hence
int)).
The queue class you develop should be tested using the following steps: (In other words, your program named Main will consist of the following)
a. Start your queue with an initial capacity of 8.
b. When the dequeue() method is called, an element from the queue
at the beginning of the queue must be removed.
c. The main() method in your Main class should consist of
statements to:
i. Create a queue object;
ii. Call enqueue() to insert twenty integers (taken from
the user) into the queue.
iii. After the above task is completed, include a for-loop that
will print out the contents of the queue.
d. After printing the queue filled with twenty integers,
call dequeue() repeatedly to remove the beginning element of the
queue.
e. Print the contents of the queue after removing every fifth
number.
f. For your reference, the execution of the Main program is shown
below. User inputs for populating the Queue is shown in the first
line. Next, your program outputs are shown.
*Make sure the code can run in Repl.it*
Points to think about
*Make sure the code can run in Repl.it*
Sample Run
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Initial content: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
After removing 5 elements: 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
After removing 5 elements: 11 12 13 14 15 16 17 18 19 20
After removing 5 elements: 16 17 18 19 20
import java.util.*;
/* Class queue3503 */
class queue3503
{
protected int Queue[] ;
protected int front, rear, size, getsize;
/* Constructor */
public queue3503(int n)
{
size = 8;
getsize = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
/* Function to check if queue is empty */
public boolean empty()
{
return front == -1;
}
/* Function to check if queue is full */
public boolean isFull()
{
return front==0 && rear == size -1 ;
}
/* Function to get the size of the queue */
public int getSize()
{
return getsize ;
}
/* Function to check the front element of the queue */
public int peek()
{
if (empty())
throw new NoSuchElementException("Underflow Exception");
return Queue[front];
}
/* Function to enqueue an element to the queue */
public void enqueue(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;
getsize++ ;
}
/* Function to dequeue front element from the queue */
public int dequeue()
{
if (empty())
throw new NoSuchElementException("Underflow Exception");
else
{
getsize-- ;
int ele = Queue[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
return ele;
}
}
/* Function to display the status of the queue */
public void display()
{
System.out.print("\nQueue = ");
if (getsize == 0)
{
System.out.print("Empty\n");
return ;
}
for (int i = front; i <= rear; i++)
System.out.print(Queue[i]+" ");
System.out.println();
}
}
/* Class QueueImplement */
public class QueueImplement
{
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();
/* creating object of class queue3503 */
queue3503 q = new queue3503(n);
/* Perform Queue Operations */
char ch;
do{
System.out.println("\nQueue Operations");
System.out.println("1. enqueue");
System.out.println("2. dequeue");
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 enqueue");
try
{
q.enqueue( scan.nextInt() );
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 2 :
try
{
System.out.println("dequeued Element = "+q.dequeue());
}
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.empty());
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;
}
/* display Queue */
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');
}
}
run this code you will get your desired output
as you need to change the count of no removed 5 you have to just add a count*5 to remove as follows
Queue<string> queueCopy = new
Queue<string>(numbers.ToArray());
string[] array = new string[numbers.Count * 5];
numbers.CopyTo(array, numbers.Count);