Question: Data Structures I Sec. 0 0 1 Lab Assignment # 1 Stack Review Due date: 1 / 2 3 / 2 4 at 1 1

Data Structures I Sec. 001
Lab Assignment #1
Stack Review
Due date: 1/23/24 at 11:59 pm
Purpose: The first lab assignment will serve as a review of one of the first data structures
introduced to you. In this lab, you will focus on a nave implementation of the stack data
structure that relies on an ArrayList to handle a collection of Integer objects.
In-Class 1: For this program, you will begin by implementing a Stack class with a push method
as well as a Main class to test this push method.
Create a class called Stack that contains a single field with a data type of ArrayList.
Next, create a no-args constructor that initializes this field to a newly constructed ArrayList
object. Lastly, define a push method that takes an Integer object to be added to the end of this
ArrayList object. The push method will not return anything.
In the main method within the Main class, construct a new Stack object and push five Integer
objects of your choice onto this stack.
Lab 1: Implement the remaining methods of the Stack class: peek, pop, and isEmpty.
The peek method is used to check what is currently at the top of the stack. This method does
not require any arguments and returns the Integer object at the end of the ArrayList. The get
and size methods can be used to access this object.
The pop method is used to remove what is currently at the top of the stack. This method also
does not require any arguments. In addition to removing the Integer object at the end of the
ArrayList, this method also returns that Integer object.
The isEmpty method is used to determine if the stack currently has no objects. This method
does not require any arguments and returns a boolean value to indicate if the stack is empty.
The size method of ArrayList can be used to determine if the stack has no objects.
In the main method, after pushing five Integer objects onto the stack from the in-class
assignment, use the peek method once to print what is currently at the top of the stack. The last
Integer that was pushed onto the stack should be what is displayed to the console.
Then, use a while loop to repeatedly pop objects off of the stack until the stack is empty. In other
words, this loop will continue to execute while the stack is NOT empty. For each object popped
off the stack, print that object to the console.
Criteria: Make sure you have the following in your program:
peek method in the Stack class (25 points)
pop method in the Stack class (25 points)
isEmpty method in the Stack class (25 points)
Call to peek method in the main method (5 points)
Loop to repeatedly call the pop method in the main method (20 points)

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!