Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Lab-lmplement a Stack ADT as an Array Part #1: Practice with the Stack Design a Java class called StackArray, in StackArray.java. It should have three
Lab-lmplement a Stack ADT as an Array Part #1: Practice with the Stack Design a Java class called StackArray, in StackArray.java. It should have three data elements 1) Size (number of non-null items in the array - should be zero initially) 2) Capacity (initial size of the array four is a good number for this) 3) String array (initialize to the value of capacity in the constructor) Since it's based on the Stack abstract data type, it should have these methods One-argument constructor (capacity) - No-arg constructor Get methods for size, capacity and the array isEmptx) that returns true/false (whether array size is zero) Properly-formatted toString) method (with String return type) that prints isEmpty), the size and the array contents Push0 method that takes a String argument and adds the string to the array at index position given by the size (initially zero but increases by one each time). It should also call the toString) method so the user can see the current state of the Stack object. Pop) method that prints the item at the top of the stack, decrements the size by one and sets the previous last position to null. Should also call the toString) method Resize0) method that resizes the stack to double it's size + 1. Push0 should call resize0 if the size of the array equals capacity. Otherwise you will get AralodexQutQfoundsException. Include a main) method to test the object. Add several strings (e.g. "fourscore", "and", 'seven", Years", "ago") and then pop them off the stack. When it's run, you should be able to see the stack displayed as it grows and shrinks See next page for a sample run
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