Held: Friday, 2 April 2004
Today we continue our design of the rational class.
- Please behave better when prospies are here.
- We'll leave questions on the homework until the end of class.
- Review: Java.
- Computing gcds.
- Arithmetic operations.
- Classes describe objects.
- Three core components:
- The fields or attributes that store characteristics of the object.
- The constructors that describe how to build new objects.
- The methods that allow us to gather information from objects and to otherwise manipulate them.
- We tend to separate methods into two main classes:
- Mutators, which change objects.
- Observers, which gather information but don't change the object.
- Fields and methods can also be static: Associated with the class as a whole, rather than individual objects.
- To call non-static methods and access non-static fields, you must create an object first. We often call such methods and fields object methods and object fields.
- To call static methods and access static fields, you need not create an object first. (You preface these with the name of the class.) We often call such mthods and fields class methods and class fields.
- Lots of fun documentation on existing classes can be found at
- Right before break, we started to design
Rational.java, a class to represent rational numbers.
- That class is not complete.
- It was also not compilable, but I added some stubs to make it compile.
- We've decided upon four constructors, written two of them, and sketched two more.
- We've written the simplify method, but need a method to compute greatest common divisors.
- We haven't written any of the computation methods.
- We haven't written the
- Euclid's method (for positive integers):
- The gcd of x and 0 is x.
- Assume y is less than or equal to x: The gcd of x and y is the gcd of y and x mod y.
- Proof of correctness?
- Note that this is particularly easy to implement.
- a/b + c/d = (ad + bc)/bd
- a/b * c/d = ac/bd
- Are there ways that we can reduce the chance of overflow?
- We now have built enough that we should start to write a testing class.