Lab 7: Stacks
Due Date and Submission Requirements
- Due Date: Thursday, October 17th at 11:59 p.m.
- Partner Information: This is an individual assignment. You are allowed to collaborate with other students, but each student must submit their individual, independent solution.
- Submission Instructions: Submit your solution, BookStack.java and Book.java to the appropriate D2L dropbox.
The goal of this lab is:
- Gain experience using a Stack data structure
- Implement a stack in Java using an array
Directions
Using Lab7Demo.java as a starting point, you will fill in the missing methods for the BookStack class, which are defines in the interace StackMethods. The stack class implements this interface, so you must ride all methods from the interface.
Your Stack will hold Book objects, and must use an array to hold its data . This array must be able to dynamically grow if the array is at capacity. You are not allowed to use an ArrayList.
You must define the Book class and write the methods you think you will need. Each Book object has a title (String) and an author (String).
You are NOT allowed to modify the Lab7Demo class. After creating the BookStack class, you must define the following methods (which are also defined in the interface):
- push(Book newElement)- This push() method adds a new Book object (newBook) to the top of the stack. If the Stack/Array is full, your code should dynamically grow the array by one spot. You can use the code we discussed in class, but I would recommend looking at the documentation for the Arrays.copyOf() method.
- pop()- the pop() method removes and returns the top element of the stack
- peek()- the peek() method returns the current top element of the stack
- getSize()- returns the current size of the stack
- isEmpty()- returns true of the stack is empty, returns false if the stack is not empty
- printStack() - prints out each element in the stack. The top of the stack is the first thing printed out, the bottom of the stack is the last thing printed out.
Starting Code
Output
When you run your program, your output should look exactly like this screenshot
Rules
- You are not allowed to import Stack
- You are not allowed to use ArrayLists
Grading (10 points)
- 2 points- push() is correct
- 2 points- pop() is correct
- 1 point- peek() is correct
- 1 point- getSize() is correct
- 1 point- isEmpty() is correct
- 1 point- printStack() is correct
- 2 points- The BookStack class is correctly defined, and is correct