CSC153 2004S, Class 35: Rational Numbers
Admin:
* Quick survey!
* Homework questions at end of class.
* Please be nice to the prospies.
Overview:
* Review: Java
* Review: Rational.java
* Computing gcd
* Doing math
* Testing
* Constructors
* HW5
Review:
* Java is an object-oriented language
* In Java, you describe objects with *classes*
* Classes have three parts:
* Fields: Values associated with the object (like variables)
* Constructors: Instructions for building new objects (by filling in the fields)
* Methods: What you can do with the objects
* Classes can have destructors, but they are not necessary since Java manages dynamic memory for you.
* We tend to think of two kinds of methods
* Mutators: Change things (bang methods)
* Observers: Look at values, but not change them (everything else)
* Fields and methods can also be *static*: Associated with the class rather than with individual objects
* For non-static (object) methods, you must first create an object in order to call them.
* To call a object method:
nameofobject.methodname(params)
* To call a class method:
NameOfClass.methodname(params)
Review: A Rational Class
* Stored in Rational.java
* Observation: Simplification important
Writing GCD: Look at the works of Euclid
* gcd of x and 0 is: x
* Assume x >= y: gcd of x and y is the gcd of y and x mod y
* Why?
gcd(x,y) divides x
gcd(x,y) divides y
Therefore gcd(x,y) divides gcd(y, x mod y)
Let x mod y = q, then exists c such that x = y*c + q
If exists d that divides x, d divides y, then d divides q