Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PYTHON from PythonLabCode.LabCode import StackEmptyException class Stack: def __init__(self, max_size=5): self.top = -1 self.max_size = max_size # TODO comment self.items = [ for x in
PYTHON
from PythonLabCode.LabCode import StackEmptyException class Stack: def __init__(self, max_size=5): self.top = -1 self.max_size = max_size # TODO comment self.items = ["" for x in range(max_size)] # TODO comment def is_empty(self): # TODO return True # Possibly you will remove this line, this is for running Unit Tests before writing code def is_full(self): return self.top == self.max_size - 1 # TODO comment def push(self, item): # TODO pass # Possibly you will remove this line, this is for running Unit Tests before writing code def pop(self): item_str = "" # TODO return item_str def peek(self): if self.is_empty(): raise StackEmptyException return self.items[self.top] def size(self): # TODO return -1 # Possibly you will remove this line, this is for running Unit Tests before writing code def print_stack_up(self): stack_str = "" # TODO return stack_str; # Possibly you will remove this line, this is for running Unit Tests before writing codeRun the Unit Tests First run the Unit Tests to determine the test that are not passing (RED). You will refactor those until the corresponding tests are passing (GREEN). Do not change the tests to make them pass, you are only changing the code to make your tests GREEN. Some tests might be passing, but still are dependent on other code and may not pass in later runs, indicating you may need to change update other code. Always remove TODO when you have completed the task. Stack Class Class members These are provided. This is an array of string. Look at what other members are necessary: top and maxSize. These are integers. Add comments beside maxSize and top that explain what they represent. isFull() This is written for you. Since the array holds the items, this stack will have a limit. Some stacks may be implemented using other collections (List, ArrayList, etc.) that have sizes which can be increased and isFull() might not need to be tested. In this lab you are limited in stack size. - Add comments on how this tests isfull() isEmpty() This is not written. Since the array holds the items in the stack, what will the isEmpty() stack look like? HINT: look at the default no-arg constructor and look at isFull(). peek() This is written for you, look at the code and add comments about the if-statement and why it is necessary. Notice the exception thrown for error. pop() This is incomplete. Add the code needed to complete this, remove the return line. HINT: look at peek() push() This is not written. Write the coded need to push() an item on the stack. Refer to pop() and peek(). What if statement would you include here? Is an exception needed? - Write the code to an item to the top of stack (array) How do you add to the top of the stack (array) What if statement would you include here? Is an exception needed? What happens with top? - Add comments about the above code. printStackUp() This is not written. Write this to return a string that will be printed in a driver/tester. When the stack is empty throws StackEmptyException Extra Credit (5 pts): Improve the 4 Unit Test for isFull() and isEmpty() to be written more efficiently
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started