Question
Create a first in first out order class in java with following methods. public FifoList() The default constructor for the FifoList class, which will initialize
Create a first in first out order class in java with following methods.
public FifoList()
The default constructor for the FifoList class, which will initialize maxSize to 0 and the int array to null.
- public FifoList(int maxSize)
The constructor for the FifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new FifoList(5) should create a FifoList whose maximum size is 5. If the parameters passed are invalid (negative numbers) then initialize your class variables maxSize to 0 and the integer array to null.
public FifoList(FifoList q)
The deep copy constructor for the FifoList class, which takes the FifoList object q as an input and creates a deep copy of the FifoList. If the parameters passed are invalid (null object), then initialize your class variables maxSize to 0 and the integer array to null.
- public void add(int element)
A method to add an int element to the FifoList. If FifoList is already full or FifoList is null, print the contents of the MAX_SIZE_ERROR string and return.
public int delete()
A method to delete the top element. Since elements in FifoList are maintained in FIFO order, this method should delete the first element that was inserted into the FifoList and return it. If FifoList is empty or FifoList is null, print the contents of EMPTY_ERROR and return -1.
- public int peek()
A method that returns the top element. Since elements in FifoList are maintained in FIFO order, this method should return the first element that was inserted into the FifoList. If FifoList is empty or FifoList is null, print the contents of EMPTY_ERROR and return -1.
public int size()
A method that returns the size of the FifoList based on how many elements have been added.
public String toString()
A method to print the FifoList as a string in the specified format. Since elements are stored in FIFO order, this method should return string where elements are in FIFO order. If FifoList is empty or FifoList is null, this method should return an empty string. For example:
if the FifoList has elements 2,5,4 (in the order they are inserted), it should return: 2-5-4 If 6 is added to the FifoList and if the toString() method is called, it should return: 2-5-4-6
Tests:
Creating FifoList of maxSize 5 and adding 2, then 7, and then 4: 2, 7, 4 Add 5 to the FifoList: 2, 7, 4, 5 Peek operation (returns the top element): Since FifoList maintains elements in FIFO order, the top element is 2. Hence, returns 2. Delete operation (deletes the top element): 7, 4, 5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started