

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CS 1410 Introduction to Computer Science โ CS Section 2: MWF 9:30 a.m. โ 10:20 a.m. Instructor: Xiaojun Qi Programming Assignment #
Given: March 3, 2009 Due Date: March 20, 2009 Submit electronically via the eagle system before 11:59 p.m. Total Points: 25 points
Write a template class to implement a stack data structure. In addition, the stack operations will be monitored by using exception handling. That is, you need to protect the stack operations using the exception handling techniques introduced in class and in the textbook.
A stack is a simple data structure that has items pushed onto it and items popped off of the top. The following diagram illustrates how a stack looks and works.
Empty Stack
3
27
9
Stack w/ Integers
Hi Mom
This is fun
Stack w/ Strings
A stack may contain any data type. In the example above, the stack contains either integers or strings. A stack could contain floats, doubles, or even other classes.
Instead of writing individual classes and functions to handle each possible kind of stack, it is useful to create a generic template stack and then define its type in the code and allow the compiler to provide the specific implementation at compile time.
In this assignment, you must create a template stack class named TStack , with the following public members:
TStack(int nSize) This is a constructor that will initialize the maximum size of the stack. In other words, you must not hardcode the size of the stack! Each instance must have a dynamic stack size according to the value passed into this constructor.
void Push(T item) This member will place a new item on the top of the stack T Pop() This member will pop and return the item on the top of the stack int Size() Returns the maximum size of the stack int Position() Returns the current position of the stack pointer (i.e. the top of the stack) operator[] Overload the [] operator so that it will return a copy of the item located at a specified index. This operation is a read-only operation. That is, it does not affect the stack.
In addition to the above required members of your class, you must include exception handling to not allow any error conditions to occur in your code. This exception class must allow the calling code (the code that uses the stack class) to obtain a short message about the error condition and the current stack size and position. That is, you have to create an exception class.
Write a driver program that clearly demonstrates all the required elements of this assignment. Make sure to add appropriate comments to each of your test cases. It is your responsibility to prove to the grader your code is correct, not for her to guess if it worked correctly.
Let us focus on the stack of integers (i.e., TStack