Fundamentals of Computer Science II (CSC-152 99F)


Class 39: Stack Lab

Back to Linear Structures for Solving Puzzles. On to Dictionaries.

Held Monday, November 8, 1999

Overview

Today, you will ground your understanding of stacks by working with two implementations of stacks. You will begin with an existing node-based (linked) implementation and consider the basic operations for that implementation. You will then implement a toString method to aid in testing. Finally, you will begin to develop an array-based implementation.

Notes

Contents

Summary


Preparation

In order to do this lab, you will need the following files.

  1. Create a new directory to use for the lab. You can use the file manager or the Unix mkdir command.
  2. Copy all of the files to that directory.
  3. Scan the files so that you can understand their purpose. It might help if you took notes on the purpose of each.

Preliminary Questions

You need not turn in answers to these questions. However, you should be prepared to answer them in class and on exams. Feel free to discuss the questions with your colleagues.

Test Plan

Design a simple test plan that you might use to test an implementation of stacks. You might consider what the implementation should do when the same element is added multiple times, when the last element is deleted, and so on and so forth. Summarize your plan and the expected output here.

Executing Your Test Plan

  1. Compile the files.
  2. Run the tester (NBSTester). Does the NodeBasedStack class seem to work the way you expect?

Improving Output

It might be easier to test the class if you could see the stack being built. Write a toString method that generates a human-readable representation of the contents of the stack. Here's a possible strategy:

create tmp, a reference to the top of the stack
create a result string
while tmp does not refer to the top of the stack
  add the contents of tmp to the result string
  set tmp to the next element

Once you've written this method, make sure to test it (perhaps by running your test plan).

Updating Linear Structures

Note that this is a puzzle about object-oriented design, and not necessarily about stacks.

Both add and get in the Linear class have a precondition having to do with the number of elements in the structure. It might be helpful if the methods threw an exception in cases in which the structure has no elements (get) or no room (add).

  1. Update the method declaration in Linear.java so that it's clear that get throws an exception.
  2. Do you think you have to change any other classes? Why or why not?
  3. Try recompiling Linear.java, Stack.java, and StackTester.java. Does everything continue to work successfully?
  4. Update the method declaration in Linear.java so that it's clear that add throws an exception.
  5. Do you think you have to change any other classes? Why or why not?
  6. Try recompiling Linear.java, Stack.java, and StackTester.java. Does everything continue to work successfully?
  7. Update NodeBasedStack so that it throws exceptions in add and get.
  8. Do you expect it to compile? Why or why not?
  9. Update Stack so that it throws exceptions in get.

Preparing to Work With Array-Based Stacks

  1. Scan the code for ArrayBasedStack and ensure that it compiles.
  2. Build an ABSTester class that can be used to test array-based stacks. You may want to refer to NBSTester for assistance.
  3. Which stub methods work ``correctly''? Which do not?

Implementing Array-Based Stacks

  1. Implement the isEmpty method in ArrayBasedSTack.
  2. Implement the add method in ArrayBasedStack. This method should add the object at the end of the array and update the size.
  3. Test it. Note that you need not have get implemented to test the method because I've already implemented toString.
  4. Implement the peek method in ArrayBasedStack. This method should return the ``last'' element in the array.
  5. Implement the get method in ArrayBasedStack. This method should decrease the size of the stack and return the old last element.

History

Tuesday, 10 August 1999

Thursday, 4 November 1999.

Back to Linear Structures for Solving Puzzles. On to Dictionaries.


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Outlines/outline.39.html

Source text last modified Thu Nov 4 21:02:11 1999.

This page generated on Thu Nov 4 21:04:58 1999 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu