UE CS 215 - Project 1 - CS 215 - Fundamentals of Programming I

Unformatted text preview:

CS 215 - Fundamentals of Programming IISpring 2008 - Project 120 pointsOut: January 23, 2008Due: January 30, 2008Reminder: Programming Projects (as opposed to Homework problems) 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):● the denominator must not be 0● the ratio must be in reduced form. For value 0, the denominator is 1; for non-zero rational numbers the greatest common divisor of numerator and denominator is 1. For example, a 01/22/08 1 of 5Rational created with initial values 6/8 will be reduced to 3/4. Note this means that all integer values 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: non-zero value numerator and denominator have been reduced; 0 value denominator is 1Analysis Objects Type Kind Movement Nameinitial numerator int variable received initialNumeratorinitial denominator int variable received initialDenominatorNotes: the greatest common divisor (GCD) of two non-zero integers m and n may be computed using Euclid's recursive algorithm:● if n <= m and n divides m, then GCD(m, n) is n ● if n > m, then GCD(m, n) is GCD(n, m) ● otherwise GCD(m, n) is GCD(n, the remainder of m divided by n) ● 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 Objects 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 01/22/08 2 of 5Objects 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.● LeastCommonDenominator - Returns the least common denominator of this Rational and another RationalAnalysis Objects Type Kind Movement Nameanother Rational Rational variable received aRationalleast common denominator int variable returned -----Notes: The least common denominator is the least common multiple (LCM) of the two denominators. The LCM of two non-zero integers a and b can be computed using the greatest common divisor (GCD) as follows: LCM (a, b) = ab/GCD(a,b).● 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".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 -----01/22/08 3 of 5● 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 Objects 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 is reduced for non-zero Rationals, denominator is 1 for value 0.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 it just writes "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. 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


View Full Document

UE CS 215 - Project 1 - CS 215 - Fundamentals of Programming I

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 Project 1 - CS 215 - Fundamentals of Programming I
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 Project 1 - CS 215 - Fundamentals of Programming I 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 Project 1 - CS 215 - Fundamentals of Programming I 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?