Question: * I just need MyStack.java and Main.java * * * MyStack . java * import java.util.NoSuchElementException; public class MyStack implements IStack { / / add
I just need MyStack.java and Main.java
MyStackjava
import java.util.NoSuchElementException;
public class MyStack implements IStack
add any necessary variables here
@Override
public void pushObject item
@Override
public Object pop
@Override
public Object peek
@Override
public int indexOfObject item
@Override
public int size
@Override
public boolean isEmpty
add any necessary methods or classes below
IStackjava
A stack is a last in first out LIFO data structure.
New items are pushed onto the top of the list.
Items are popped from the top of the list.
public interface IStack
Pushes an item onto the top of this stack.
public void pushObject item;
Removes the object at the top of this stack and returns that object as the value of this function.
@return the item at the top of the stack or throws a NoSuchElementException is the stack is empty
public Object pop;
Looks at the object at the top of this stack without removing it from the stack.
@return the item at the top of the stack or throws a NoSuchElementException is the stack is empty
public Object peek;
searches the stack for an item.
@return the zerobased index of the item in the stack; returns if the item is not in the stack.
public int indexOfObject item;
A count of the number of items in the stack.
@return A count of the number of items in the stack.
public int size;
Tests if this stack is empty.
@return true if the stack is empty
public boolean isEmpty;
Mainjava
you may use this file to write and run code to test your code
public class Main
public static void mainString args
Structure of the Fields
While there are no required fields for your MyStack class, you will need to decide what fields to implement. This decision will be largely based on your choice to implement this MyStack as either an array list or a linked list.
Structure of the Methods
As described by the UML Class Diagram above, your MyStack class must implement the following methods:
a public method named push that takes an Object argument and returns nothing
a public method named pop that takes no arguments and returns an Object
a public method named peek that takes no arguments and returns an Object
a public method named indexOf that takes an Object argument and returns an int
a public method named size that takes no arguments and returns an int
a public method named isEmpty that takes no arguments and returns an boolean
Note that:
these methods are declared in the IStack interface. You will be implementing these methods in this MyStack concrete class.
Additional Information
MyStack
This concrete class will store its elements in either an internal array list or linked list. All such implementation details must be contained in your myStack.java file. You may add any additional fields, methods, and inner classes that you will need to achieve this.
push method
Add a new item to top of the stack. For example: given the stack where the value is on the top and an instruction to push the result would be this with the value now on the top of the stack.
pop method
Remove and return the item currently on the top of the stack. For example: given the stack where the value is on the top and an instruction to pop the stack would now look like this and the value would be returned.
Throws a NoSuchElementException if the stack is currently empty when this method is called.
peek method
Return but do not remove the item currently on the top of the stack. For example: given the stack where the value is on the top and an instruction to peek the stack would still look like this and the value would be returned.
Throws a NoSuchElementException if the stack is currently empty when this method is called.
indexOf method
Return the zerobased number of elements from the front or top of the collection where the specified item is first found. Returns if the item is not found in the collection. For example: given the stack where the value is on the top and the instruction indexOf the value would be returned because the value was found at index element below the top in the stack. For another example: given the stack where the value is on the top and the instruction indexOf the value would be returned because the value is not found in the stack
size method
Returns the number of elements currently stored in the stack.
isEmpty method
Returns true if there are currently no items stored in this stack, otherwise returns false.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
