CS 112 Project 7: Stacks and Exceptions
Objectives:
- Practice building Stack operations.
- Use a Stack to solve a problem
- Practice using exceptions.
Introduction
This week's project is a single person project.
The project is to:
- Add some new methods to your Stack class:
- aStack.getSize(), that returns the number of items currently in aStack;
- aStack.getCapacity(), that returns the current capacity of aStack; and
- aStack.setCapacity(newCapacity),
that changes the capacity of aStack
to newCapacity.
Note that if the new capacity is different from the old capacity,
changing the capacity involves dynamically allocating a new array
that is the new size,
copying the Items from the old array into the new array,
and deallocating the old array.
(This one is tricky; if newCapacity <
getSize(), your setCapacity() method
should throw an exception.)
- Convert your Stack into a
Stack<Item> template; and
- Use your Stack<Item> to solve a problem.
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:
- Use setCapacity() and getCapacity() to double the stack's capacity; and
- 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
-
A hard copy of this grade sheet,
attached to a hard copy of a
script file that lists the files in
your project,
shows that it compiles/links correctly, and shows it correctly
running and providing the required functionality.
-
An electronic copy of your project, in your
/home/cs/112/current/yourUserName/proj7 folder.
CS >
112 >
Projects >
07
This page maintained by
Joel Adams.