Question: In our class example StackApp, add the following static function in StackApp class: public static boolean searchItem(StackOfIntegers stack, int num); The function will search the

In our class example StackApp, add the following static function in StackApp class:

public static boolean searchItem(StackOfIntegers stack, int num);

The function will search the num in the stack, if the num is in the stack, returns true, otherwise, returns false.

Then, inside main(), test the searchItem() function.

*Class I need the function added in*

public class StackApp {

public static void main(String[] args) { StackOfIntegers stack = new StackOfIntegers(); for (int i = 0; i < 10; i++) stack.push(i); // Push i to the stack while (!stack.empty()) // Test if stack is empty System.out.print(stack.pop() + " "); // Remove and return from stack System.out.println(); //ConvertToBinary(13, 16); } public static void ConvertToBinary(int num, int base) { StackOfIntegers S = new StackOfIntegers(); //Fill the stack with remainders. while (num != 0) { S.push(num % base); num /= base; } // Display implemented with character conversion. /*while (!S.empty()) { if (S.peek() > 9) System.out.print((char)(S.peek() - 10 + 'A')); else System.out.print(S.peek()); S.pop(); }*/ // Display implemented with an array of chars for numbers 10-15 /*int y; char[] HexArray = new char[] {'A', 'B', 'C', 'D', 'E', 'F'}; while (!S.empty()) { y = S.peek(); if (y > 9) System.out.print(HexArray[y - 10]); else System.out.print(y); S.pop(); }*/ /* Display implemented with a switch statement.*/ int y; while (!S.empty()) { y = S.peek(); switch (y) { case 10: System.out.print('A'); break; case 11: System.out.print('B'); break; case 12: System.out.print('C'); break; case 13: System.out.print('D'); break; case 14: System.out.print('E'); break; case 15: System.out.print('F'); break; default: System.out.print(y); } S.pop(); } } }

public class StackOfIntegers { private int[] elements; private int size; public static final int DEFAULT_CAPACITY = 16;

/** Construct a stack with the default capacity 16 */ public StackOfIntegers() { this(DEFAULT_CAPACITY); }

/** Construct a stack with the specified maximum capacity */ public StackOfIntegers(int capacity) { elements = new int[capacity]; }

/** Push a new integer into the top of the stack */ public void push(int value) { if (size >= elements.length) { int[] temp = new int[elements.length * 2]; System.arraycopy(elements, 0, temp, 0, elements.length); elements = temp; }

elements[size++] = value; }

/** Return and remove the top element from the stack */ public int pop() { return elements[--size]; }

/** Return the top element from the stack */ public int peek() { return elements[size - 1]; }

/** Test whether the stack is empty */ public boolean empty() { return size == 0; }

/** Return the number of elements in the stack */ public int getSize() { return size; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!