DOC PREVIEW
UMD CMSC 131 - Lecture 16: Rational Numbers

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 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 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)Lecture 16:Rational NumbersLast time:1.Aliasing and Mutability2.Floating Point calculationsToday:1.Example class development: Rational NumbersCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)1Definition of a Rational NumberWhat is a rational number?As a decimal it either terminates or repeats a pattern:1.750.242935353535As a fraction, it can be represented as a fraction of two integers.CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)2Today we will start an extended exampleWe will implement a class, Rational, for (immutable) rational numbersThe class will includeConstructorsArithmetic operations (+, -, *, /)toStringComparisons (equals, compareTo)CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)3“Lowest Terms”?How do we represent the fraction 20/60 ? Reduce to lowest terms.Given a fraction p/q, how do you put it into lowest terms?MethodFind greatest common divisor (gcd) of p, qgcd of p, q: largest number that divides both p, qEuclid’s algorithm (beyond scope of this lecture) performs this if p, qare both positiveReplace p/q by (p/gcd) / (q/gcd)ExampleConsider 18/24gcd of 18 and 24 is 6So 18/24 = (18/6) / (24/6) = 3/4CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)4HintsCome up with representative test casesIntertwine implementation and testingDo constructors and getters first, then testImplement “related operations”, then testRerun each test (even ones for previously tested methods) when you testThis is called regression testingUseful for detecting changes that may invalidate previous test results!Easy to set up in EclipseUse debugger to track down sources of errors in testsCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)5Rational Numbers (continued): Arithmetic OperationsWhat you remember from middle / high schoolp/q + s/t = (p*t + q*s) / (q*t)p/q * s/t = (p*s) / (q*t)p/q – s/t = p/q + (-s/t)1/(p/q) = q/p(p/q) / (s/t) = p/q * t/s = p/q * (1/(s/t))We will focus on theseWe will focus on theseWe will focus on theseWe will focus on thesetwo cases.two cases.two cases.two cases.CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)6Comparisonsp/q = s/t ifp/q, s/t are in lowest terms, andp = q and s = tp/q < s/t if p*t < q*sWe will focus on this case.We will focus on this case.We will focus on this case.We will focus on this


View Full Document

UMD CMSC 131 - Lecture 16: Rational Numbers

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 16: Rational Numbers
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 Lecture 16: Rational Numbers 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 Lecture 16: Rational Numbers 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?