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
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// LifoList.java
public class LifoList {
// array to store elements
private int array[];
// max size
private int maxSize;
// current count of numbers in array
private int count;
// constants for error messages
private static final String MAX_SIZE_ERROR = "ERROR! LifoList is full!";
private static final String EMPTY_ERROR = "ERROR! LifoList is empty!";
// default constructor for LifoList class which will initialize your class
// variables maxSize to 0 and the int array to null.
public LifoList() {
maxSize = 0;
array = null;
count = 0;
}
// constructor for LifoList class which takes the int input maxSize to
// initialize the size of the array.
public LifoList(int maxSize) {
// using default values if maxSize is invalid
if (maxSize < 0) {
this.maxSize = 0;
array = null;
count = 0;
} else {
this.maxSize = maxSize;
array = new int[maxSize];
count = 0;
}
}
// deep copy constructor for LifoList class which takes the LifoList object
// s as an input and creates a deep copy of the LifoList
public LifoList(LifoList s) {
// using default values if maxSize is invalid
if (s == null) {
maxSize = 0;
array = null;
count = 0;
} else {
// otherwise copying values from s to this list
maxSize = s.maxSize;
array = new int[s.maxSize];
for (int i = 0; i < count; i++) {
array[i] = s.array[i];
}
count = s.count;
}
}
// method to add an int element to the LifoList
public void add(int element) {
// printing error if full
if (count == maxSize || array == null) {
System.out.println(MAX_SIZE_ERROR);
} else {
// adding to array at count index and updating count
array[count] = element;
count++;
}
}
// method to delete and return the top element
public int delete() {
// printing error and returning -1 if empty
if (count == 0) {
System.out.println(EMPTY_ERROR);
return -1;
}
// decrementing count and returning element at index count (previously
// count-1)
count--;
return array[count];
}
// method that returns the top element.
public int peek() {
// printing error and returning -1 if empty
if (count == 0) {
System.out.println(EMPTY_ERROR);
return -1;
}
// returning element at index count-1
return array[count - 1];
}
// returns the size
public int size() {
return count;
}
// return string where elements are in LIFO order
public String toString() {
String s = "";
for (int i = count - 1; i >= 0; i--) {
s += array[i];
// appending a '-' if this is not the first element
if (i != 0) {
s += "-";
}
}
return s;
}
// main method for testing
public static void main(String[] args) {
// Creating LifoList of maxSize 5 and adding 2, then 7, and then 4:
LifoList list = new LifoList(5);
list.add(2);
list.add(7);
list.add(4);
// printing list
System.out.println("list: " + list);
// adding 5
list.add(5);
// printing list
System.out.println("list: " + list);
// peeking top element
System.out.println("peek: " + list.peek());
// removing top
System.out.println("delete: " + list.delete());
// printing list
System.out.println("list: " + list);
}
}
/*OUTPUT*/
list: 4-7-2
list: 5-4-7-2
peek: 5
delete: 5
list: 4-7-2
Create a LIFO class with following methods in java - public LifoList() The default constructor for...
In Java. What would the methods of this class look like?
StackADT.java
public interface StackADT<T>
{
/** Adds one element to the top of this stack.
* @param element element to be pushed onto stack
*/
public void push (T element);
/** Removes and returns the top element from this stack.
* @return T element removed from the top of the stack
*/
public T pop();
/** Returns without removing the top element of this
stack.
* @return T...
create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...
In Java. How would this method look?
LinkedBinaryTree.java
import java.util.Iterator;
public class LinkedBinaryTree implements BinaryTreeADT {
private BinaryTreeNode root;
/**
* Creates an empty binary tree.
*/
public LinkedBinaryTree() {
root = null;
}
/**
* Creates a binary tree from an existing root.
*/
public LinkedBinaryTree(BinaryTreeNode root) {
this.root = root;
}
/**
* Creates a binary tree with the specified element...
JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings in the stack b. A private data field size to store the number of strings in the stack c. A constructor to construct an empty stack with a default capacity of 4 d. A constructor to construct an empty stack with a specified capacity e. A method empty() that returns true if the stack is empty f. A method push(String value) that stores value...
In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...
JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...
Modify listlink.java program (non generic) by adding the
following methods:
public void insertsorted(x); // Inert x in a sorted
list.
public void deletex(x); //Search for x in the sorted list,
if found, delete it from the sorted list.
Assume you have a data file p2.txt with the following
contents:
8
4 15 23 12 36 5 36 42
3
5 14 4
and your java program is in xxxxx.java file, where xxxxx is
the first 5 characters of your last...
Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed to the constructor Methods: Accessors int getID( ) String getName( ) Class Roster: This class will implement the functionality of all roster for school. Instance Variables a final int MAX_NUM representing the maximum number of students allowed on the roster an ArrayList storing students Constructors a default constructor should initialize the list to empty strings a single parameter constructor that takes an ArrayList<Student> Both...