UE CS 215 - CS 215 ­ Fundamentals of Programming II

Unformatted text preview:

CS 215 - Fundamentals of Programming IISpring 2011 - Project 220 pointsOut: February 2, 2011Due: February 9, 2011Reminder: Programming Projects (as opposed to Homework exercises) are to be your own work. See syllabus for definitions of acceptable assistance from others.The instructor's format for class specifications is defined in the on-line handouts An Analysis and Design Style Guideline and An Analysis and Design Style Guideline for Classes. (They are used by the instructor in CS 210.) Links to these handouts also is provided on the CS 215 home page. Problem StatementA rational number is a number that can be expressed as an integer or the quotient of an integer divided by a nonzero integer. Expressed as a/b, a is called the numerator and b is called the denominator. Results of the binary arithmetic operations on rational numbers represented by a/b and c/d are as follows:Operator Resulta/b + c/d (ad + bc) / bda/b – c/d (ad - bc) / bda/b * c/d ac / bda/b / c/d ad / bc, where c not equal to 0In addition, a/b = c/d when ad = bc. From this relationship, the equality and relational operations can be defined.Consider the following specification for a Rational class that models a rational number. The analysis of each operation is given formally, but the design of each operation is given informally in English.Specification for Rational Class Attributes: Objects Type Namenumerator of the Rational int numeratordenominator of the Rational int denominatorThe class invariant is as follows (i.e., all valid objects must meet these conditions):Revised: 02/02/2011 Page 1 of 6 D. Hwang● The denominator must be greater than 0. For example, a Rational created with initial values 3/-4 will be stored as -3/4, and a Rational created with initial values -3/-4 will be stored as 3/4.● The ratio must be in reduced form. That is, the greatest common divisor of the numerator and denominator is 1. This can be achieved by dividing both the numerator and denominator by the greatest common divisor. For example, a Rational created with initial values 6/8 will be reduced to 3/4. Note this means that all integer values (including 0) have denominators of 1.Operations ● Explicit-value constructor - initialize attributes from passed values Must have default argument values of 0 and 1 (i.e. value 0)Precondition: Initial denominator must be non-zero. Use assert() to check this.Post condition: ratio conforms to the class invariant as described aboveAnalysis Objects Type Default Movement Nameinitial numerator int 0 received initialNumeratorinitial denominator int 1 received initialDenominatorNotes: the greatest common divisor of two positive integers m and n may be computed using Euclid's algorithm. Here is an iterative version of this algorithm:1. If m < n then swap values so that n < m2. While n > 0 do2.1. Save the value of n2.2. Recompute n as the remainder of m divided by n2.3. Set m to the saved value of n3. m is now the greatest common divisor ● GetNumerator - Returns numerator of this Rational Analysis Objects Type Movement Namenumerator of Rational int returned numerator● GetDenominator - Returns denominator of this Rational Analysis Revised: 02/02/2011 Page 2 of 6 D. HwangObjects Type Movement Namedenominator of Rational int returned denominator● Reciprocal - Returns a Rational that is the reciprocal of this Rational. Precondition: This Rational must not be value 0. Use assert() to check this.Analysis Objects Type Movement NameReciprocal Rational Rational returned -----The reciprocal of this Rational is the Rational that produces value 1 when multiplied with this Rational.● AsMixedFraction - Returns a string that is this Rational in the form "i+n/d" (no spaces) where i is an integer and n < d. For example, 8/3 would return a string "2+2/3". If n = 0, then write just "i". If i = 0 then write just "n/d". Hint: you should use an ostringstream to do this. See design notes below.Analysis Objects Type Movement Namemixed fraction string returned -----● friend overloaded arithmetic operator functions: operator+, operator-, operator*, operator/Returns a Rational object whose value is the result of performing the appropriate operation.Precondition for operator/: The right operand must not be value 0. Use assert() to check this.Analysis Objects Type Movement Namea Rational Rational received leftOperandanother Rational Rational received rightOperandresult of computation Rational returned -----● friend overloaded equality and relational operator functions: operator==, operator!=, operator<, operator<=, operator>, operator>=Returns true if the relationship between the left and right operands is true, false otherwiseRevised: 02/02/2011 Page 3 of 6 D. HwangAnalysis Objects Type Movement Namea Rational Rational received leftOperandanother Rational Rational received rightOperandresult of comparison bool returned -----● operator>> - friend overloaded operator function that reads Rational values from an input stream without prompting in format "n/d" (no spaces) or just "n" and store appropriate values in the attributes. Must check that the input denominator is not 0. If so, the Rational object is unchanged and the input stream should be put in the failed state and returned. Also, if the input stream fails for any reason, it should just be returned. See design notes below.Post condition: ratio conforms to the class invariant as specified above.Analysis Objects Type Movement Nameinput stream istream received, passed back, returned ina Rational Rational passed back targetNote: the operation in.peek() will return the next character in the input stream without removing it from the stream.● operator<< - friend overloaded operator function that writes Rational to an output stream in format "n/d" (no spaces), except when d = 1, then write just "n".Analysis Objects Type Movement Nameoutput stream ostream received, passed back, returned outa Rational Rational received sourceAssignment Write the implementation for this Rational class. Note that the class and member function names must be as specified above, including case. All other identifiers must have consistent case usage, but do not have to match the style of these names. The Rational class definition and friend operator function prototypes are to be put in header file rational.h with suitable compilation guards. The implementations of the Rational class and friend operator functions are to be put in source file rational.cpp.


View Full Document

UE CS 215 - CS 215 ­ Fundamentals of Programming II

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download CS 215 ­ Fundamentals of Programming II
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 215 ­ Fundamentals of Programming II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 215 ­ Fundamentals of Programming II 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?