Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java program: For this assignment you will write a class that manages a stack of integer data and a menu-driven program that allows the user

Java program:

For this assignment you will write a class that manages a stack of integer data and a menu-driven program that allows the user to exercise this class.

Class that manages a stack of integer data:

  • Stack will store int data items.
  • Stack will be implemented using an int array of fixed size:
    • The user of the program will determine the size of the stack; this information will be passed to the constructor of the class.
  • Class will contain:
    • Constructor:
      • Constructor will set up an int array of size selected by the user and store its address in the class reference variable for the stack.
    • push() method:
      • Push method will receive one int as a parameter and will store this at the top of the current stack.
      • There will be no error-checking in the push method.
    • pop() method:
      • Pop method will remove the top value from the stack and return it to the calling method.
      • There will be no error-checking in the pop method.
    • stackFull() method:
      • A Boolean method; returns:
        • true if the stack is full.
        • false, otherwise.
    • stackEmpty() method:
      • A Boolean method; returns;
        • true if the stack is empty.
        • false, otherwise.
    • printStack() method:
      • This method will print the current contents of the stack along with the cell of the array in which each element is stored:
        • Note: This method will print the items that are currently on the stack; do not print any items that have been popped off the stack but have not been overwritten by a following push operation.
    • Instance variables:
      • Reference variable for an integer array.
      • (Optional) Integer variable to hold size of array being used as the stack:
        • Note: In Java this instance variable is not necessary since the programmer can use .length to determine the arrays size.
          • Remember that the size of the array is not necessarily the number of items currently stored in the array.

A menu-driven test program for your stack class; this program will perform the following tasks:

  • Ask the user the size of the stack they wish to work with.
  • Set up an object of your stack class:
    • Pass the size of the desired stack to the class constructor.
  • Display a menu with the following options:
    • Push an integer onto the stack.
    • Pop an integer from the stack.
    • Determine whether the stack is full.
    • Determine whether the stack is empty.
    • Print the current contents of the stack.
    • Exit the menu.

Coding of menu options:

  • Push an integer onto the stack:
    • Start by calling the class stackFull() method:
      • If the stack is full, display a message to the user and exit this menu option.
      • If the stack is not full, allow the user to input an integer from the keyboard, then pass this integer to the class push() method.
        • Only ask for a value if the stack is not full; dont waste the users time by having them input a value only to find there is no room for that value on the stack.
  • Pop an integer from the stack:
    • Start by calling the class stackEmpty() method:
      • If the stack is empty, display a message to the user and exit this menu option.
      • If the stack is not empty, call the class pop() method; upon returning from the call, display the value popped.
  • Determine whether the stack is full:
    • Display the current status of the stack:
      • Dont just display true or false; use the value returned by the method to determine whether to tell the user whether the stack is full or not; print a message for the user.
  • Determine whether the stack is empty:
    • Display the current status of the stack:
      • Dont just display true or false; use the value returned by the method to determine whether to tell the uses the stack is empty or not; print a message for the user.
  • Print the current contents of the stack:
    • Determine whether the stack is empty:
      • If so, print a message stating this fact.
      • If not, print the contents of the stack with the array subscripts for the data:
        • Only print the items that are currently on the stack; remember that the array might contain values that have been popped off the stack but not overwritten by a following push.
    • The output for this operation might look like the following: Stack[0] = 5 Stack[1] = 12 Stack[2] = 4
      • Assuming the numbers 5, 12, and 4 had been pushed in that order.
        • Remember that the first value pushed is stored in array cell with index 0.

Note that the class push and pop methods are bare-bones methods:

  • The push method receives a single integer and stores it on the top of the stack.
  • The pop method removes the top integer from the stack and returns it to the calling method.

Saying that the push and pop methods are bare-bones means that there is no error checking in these methods. But remember that attempting to push a value onto a full stack causes a StackFull error; attempting to pop a value from an empty stack causes a StackEmpty error.

  • Since we are using a fixed-size array for our stack, these would be array subscript out of range exceptions; they would cause the program to crash.
  • Therefore, the push menu option must call the class stack full method before calling push; if it finds that the stack is already full, push is not called.
  • Similarly, the pop menu option must call the class stack empty method before calling pop; if it finds that the stack is empty, pop is not called.

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

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions

Question

Assess various approaches to understanding performance at work

Answered: 1 week ago