Transcript Chap. 4b

Class Example - Rationals
Rational numbers are represented by the ratio of two
integers, a numerator and a denominator, e.g., 2/3.
This is opposed to irrational numbers which cannot
be expressed as the ratio of two integers, such as Pi.
Rational numbers are usually put into lowest terms,
which is done by dividing the numerator and
denominator by the greatest common denominator
(GCD) of both: 4/6 -> 2/3.
Rationals (cont'd)
Rational numbers are not a built-in type like ints
and doubles. Suppose that we what to work with
them as a new type. We can create a new
Rational class that represents rational numbers
and incorporates the usual operators that we apply
to numbers: add, subtract, multiply, and divide. In
addition, we want to be able to read and write
rational numbers in human-readable form.
Rational Methods
The arithmetic operators all take two operands (both
rational) and return a third value (also a rational).
When we implement this, one of the operands will
be the object that the method is called upon and the
second operand will be explicitly passed as an input
parameter to the method. The return value will be
used to return the result. For example, to add two
rational numbers together we say:
c = a.add(b);
Rational Operators
n1/d1 + n2/d2 = (n1 * d2 + n2 * d1) / (d1 * d2)
 n /d - n /d = (n * d - n * d ) / (d * d )
1 1
2 2
1
2
2
1
1
2
 n /d * n /d = (n * n ) / (d * d )
1 1
2 2
1
2
1
2
 n /d / n /d = (n * d ) / (n * d )
1 1
2 2
1
2
2
1

Representing Rational Numbers
To represent a rational number, we will need to store
the numerator and the denominator, both ints.
private int n, d;
We use the private modifier so that the parts cannot
be accessed from outside the Rational class
(information hiding).
A Rational Method
public Rational add(Rational b) {
Rational c = new Rational();
c.n = n * b.d + b.n * d;
c.d = d * b.d;
return c;
}
The variables n and d (which are not qualified) refer
to the values of the object upon which the add
methods is called. In the example
c = a.add(b);
the add method is called on object a, so n and d
refer to a's fields.
Other Methods
public Rational subtract(Rational b) {
Rational c = new Rational();
c.n = n * b.d - b.n * d;
c.d = d * b.d;
return c;
}
public Rational multiply(Rational b) {
Rational c = new Rational();
c.n = n * b.n;
c.d = d * b.d;
return c;
}
Other Methods (cont'd)
public Rational divide(Rational b) {
Rational c = new Rational();
c.n = n * b.d;
c.d = b.n * d;
return c;
}
Rational Class
public class Rational {
private int n,d;
public Rational add(Rational b) { ... }
public Rational subtract(Rational b) { ...}
public Rational multiply(Rational b) { ... }
public Rational divide(Rational b) { ... }
}
Additional Methods
For the time being we will also write two additional
methods so we can test our class:
public void set(int n0, int d0) {
n = n0; d = d0;
}
public String toString() {
return n + "/" + d;
}
Testing the Rational Class
The following main program will test the methods of
the Rational class:
public class Main2 {
public static void main(String args[]) {
Rational a = new Rational();
Rational b = new Rational();
a.set(1,3);
b.set(2,3);
Rational c = a.add(b);
System.out.println(c);
}
}
The result of this program is:
9/9
Oops!
What have we forgotten? The rational number should
be represented in lowest terms, so 9/9 isn't quite
right. The GCD of the numerator and the
denominator is 9, so dividing both by the GCD 9,
yields 1/1, which is the rational number in lowest
terms.
Calculating the GCD
Finding the GCD is one of the oldest known
algorithms, ascribed to Euclid (hence Euclid's
algorithm). We find the GCD by repeatedly taking
the remainder of one number divided by the other
until one of the terms becomes 0. The other term is
the gcd. This works because the gcd(x,y) = gcd(x, x
% y).
Euclid's Algorithm
private int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
Reduce method
To avoid writing the same code over and over again,
we also write a reduce method, that takes a Rational
number, reduces it to lowest form, and returns it:
private Rational reduce() {
int g = gcd(n,d);
n /= g;
d /= g;
return this;
}
Reduce method (cont'd)
Two things to note:

Both reduce and gcd are private methods. That is,
they can only be called by other methods of the
Rational class, and not from any other class.

reduce returns this, which refers to the
(anonymous) object upon which reduce was called.
New add method
public Rational add(Rational b) {
Rational c;
c.n = n * b.d + b.n * d;
c.d = d * b.d;
return c.reduce();
}