DOC PREVIEW
UE CS 215 - CS 215 ­ Fundamentals of Programming II

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 215 - Fundamentals of Programming IISpring 2010 - Project 120 pointsOut: January 27, 2010Due: February 3, 2010Reminder: 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):01/26/2010 Page 1 of 5● 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. Throws RangeError exception with a suitable error message if not met.Post condition: ratio conforms to the class invariant as described aboveAnalysis Objects Type Kind Movement Nameinitial numerator int variable received initialNumeratorinitial denominator int variable received initialDenominatorNotes: the greatest common divisor of two non-zero 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 Kind Movement Namenumerator of Rational int variable returned numerator● GetDenominator - Returns denominator of this Rational Analysis 01/26/2010 Page 2 of 5Objects Type Kind Movement Namedenominator of Rational int variable returned denominator● Reciprocal - Returns a Rational that is the reciprocal of this Rational. Precondition: This Rational must not be value 0. Throws RangeError exception with a suitable error message in this caseAnalysis Objects Type Kind Movement NameReciprocal Rational Rational variable returned -----The reciprocal of this Rational is the Rational that produces value 1 when multiplied with this Rational.● WriteAsMixedFraction - Write this Rational to an output stream in the form "i+n/d" (no spaces) where i is an integer and n < d. For example, 8/3 would be written as 2+2/3. If n = 0, then write just "i". If i = 0 then write just "n/d".Analysis Objects Type Kind Movement Nameoutput stream ostream variable received, passed back out● friend overloaded arithmetic operator functions: operator+, operator-, operator*, operator/Returns a Rational object whose value is the result of performing the appropriate operation.operator/ throws RangeError exception with a suitable error message if the right operand is 0Analysis Objects Type Kind Movement Namea Rational Rational variable received leftOperandanother Rational Rational variable received rightOperandresult of computation Rational variable 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 otherwiseAnalysis 01/26/2010 Page 3 of 5Objects Type Kind Movement Namea Rational Rational variable received leftOperandanother Rational Rational variable received rightOperandresult of comparison bool variable 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 in attributes. Must check that the input denominator is not 0. If so, the Rational object is unchanged and a RangeError exception is thrown with a suitable error message. Post condition: ratio conforms to the class invariant as specified above.Analysis Objects Type Kind Movement Nameinput stream istream variablereceived, passed back, returnedina Rational Rational variable passed back aRationalNote: 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 Kind Movement Nameoutput stream ostream variablereceived, passed back, returnedouta Rational Rational variable received aRationalAssignment Write the implementation for this Rational class. Note that the function names for this class must be as specified above (including case). The Rational class definition and friend operator function prototypes should be put in header file rational.h with suitable compilation guards. The implementations of the Rational class and friend operator functions should be put in source file rational.cpp. Write a main program that adequately tests your Rational class in file testrational.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?