Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help in Java! Stack Operations using an Array Implementation In this lab you will use an array to implement a stack. An index will be

Help in Java!
image text in transcribed
image text in transcribed
image text in transcribed
Stack Operations using an Array Implementation In this lab you will use an array to implement a stack. An index will be needed (a variable named topOfStack) that will keep track of the location of the top element in the stack within the array This variable is initialized with a value of-1 Each operation is described below, where the variable colorStack references an empty ArrayStack object. The given code fragments show how the stack will be used by a client program, your test file, or a main method StackIntertacecString> colorStack nex ArrayStack0: colorStack: push: Adds an element to the end of the aray (the top of the list) and increments the index of the top element by one. Also expands the array if it becomes full Notice how the value of topOStack is incremented when an element is pushed onto the stack colorStack.push (nev String("Black"]) colorStack push (new String("White")): pop: Returns the elemeat at the top of the stack, decrements the top of stack indexs, and deletes the element from the array colorStack pop 0: 7zeturns White colorStack.pop 1zeturns "Black . peek: Returns the element at the top of the stack without modifying stack or changing the value of topOfStack sEmpty: Retarns false if the stack still has elements (Le. the top index 0) Data Stractures | Stack Implementation Create a new Java project called Lab4 (select New from the Project menu item) and select the Next> button as indicated to the right. On the next window select the Libraries by clicking Add Library Then, you are going to add the Junit Library to your Java project as shown below After you select the testing framework, you can click Finish to create your project Download from Blackboard and import from the File System location of the download the files, StackInterface-java, ArrayBasedStackTest dava, and EmptyStackException-java. Check to see that these files are now located inside the (default package) of your sre folder Create class called ArrayBasedStackeT> that implements the stack!nterface > Declare the following variables: e data: references an array storing elements in the list . coporstack: an int value representing the location of the stack top in the array INITIAL CAPACITY:0default capacity ofthe stack set to 5 Data Structures Stack private TO data private int topofStack private static final int INITIAL CAPACITY 5 Add a constructor that will initialize the stack with a user-defined initial capacity that must be greater than 0. If the parameter is 0 or less, throw an IlegalArgumentException with the comment "Array initial size error". The top of the stack is assigned a value of-1 to indicate that the stack is currently empty public ArrayBasedStack(int copacity) if(capocity)t throw new IllegalArgumentException"Array initial size error. SuppressRarnings("unchecked") TO tempStock (T)new Object[copacity]: data teepStack; top0fStack--1 Another constructor takes no parameters and sets the capacity of the data array to the default value. Add a private method called expandArray() that will double the size of the array when called. To reduce the amount of code, you can use the copyOf method in java.util.Arrays. Add another private method called is ArroyfullO that returns true if the topofStack exceeds the largest index value in the array StackInterface method implementations Add a method called push that will add an element to the top of the stack If the array is full, increase the length of the array by calling the private method, expandArrayO that you wrote to expand the array e Increase the toporStack to point to the new index location and insert the new element into dhe stack The isEnptyO method returns true if there are no elements in the stack and false otherwise. If the top of the stack is less than 0 then there are no elements currently in the stack. Implement isEmptyO. The pop method will remove the last element that was added to the stack. Implement popOby following these guidelines: If stack is empty, throw an EmptyStackException. Ifthere are elements in the stack, get the element at the top ofthe stack (index is topostack) and decrement the index of the top of the stack Set the previous top of the stack to null Decrement the index of the top of the stack and return the previous top. - - Add a method called peek() that will return the top ofthe stack but will not remove it. . If the atack is empty, throw an exception Otherwise, returm the element at the top of the stack

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

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions

Question

Describe effectiveness of reading at night?

Answered: 1 week ago

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago

Question

2. What are your challenges in the creative process?

Answered: 1 week ago