Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 2 [20 Marks] Create Stack-data structure and use that stack in the driver class. Define a generic class called MyStack with the following specifications

  1. Question 2 [20 Marks]

Create Stack-data structure and use that stack in the driver class.

  1. Define a generic class called MyStack> with the following specifications (see the class diagram below):
    1. Create private Array reference field of Object type, and a private integer type field to store the length of the array.
    2. 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)
    3. 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.
  2. Create member method called pop () to remove the data item from the top of the stack and return that item to the called method.
  3. Create member method called push() to push an item onto the top of the stack.
  4. 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.
  5. 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:

  1. Use the same header method from Q1 with different description of the Goal (c) Define the driver method with the following specs:
    1. Call the header method.
    2. 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.
  2. creating a String array with 6 different values following this order:

"studentNumber2", "lastName2", "firstName2", "studentNumber1", "lastName1",

"firstName1"

  1. 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.
  2. Using the distance metric that you found from the search method store that number in a string variable
  3. 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):

  1. Print a message "Team Member 1 Info...." on the screen.
  2. Pop the stack for three times.
  3. Repeat i) and ii).
  4. Print a message "Here is the status of the Stack..."
  5. 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.....

  1. 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!

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

Unity From Zero To Proficiency Beginner A Step By Step Guide To Coding Your First Game

Authors: Patrick Felicia

1st Edition

1091872023, 978-1091872028

More Books

Students also viewed these Programming questions

Question

the following questions: 4-10.

Answered: 1 week ago