Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given an array implementation of a STACK that holds integers. (Refer to Array Implementation of Stack on Blackboard) .Stack is initialized in the Driver class
- Given an array implementation of a STACK that holds integers. (Refer to Array Implementation of Stack on Blackboard) .Stack is initialized in the Driver class as follows: StackArray
intStack = new StackArray (); - Write a new public void member function called countPosNeg. This function counts and displays the number of positive integers and number of negative integers in IntStack. IntStack must be returned to its original state after counting.
- Array Implement of Stack:
package ArrayImplementations; public class StackArray{ private int top=-1; private static final int MAX_ITEMS = 10; private E items[]; @SuppressWarnings("unchecked") public StackArray() { items = (E[]) new Object[MAX_ITEMS]; System.out.println("Stack Created!"); } public void push(E e) { if (isFull()==true) { System.out.println("Stack Full!"); } else{ top=top+1; items[top] = e; } } public E pop() { if (isEmpty()==true) { System.out.println("Stack Empty!"); } else{ E e = (E) items[top]; items[top] = null; top = top-1; return e; } return null; } public boolean isFull() { if (top == items.length-1) { return true; } return false; } public boolean isEmpty(){ if (top==-1) { return true; } return false; } @Override public String toString() { System.out.println("Array:"); System.out.print("{"); for(int i = 0; i < items.length ;i++) { System.out.print(items[i]+" "); } System.out.print("}"); return ""; } public static void main(String[] args) { // Code reference for countPosNeg method StackArray intStack = new StackArray (); intStack.push(10); intStack.push(10); intStack.push(30); intStack.push(-40); System.out.println("intStack before counting"); System.out.println(intStack); // call countPosNeg here System.out.println("intStack after counting"); System.out.println(intStack); // Code reference for sameStack method StackArray stack = new StackArray (); StackArray stack2 = new StackArray (); stack.push(10); stack.push(20); stack.push(30); stack.push(40); stack2.push(10); stack2.push(20); stack2.push(30); stack2.push(40); System.out.println(stack); System.out.println( stack2); //Calling comparison method // stack.sameStack(stack2); } }
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