/* Submitted by: Maderal, Kurt Merkell John P. // ICS 20 A // Xavier University // // // // An implementation of an Array-Based Queues */ import java.io.*; public class QueueArray { private BufferedReader SizeReader; private BufferedReader ItemReader; private Object storage[]; private int front,rear,check; private String inp3; private int N; // constructor named Queues to instantiate the size of the array "storage" and other variables public QueueArray() throws IOException { try { System.out.println("\n -------------------- Array-Based Queue --------------------\n"); SizeReader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Input the Size : Array of Objects: "); N = Integer.parseInt(SizeReader.readLine()) + 1; storage = new Object [ N ]; front = 0; rear = 0; check = 0; //-1 if reset instruct(); access(); } catch (Exception e) { System.out.println("\n ERROR.... Pls. TRY AGAIN...\n"); reset(); } } // method instructions public void instruct() { System.out.println("\nCommands for Methods: type \" e \" for "+"\"Enqueue\""+" with your Input..."); System.out.println(" \" d \" for "+"\"Dequeue\""); System.out.println(" \" f \" for "+"\"Front\""); System.out.println(" \" s \" for "+"\"Size\""); System.out.println(" \" y \" for "+"\"IsEmpty?\""); System.out.println(" \" r \" for "+"\"Reset All\""); System.out.println(" \" p \" for "+"\"Print / Display All Inputs\""); System.out.println(" \" i \" for "+"\"Commands / Instructions / HELP\""); System.out.println(" \" x \" for "+"\"Exit Program\"\n\n"); System.out.println(" --------> NOTE : NO SPACES.... <--------\n\n"); } //method access() for starting public void access() throws IOException { // "infinite for loop" so that inputs are ask over and over // if check == -1 then loop terminates and reset method is called for (; ; ) { inputs(); if (check==-1) { break; } } reset(); } //reset method that calls for a new Object of type Queues public void reset() throws IOException { QueueArray q = new QueueArray(); } //method inputs() public void inputs() throws IOException { ItemReader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Type Command: "); inp3 = ItemReader.readLine(); String com3 = (inp3.charAt(0)+""); if ( (com3.equals("e"))||(com3.equals("d"))||(com3.equals("f"))||(com3.equals("s"))||(com3.equals("y"))||(com3.equals("x"))||(com3.equals("r"))||(com3.equals("p"))||(com3.equals("i")) ) { if (com3.equals("e")) { String s3 = ""; for (int i=1; i inputs ( ) // method size : returns the size of inputs that have been added public int size() { return ( N - front + rear ) % N ; } // method IsEmpty : returns " true " if queue is Empty, " false " otherwise public boolean IsEmpty() { return front == rear; } // method enqueue : adding of inputs to the queue public void enqueue(Object item) { if (size()==N-1) { System.out.println(" Error..!!! Queue is Full..."); } else { storage[rear] = item; rear = (rear+1) % N; System.out.println(" Enqueued : "+ item); } } // method dequeue : removing the first element input in the queue public void dequeue() { if (IsEmpty()) { System.out.println(" Error..!!! Queue is Empty..."); } else { Object temp = storage[front]; System.out.println(" Dequeued : "+ temp); storage[front] = null; front = (front + 1 ) % N; } } // method front : return and show the item / element that will be dequeued next public Object front() { if ( IsEmpty() ) { return (new String("Error..!!! Queue is Empty...")); } else return storage[front]; } // method dispayInputs : this method reveals all the items or elements stored in the array public void displayInputs() { String dis=""; if (! IsEmpty() ) { for (int i=0; i> "+dis+"\n"); } else System.out.println(" Error..!!! Queue is Empty..."); } //main application public static void main(String args[]) throws IOException { QueueArray q = new QueueArray(); } }