Cmpt 225
Simon Fraser University
School of Computing Science
Cmpt 225 - Lab #3
Generic classes and methods, Exception handling and File I/O
Objectives
Our objectives in this Lab #3 is to become familiar with exceptions andperforming data input and output using files. Specifically, you will learn...- How to create generic classes and methods
- How to read from a file using I/O streams
- classes File, FileReader, and BufferedReader
- package java.io
- StringBuffer and StringTokenizer classes
- How to write to file using I/O streams
- classes File, FileWriter, and BufferedWriter
- package java.io
- classes PrintWriter
- Exceptions
- exception classes
- checked and unchecked exceptions
- try, catch and throw
- catch(...)
- finally keyword
Problem Statement
In this lab, you are to build a Stack abstract data type (ADT) class, whichmust satisfy the following requirements:- It must be an index-based Stack abstract data type (ADT) class.
- It must allow the user to store elements of any data type (e.g., int, double,char, string, objects) into it.
- It must provide functions for pushing, popping and peeking at an elementas well as returning the number of elements currently stored onto the stack.
- Your stack must throw exceptions when the stack is empty and the clientcode is attempting to pop or peek at an element.
- When the stack is full, it must be expanded in a transparent fashion, i.e.,the expansion must be unknown to the client code. This is to say that yourprogram cannot notify the client code that the Stack is full nor shouldit prevent the client code from inserting more elements.
- When the stack is full, your program must expand the stack in a transparentfashion, i.e., the expansion must be unbeknown to the client code. Thisis to say that your program cannot notify the client code that the stackis full nor must it prevent the client code from pushing more elementsonto the stack.
Requirements
You need to take the following into account whenyou develop your application:- For testing with integers, use file stack.txtthat has the following format:
- on the first line, there is a number (N) thatrepresents the number of elements to push onto the stack
- next N lines contains integer numbers to push ontothe stack, one per line
- For each number pushed onto the stack: write thisnumber as well as the stack size (after this number has been pushed ontothe stack) to an output file.
Suggestions:
- When testing your Stack abstract data type (ADT) class, use various datatypes such as int as well as objects of Student class, which youcreated in the previous labs.
Resources
The first thing we will need to do is investigate the topics listed abovein the Objectives section. Below are partial lists of books andweb sites you may wish to look at in order to perform your investigation.List of books:
List of web sites:As stated, the above lists are not exhaustive, therefore feel free to addto them the Java books and web sites you find useful. Also, feel free tosend these (books and URL's) to the instructor so that they can be addedto this lab and to our course web site.
Anne Lavergne, Irina Pekerskaya and ZhiHao Lin - February 2005 - Last Revision date August 2006