Question
COMPLETE THE FOLLOWING METHODS EVERYTHING ELSE IS DONE. search, contains, lastIndex, firstIndex DO NOT USE BUILT IN METHODS MUST USE IF STATEMENTS ECT..... import java.util.ArrayList;
COMPLETE THE FOLLOWING METHODS EVERYTHING ELSE IS DONE.
search, contains, lastIndex, firstIndex
DO NOT USE BUILT IN METHODS MUST USE IF STATEMENTS ECT.....
import java.util.ArrayList;
import java.util.EmptyStackException;
/**
* The MyStack class represents a last-in-first-out (LIFO) stack of objects.
* MyStack uses a generic ArrayList. Your stack should work exactly like java.util.Stack,
* except that you only have to implement a subset of the methods, as shown below.
* @param
*/
public class MyStack
private ArrayList
public MyStack() {
items = new ArrayList<>();
}
/**
* Pushes item onto the top of this stack
* @param item the item to be pushed onto this stack
* @return the item argument
*/
public E push(E item) {
items.add(item);
return item;
}
/**
* Removes the object at the top of this stack and returns that object as the value of this function.
* @return The object at the top of this stack (the last item)
* @throws EmptyStackException if stack is empty when called
*/
public E pop() {
if(items.isEmpty()) {
throw new EmptyStackException();
}
else {
return items.remove(items.size() - 1);
}
}
/**
* Looks at the object at the top of this stack without removing it from the stack.
* @return the object at the top of this stack (the last item)
* @throws EmptyStackException if stack is empty when called
*/
public E peek() {
if(items.isEmpty()) {
throw new EmptyStackException();
}
else {
return items.get(items.size() - 1);
}
}
/**
* Test if Stack is empty
* @return true if and only if this stack contains no items; false otherwise.
*/
public boolean isEmpty() {
if(items.isEmpty()) {
return true;
}
else {
return false;
}
}
/**
* Returns the number of items in the stack
* @return size of the stack
*/
public int size() {
return items.size();
}
/**
* Removes all the elements from the Stack. The Stack will be empty after this call
* (unless an exception occurs)
*/
public void clear() {
items.clear();
}
/**
* Returns the 1-based position where an object is on this stack.
* If the object o occurs as an item in this stack, this method
* returns the distance from the top of the stack of the occurrence
* nearest the top of the stack; the topmost item on the stack is
* considered to be at distance 1. The equals method is used to
* compare o to the items in this stack.
*
* Example:
*
* s.push(4);
* s.push(5);
* s.push(6);
* s.search(6); // 1
* s.search(5); // 2
* s.search(4); // 3
* s.search(27) // -1
*
* @param o the desired object.
* @return the 1-based position from the top of the stack where the object is located;
* the return value -1 indicates that the object is not on the stack.
*/
public int search(Object o) {
}
/**
* Returns true if this stack contains the specified element.
* More formally, returns true if and only if this stack contains
* at least one element e such that (o==null ? e==null : o.equals(e)).
* @param o element whose presence in this ArrayList is to be tested
* @return true if this ArrayList contains the specified element
*/
public boolean contains(Object o) {
}
/**
* Returns the index of the first occurrence of the specified element in this stack,
* or -1 if this ArrayList does not contain the element. This is the index in the
* underlying ArrayList, NOT the position in the stack as defined in the search
* function. More formally, returns the lowest index i such that (o==null ?
* get(i)==null : o.equals(get(i))), or -1 if there is no such index.
* @param o element to search for
* @return the index of the first occurrence of the specified element in this ArrayList,
* or -1 if this ArrayList does not contain the element
*/
public int indexOf(Object o) {
}
/**
* Returns the index of the last occurrence ;of the specified element in this stack,
* or -1 if this ArrayList does not contain the element. This is the index in the
* underlying ArrayList, NOT the position in the stack as defined in the search
* function. More formally, returns the highest index i such that (o==null ?
* get(i)==null : o.equals(get(i))), or -1 if there is no such index.
* @param o element to search for
* @return the index of the last occurrence of the specified element in this stack
*/
public int lastIndexOf(Object o) {
}
/**
* String representation of a stack.
* Format is the same as {@link ArrayList#toString()} and
* the order is from bottom of the stack to the stop.
* @return string representation of a stack
*/
@Override
public String toString(){
StringBuilder b = new StringBuilder();
b.append("[");
for (E e : items) {
b.append(e);
if (items.iterator().hasNext()) {
b.append(",");
}
}
b.append("]");
return b.toString();
}
/**
* Returns the element at the specified position in this list.
* @param index index of the element to return. This is the index in the
* underlying ArrayList, NOT the position in the stack as defined in the search
* function.
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size())
*/
public E get(int index) {
return items.get(index);
}
}
---------------------------------------------------------------------------------------------------------------------
// TestProgram.java - Test program for stack lab
import java.util.EmptyStackException; import java.util.Random; import java.util.Stack;
public class MyStackTestProgram {
private static Random random = new Random();
public static void main(String[] args) {
// Make a Java stack Stack
// test each method, leave code in so that TA can verify the class testPush(javaStack, myStack); testPop(javaStack, myStack); testPeek(javaStack, myStack); testSize(javaStack, myStack); testClear(javaStack, myStack); testIsEmpty(javaStack, myStack); testFirstIndex(javaStack, myStack); testLastIndex(javaStack, myStack); testContains(javaStack, myStack); testSearch(javaStack, myStack);
}
public static void testPush(Stack
public static void testPop(Stack
// Stack empty try { javaStack.pop(); } catch (EmptyStackException e) { System.err.println("Java Stack: caught EmptyStackException"); } try { myStack.pop(); } catch (EmptyStackException e) { System.err.println("My Stack: caught EmptyStackException"); } }
public static void testPeek(Stack
public static void testSize(Stack
public static void testClear(Stack
public static void testIsEmpty(Stack
public static void testFirstIndex(Stack
public static void testLastIndex(Stack
public static void testContains(Stack
public static void testSearch(Stack
System.out.println(javaStack);
}
// Initialize stack by pushing random data private static void pushRandom(Stack
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