Answered step by step
Verified Expert Solution
Link Copied!

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

  1. 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();
  2. 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.
  3. 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

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

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Managing Your Information How To Design And Create A Textual Database On Your Microcomputer

Authors: Tenopir, Carol, Lundeen, Gerald

1st Edition

1555700233, 9781555700232

More Books

Students also viewed these Databases questions

Question

what is a peer Group? Importance?

Answered: 1 week ago