Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 the type of elements in this list

*/

public class MyStack {

private ArrayList items;

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 javaStack = new Stack<>(); // Make a student stack MyStack myStack = new MyStack<>();

// 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 javaStack, MyStack myStack){ System.err.println("testPush: verifying push() method"); pushRandom(javaStack, 10); pushRandom(myStack, 10); System.err.println("Java Stack: " + javaStack); System.err.println("My Stack: " + myStack); }

public static void testPop(Stack javaStack, MyStack myStack){ System.err.println("testPop: verifying pop() method"); pushRandom(javaStack, 10); pushRandom(myStack, 10); // Legal pops for (int i = 0; i < 10; i++) { System.err.printf("javaStack.pop(): %4s, myStack.pop(): %4s ", javaStack.pop(), myStack.pop()); }

// 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 javaStack, MyStack myStack){ System.err.println("testPeek: verifying peek() method"); pushRandom(javaStack, 10); pushRandom(myStack, 10); for (int i = 0; i < 10; i++) { System.err.printf("javaStack.peek(): %4s, myStack.peek(): %4s ", javaStack.peek(), myStack.peek()); javaStack.pop(); myStack.pop(); } }

public static void testSize(Stack javaStack, MyStack myStack){ System.err.println("testSize: verifying size() method"); pushRandom(javaStack, 23456); pushRandom(myStack, 23456); for (int i = 0; i < 12123; i++) { javaStack.pop(); myStack.pop(); } System.err.printf("javaStack.size(): %4s, myStack.size(): %4s ", javaStack.size(), myStack.size()); }

public static void testClear(Stack javaStack, MyStack myStack){ System.err.println("testClear: verifying clear() method"); pushRandom(javaStack, 23456); pushRandom(myStack, 23456); javaStack.clear(); myStack.clear(); System.err.printf("javaStack.size(): %s, myStack.size(): %s ", javaStack.size(), myStack.size()); }

public static void testIsEmpty(Stack javaStack, MyStack myStack){ System.err.printf("javaStack.isEmpty(): %4s, myStack.isEmpty(): %4s ", javaStack.isEmpty(), myStack.isEmpty()); javaStack.push("1111"); myStack.push("2222"); System.err.printf("javaStack.isEmpty(): %4s, myStack.isEmpty(): %4s ", javaStack.isEmpty(), myStack.isEmpty()); javaStack.pop(); myStack.pop(); System.err.printf("javaStack.isEmpty(): %4s, myStack.isEmpty(): %4s ", javaStack.isEmpty(), myStack.isEmpty()); }

public static void testFirstIndex(Stack javaStack, MyStack myStack){ pushRandom(javaStack, 1234); pushRandom(myStack, 1234); pushRandom(javaStack, 1234); // guarantees duplicates, same random seed pushRandom(myStack, 1234); // guarantees duplicates, same random seed System.err.println("javaStack.indexOf(\"7449\"): " + javaStack.indexOf("7449")); System.err.println("myStack.indexOf(\"7449\"): " + myStack.indexOf("7449")); }

public static void testLastIndex(Stack javaStack, MyStack myStack){ pushRandom(javaStack, 1234); pushRandom(myStack, 1234); pushRandom(javaStack, 1234); // guarantees duplicates, same random seed pushRandom(myStack, 1234); // guarantees duplicates, same random seed System.err.println("javaStack.lastIndexOf(\"7449\"): " + javaStack.lastIndexOf("7449")); System.err.println("myStack.lastIndexOf(\"7449\"): " + myStack.lastIndexOf("7449")); }

public static void testContains(Stack javaStack, MyStack myStack){ pushRandom(javaStack, 1234); pushRandom(myStack, 1234); System.err.println("javaStack.contains(\"7449\"): " + javaStack.contains("7449")); System.err.println("coolStack.contains(\"7449\"): " + myStack.contains("7449")); System.err.println("javaStack.contains(\"4444\"): " + javaStack.contains("4444")); System.err.println("coolStack.contains(\"4444\"): " + myStack.contains("4444")); }

public static void testSearch(Stack javaStack, MyStack myStack){ pushRandom(javaStack, 1234); pushRandom(myStack, 1234); System.err.println("javaStack.search(\"7449\"): " + javaStack.search("7449")); System.err.println("coolStack.search(\"7449\"): " + myStack.search("7449")); System.err.println("javaStack.search(\"1202\"): " + javaStack.search("1202")); System.err.println("coolStack.search(\"1202\"): " + myStack.search("1202")); System.err.println("javaStack.search(\"4444\"): " + javaStack.search("4444")); System.err.println("coolStack.search(\"4444\"): " + myStack.search("4444"));

System.out.println(javaStack);

}

// Initialize stack by pushing random data private static void pushRandom(Stack stack, int number) { random.setSeed(1234); for (int i = 0; i < number; i++) { stack.push(Integer.toString(random.nextInt(10000))); } } private static void pushRandom(MyStack stack, int number) { random.setSeed(1234); for (int i = 0; i < number; i++) { stack.push(Integer.toString(random.nextInt(10000))); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

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

Question

15. Identify the rescue from without in The Empire Strikes Back.

Answered: 1 week ago

Question

2. Identify the purpose of your speech

Answered: 1 week ago