//File: FifoList.java //Name: Trai Pham //Date: 02/09/2020 //Course: CSE 8B /**A class that extends OrderedDS class. This class has to implement all methods in the OrderedDS and DataStructure classes and BasicMethods interface This class allows you to input elements starting from left to right */ public class FifoList extends OrderedDS{ //holds the maximum size of integer in the data structure private int maxSize; private int indexAt; //can declare any other varaibles //printed when FifoList is empty private static final String EMPTY_ERROR = "FifoList is empty"; //printed when FifoList reached its Max size private static final String MAX_SIZE_ERROR = "FifoList maximum limit reached"; //must put the @Override keyword above the methods //that are inherited from parent classes: //default Constructor for FifoList class public FifoList(){ this.maxSize = 0; this.array = null; this.indexAt = 0; } //int input for max size of array public FifoList(int maxSize){ if(maxSize < 0){ this.maxSize = 0; this.array = null; } else{ this.maxSize = maxSize; this.array = new int [this.maxSize]; } } //deep copy constructor for Fifo class public FifoList(FifoList q){ if( q == null){ this.maxSize = 0; this.array = null; } else { this.maxSize = q.maxSize; for(int i = 0; i < this.maxSize; i++){ this.array[i] = q.array[i]; } } } //adds an int element to the FifoList public void add(int element){ //Maxsize and null error check if(this.array == null || this.indexAt == this.array.length){ System.out.println(MAX_SIZE_ERROR); return; } else{ this.array[this.indexAt] = element; this.indexAt++; } } //deletes the first element in the list if requirements are satisfied @Override public int delete(){ //empty error check if(this.indexAt == 0|| this.array == null){ System.out.println(EMPTY_ERROR); return -1; } else{ int reserved = this.array[0]; for( int i = 1; i < this.indexAt; i++){ this.array[i-1] = this.array[i]; } indexAt--; return reserved; } } @Override public int peek(){ //when FifoList is empty if(this.indexAt == 0 || this.array == null){ System.out.println(EMPTY_ERROR); return -1; } else{ //returns first element inserted in list return this.array[0]; } } public int size(){ return this.indexAt; } public String toString(){ String output = ""; if(this.indexAt == this.array.length + 1 || this.array == null){ return ""; } StringBuilder a = new StringBuilder(); output = this.array[0] + ""; a.insert(0, output); for(int i = 1; i < this.indexAt; i++){ a.append("-" + this.array[i]); } return a.toString(); } public static void main (String [] args){ FifoList a = new FifoList(5); System.out.println(a.array.length); //gives you length of the array a.add(2); System.out.println(a); a.add(5); System.out.println(a); a.add(0); System.out.println(a); a.add(25); System.out.println(a); a.add(9); System.out.println(a); a.add(10); // System.out.println(a); // a.add(9); System.out.println("Testing add method"); a.delete(); System.out.println(a); a.delete(); System.out.println(a); a.delete(); System.out.println(a); System.out.println("Testing deleting method"); System.out.println(a.peek()); System.out.println("Returning the top number"); // System.out.println("Testing delete method"); System.out.println(a.size()); System.out.println("Counting number of elements"); // a.delete(); // a.delete(); // a.delete(); // System.out.println("Empty Error"); } }