COP 4232 Software Systems Development Lab1 Requirements Documents © Dr. David A. Workman School of Computer Science University of Central Florida Orlando, Florida August 30, 2005 COP 4232 Lab1: OO Data and Exception Handling Summer 2005 Lab1: Designing, Implementing and Testing an Abstraction for Money Objectives: To gain experience designing, implementing and testing classes in C++. To develop a reusable type for later assignments. To learn how to use the following features of C++: (1) (2) (3) (4) Writing classes. File IO using fstream and its subclasses ifstream and ofstream. Exception classes and exception handling. Overloaded extraction (>>) and insertion( << ) operators as friend functions. General Requirements (all Labs): All programming labs associated with this course shall include, as a minimum, the following deliverables: all source files necessary to successfully compile, link and execute the delivered application program using g++ on Unix (Olympus or monroe); all input and output data files used to test the program prior to delivery; a Readme file giving your name (or team members), submission date, Lab number, and any comments about the submission that will help in fairly evaluating your work. All delivered files shall be submitted electronically via email in the form of a (.zip) file with a prefix having the form: Lab <number> - <Last name> - <month><day>. For example, the file name for Dr. Workman submitting Lab 1 on May 21 would be: Lab1-Workman-May21.zip. Note that only the first letter of each hyphenated phrase should begin with an upper case character; <number> is a single digit, <month> is three letters, <day> is two digits. Any resubmission of the same Lab shall be submitted using the same file format – only the date portion would need to change. If you have more than one submission of a given Lab on the same day, then only the latest one will be evaluated for a grade. 2/18/2016 © David A. Workman Page 2 COP 4232 1. Lab1: OO Data and Exception Handling Summer 2005 Lab Introduction Req1. Implement a new base class, Money, according to the class specification given below. As part of that implementation effort, test the Money class independently to ensure all methods work according to their specification. Include among your deliverables the test data and test driver for this requirement. Put your test driver (main() function) for this assignment in a file named, lab1.cpp. Define a header file, “Money.h”, as your Money interface file, and a file, “Money.cpp” as your implementation file. An exception class, AppError, will be provided for you to use with this assignment. Req2. Your main() shall catch any AppError exceptions and output the exception message and origin before terminating the program. Req3. Your main() shall prompt the user for the names of the input and output files used for testing purposes. Specification for class Money Class Money shall provide a type and an abstraction for real money. Money shall appear on external ASCII io streams as strings of the form $dd…d.cc, where “dd…d” is a decimal digit string of length at least 1 and represents a number of dollars – the leading digit must be non-zero if the number of dollar digits exceeds 1. The number of cents digits (cc) shall be exactly two. Negative values of Money shall have the form $(dd…d.cc) on external ASCII media. Both positive and negative representations do not permit embedded blanks, and the dollars and cents fields are separated by a decimal point. Class Money shall also provide arithmetic operators for adding and subtracting values of type Money and for computing scalar integer multiples of Money values. Equality and relational operators shall also be overloaded for type Money. Finally, class Money shall provide boundary methods for IO of Money values from ASCII input and output streams in C++ - this includes overloaded versions of the extraction and insertion operators. Data Member sign char dollars short cents char 2/18/2016 Table 1. Attribute Table for Money Type Purpose Indicates whether or not the Money instance is positive or negative. “$0.00” is treated as a positive value. (sign has one of two values, ‘+’ or ‘-’). The dollar amount of a Money instance. A nonnegative integer. The cents amount of a Money instance. A nonnegative integer. © David A. Workman Page 3 COP 4232 Method +Money +Money +Money +getSign +getDollars +getCents +operator- Lab1: OO Data and Exception Handling Table 2. Method Table for Money Profile Purpose and Functional Requiremenst () Default constructor. Yields +0.00 ( ifstream& ) Boundary constructor. Reads ASCII values of the throw(AppError) form: “$dd…d.cc” or “$(dd…d.cc)” as described above. Note: “$(0.00)” is an illegal value. An AppError exception shall be raised if the external image is syntactically incorrect. ( char Sign, short Dollars, char Cents) throw(AppError) (): char (): int (): int (): Money throw(AppError) +operator- ( Money RightOp ): Money +operator+ ( Money RTop ): Money +operator== ( Money RTop ): bool +operator< ( Money RTop ): bool ( Money RTop ): bool ( Money RTop ): bool ( Money RTop ): bool (ifstream & fin): void throw( AppError ) +operator> +operator<= +operator>= +Extract 2/18/2016 Summer 2005 Parametric constructor. An AppError exception shall be raised if Sign == ‘-’ when both Dollars and Cents are zero, or any of the following constraints are violated: Dollars < 0, Cents < 0 or Cents > 99. Inspector for the implied data member. Inspector for the implied data member. Inspector for the implied data member. Unary negation operator on Money. Returns the negative equivalent of the given object, provided the object is not +0.00 – an AppError exception is thrown otherwise. The given object is unchanged. Binary subtraction operator on Money. Returns the difference of the two Money operands. Both operands are unchanged. Binary addition operator on Money. Returns the sum of the two Money operands. Both operands are unchanged. Returns true if and only if the Sign, Dollars and Cents members of both operands are identical; returns false otherwise. Returns true if the left operand is less than the right operand; returns false otherwise. Returns true if the left operand is greater than the right operand; returns false otherwise. Returns true if the left operand is less than or equal to the right operand; returns false otherwise. Returns true if the left operand is greater than or equal to the right operand; returns false otherwise. Virtual boundary effector: extracts an image of a Money value from the input stream (fin) in conformance with the OO Data protocol. An appropriate exception is thrown if the input image violates the format described above. Note: Extract reads and validates the opening token “$” – there © David A. Workman Page 4 COP 4232 Method Lab1: OO Data and Exception Handling Summer 2005 Profile Purpose and Functional Requiremenst is no closing token. #Get (ifstream & fin): Virtual boundary effector: extracts the data void members of a Money value from the input stream throw( AppError ) (fin) in conformance with the OO Data protocol. An appropriate exception is thrown if the input image violates the format described above. Note: Get reads and validates the sign, dollars and cents. +Insert (ostream& fout): Virtual boundary output: inserts a Money image void into the output stream, (fout) in accordance with the OO Data protocol. Note: it writes the opening token, “$”. #Put (ostream& fout): Virtual boundary output: inserts a Money image void into the output stream, (fout) in accordance with the OO Data protocol. Note: it writes the data members. NOTE: Operators != is automatically provided by default in C++. Table 3. Table of Friends and Neighbors (Functions) for Money Type Purpose (ifstream& fin, friend: the extraction operator. Calls the Money& value): virtual method Extract(). ifstream& throw(AppError) operator<< (ostream& fout, friend: the insertion operator. Calls the Money& value): virtual method Insert(). ostream& operator* ( int Left, Money Right): neighbor function: left scalar multiplication Money by an integer for Money. operator* ( Money Left, int Right): neighbor function: right scalar Money multiplication by an integer for Money. NOTE: friends are declared inside the class declaration, neighbors (functions) are declared outside the class declaration in the header file for Money. Data Member operator>> 2/18/2016 © David A. Workman Page 5