Question 2 [20 Marks]
Create Stack-data structure and use that stack in the driver class.
Define a generic class called MyStack
Create private Array reference field of Object type, and a private integer type field to store the length of the array.
Create constructor with length parameter that will generate an Object-array size length for the Stack. (Assumption: The index [length – 1] is the top of the array)
Create member method called empty () to tests if the stack is empty or not iv) Create a member method called peek () to check the data-item at the top of the stack without removing it from the stack.
Create member method called pop () to remove the data item from the top of the stack and return that item to the called method.
Create member method called push() to push an item onto the top of the stack.
Create member method called search() that returns the 1-based position where a data item is on this stack. In this case, the topmost item on the stack is considered to be at distance 1. If the searched item is in the 3rd position from the top, this method will return 3. The equals method from the Object Class can be used to compare anyData to the items in the stack.
Override the Object’s toString() method so that it can print your stack content (Hint:
you can return Arrays.toString(arrayName) from inside the toString() definition.) [Hint: Check the following site (scroll down) for the description of the methods again:
https://docs.oracle.com/javase/10/docs/api/java/util/Stack.html]
MyStack
-objectArray: Object[]
-length: int
+MyStack(length: int)
+empty(): boolean
+peek(): E
+pop(): E
+push(anyName: E): E
+search (anyName: Object): int
+toString(): String
Now define a driver class called StackDemo0 with the following:
Use the same header method from Q1 with different description of the Goal (c) Define the driver method with the following specs:
Call the header method.
Create an empty (length = 0) String stack using the class you have created in (a) iii) Print the stack content with the help of the toString() method.
creating a String array with 6 different values following this order:
“studentNumber2”, “lastName2”, “firstName2”, “studentNumber1”, “lastName1”,
“firstName1”
Push the above array values to the stack you created in ii. vi) Print the stack content with the help of the toString() method. vii) Print the top of the stack value by peeking. viii) Search for the first student number (e.g., 250922765) using the search method.
Using the distance metric that you found from the search method store that number in a string variable
Convert the first and last characters of this string to number using Integer’s parseInt method and then print the average of these two numbers on the screen. (Ask your instructor on this part if you have any confusion)
(f) Create a new public static void footer method with MyStack type reference vriable as a formal parameter and do the following (see the sample output):
Print a message “Team Member 1 Info….” on the screen.
Pop the stack for three times.
Repeat i) and ii).
Print a message “Here is the status of the Stack…”
Print the stack content with the help of the toString() method.
Sample Output:
******************************************************* Names: firstName1 and firstName2.
Student Numbers: studentNumber1 and studentNumber2 Goal of this project: Brief description of the project.
*******************************************************
Revisiting the concept of Stack…..
The stack is Empty! The stack: []
Pushing the String values to the stack….
The stack: [251896357, lastName2, firstName2, 250922765, lastName1, firstName1] The value at the top is: firstName1 Searching for studentNumber1….
The item has been found in distance 3 with reference to the top
The first number in the student ID is 2
The last number in the student ID is 5 The average of these two numbers is: 3.5
Team Member 1 info…..
SPROG 20178 MMM
First Name: firstName1
Last Name: lastName1
Student Number: 250922765 Team Member 2 info….. First Name: firstName2
Last Name: lastName2
Student Number:251896357
Here is the status of the Stack… The stack is Empty! The stack: [] Goodbye!