CS 112 Project 7: Stacks and Exceptions

Objectives:

  1. Practice building Stack operations.
  2. Use a Stack to solve a problem
  3. Practice using exceptions.

Introduction

This week's project is a single person project. The project is to:

You are not to add any new instance variables to your Stack class. As usual, you should use test-driven development to create the new operations. You may write and use a single test-method to test all three operations. Make certain you thoroughly test your setCapacity() method, as this method is very easy to write incorrectly.

The Problem

You are to write a simpler encode/decode program that reads an input file and creates an output file in which each word from the input file is reversed. White space and punctuation should be left as is. For example, if the input file contains:
   dear sam,
   I am having a great time and wish you were here.
   my 18th birthday was last week and several of us went out for dessert.
   computer science is my favorite course!
   
   love,
   chris.

then your program should produce an output file containing an "encoded" version of the input file:

   raed mas,
   I ma gnivah a taerg emit dna hsiw uoy erew ereh.
   ym ht81 yadhtrib saw tsal keew dna lareves fo su tnew tuo rof tressed.
   retupmoc ecneics si ym etirovaf esruoc!
   
   evol,
   sirhc. 

Note that if you were to then run your program again and use this second file as the input file, that execution of your program should produce the original input file as its output! Put differently, your program should be able to both encode and decode a file!

To accomplish this, your program needs to process each character in the file individually. One way to accomplish this is to read the input file character-by-character. (Hint: the get() function may be useful for this approach.) Alternatively, your program may read the input file a line at a time into a string variable, and then process each character in that string, one at a time.

If a character is a letter or digit, your program should push it onto a stack. If the character is anything else, then so long as the stack is not empty, your program should repeatedly pop and write the top character from the stack to the output file; and then write the non-letter/digit character to the file. To distinguish letters and digits from other characters, you may find the functions in the <cctype> library to be useful.

The twist is that you are to define your stack with an initial capacity of 1. You are to "wrap" the push() calls in a try-catch block. If push() throws an exception (because the stack is full), your exception-handler code should:

  1. Use setCapacity() and getCapacity() to double the stack's capacity; and
  2. Invoke push() again to push the character onto the (now larger) stack.

If you get to the point where you cannot figure out what is wrong, use the debugger! Practice your skills using the debugger!

Submit


CS > 112 > Projects > 07


This page maintained by Joel Adams.