Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize

Create a LIFO class with following methods in java

- public LifoList()

The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null.

public LifoList(int maxSize)

The constructor for LifoList 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 LifoList(5) should create a LifoList whose maximum size is 5. If the parameters passed are invalid (negative numbers) then initialize your class variables maxSize to 0 and the int array to null.

public LifoList(LifoList s)

The deep copy constructor for LifoList class which takes the LifoList object s as an input and creates a deep copy of the LifoList. 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 LifoList. If LifoList is already full or LifoList is null, print the contents of MAX_SIZE_ERROR string and return.

public int delete()

A method to delete the top element. Since elements in LifoList are maintained in LIFO order, this method should delete the latest element that was inserted into the LifoList and return it. If LifoList is empty or LifoList is null, print the contents of EMPTY_ERROR and return -1.

public int peek()

A method that returns the top element. Since elements in LifoList are maintained in LIFO order, this method should return the latest element that was inserted into the LifoList. If LifoList is empty or LifoList is null, print the contents of EMPTY_ERROR and return -1.

public int size()

A method that returns the size of the LifoList based on how many elements have been added.

public String toString()

A method to print the LifoList as a string in the specified format. Since elements are stored in LIFO order, this method should return string where elements are in LIFO order. If LifoList is empty or LifoList is null, this method should return an empty string. For example:

if the LifoList has elements 2, 5, 4 (in the order they are inserted), it should return: 4-5-2 If 6 is added to the LifoList and if toString() method is called, it should return: 6-4-5-2

Tests:

Creating LifoList of maxSize 5 and adding 2, then 7, and then 4: 4, 7, 2 Add 5 to the LifoList: 5, 4, 7, 2 Peek operation (returns the top element): Since LifoList maintains elements in LIFO, the top element is 5. Hence, returns 5. Delete operation (deletes the top element): 4, 7, 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions