import java.awt.Font;

import java.util.Scanner;

import javax.management.RuntimeErrorException;

public class CircleArrayQueueDemo {

Public static void main(String[] args) {public static void main(String[] args) {circlearRayQueue circlearRayQueue = new CirclearRayQueue (4); char key = ' '; Scanner scanner = new Scanner(System.in); boolean loop = true; While (loop) {system.out. println("s(show): show the queue "); System.out.println("e(exit): exiting the program "); System.out.println("a(add): add data to queue "); System.out.println("g(get): fetch data from queue "); System.out.println("h(head): View the data in the queue header "); key = scanner.next().charAt(0); switch (key) { case 's': circleArrayQueue.showQueue(); break; case 'e': circleArrayQueue.showQueue(); break; Case 'a': System.out.println(" "); int value = scanner.nextInt(); circleArrayQueue.addQueue(value); break; case 'g': try { int res = circleArrayQueue.getQueue(); System.out.printf(" fetched data is %d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = circleArrayQueue.headQueue(); System.out.printf(" Queue header data is %d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; default: scanner.close(); loop = false; break; }} System.out.println(" program exits "); }

} // Queue class CirclearRayQueue {

private int maxsize; private int front; private int rear; private int[] arr; // public CirclearRayQueue (int arrMaxSize) {arrMaxSize = arrMaxSize; front = 0; rear = 0; arr = new int[maxsize]; Public Boolean isFull() {return (rear+1)% maxSize == front; } public Boolean isEmpty() {return rear == front; } public void AddQueue (int n) {if(isFull()) {System.out.println(" This queue isFull! "); ); return; } // Arr [rear] = n; / / rear/agricultural products futures (https://www.gendan5.com/cf/af.html) back rear = (rear + 1) % maxsize; } public int getQueue() {if(isEmpty()) {throw new RuntimeException(" Queue isEmpty! "); ); } // int value = arr[front]; // Front = (Front + 1)% maxSize; return value; } public void showQueue() {if(IsEmpty ()) {System.out.println(" The queue isEmpty!"); ); return; } for(int i = front; i < front + size(); i++) { System.out.printf("arr[%d]=%d\n",i % maxsize, arr[i % maxsize]); } public int size() {return (rear + maxsize-front) % maxSize; } public int headQueue() {if(isEmpty()) {throw new RuntimeException();} public int headQueue() {if(isEmpty()) {throw new RuntimeException(); ); } return arr[front]; }

}