package Chapter1.Section2;

import edu.princeton.cs.algs4.*;

public class Rational
{
    private final int numerator, denominator;

    public Rational(int numerator, int denominator)
    {
	this.numerator = numerator / gcd(numerator, denominator);
	this.denominator = denominator / gcd(numerator, denominator);
    }

    public int numerator() { return numerator; }
    public int denominator() { return denominator; }

    public Rational plus (Rational b)
    {
	return sum(b, 1);
    }

    public Rational minus (Rational b)
    {
	return sum(b, -1);
    }

    private Rational sum(Rational b, int multiplyBit)
    {
	int newA = numerator * b.denominator();
	int newB = b.numerator * denominator();
	
	int newNum = newA + newB * multiplyBit;
	int newDom = denominator * b.denominator();

	return new Rational(newNum, newDom);
    }

    public Rational times (Rational b)
    {
	return new Rational(
	    numerator * b.numerator(), 
	    denominator * b.denominator());
    }

    public Rational divides (Rational b)
    {
	return new Rational(
	    numerator * b.denominator(),
	    denominator * b.numerator());
    }

    public boolean equals (Rational that)
    {
	if (this == that) return true;
	if (that == null) return false;
	return
	    this.numerator() == that.numerator()
	    && this.denominator() == that.denominator();
    }

    public String toString()
    {
	return numerator + "/" + denominator;
    }

    private static int gcd (int p, int q)
    {
	if (q == 0) return p;
	int r = p % q;
	return gcd(q, r);
    }
}

@@ -574,55 +574,287 @@
 ate 
-Rational sum(Rational b, int multiplyBit)%0A    %7B
+void assertMults(int a, int b)%0A    %7B%0A%09assert (long)a * (long)b %3C Integer.MAX_VALUE;%0A    %7D%0A%0A    private Rational sum(Rational b, int multiplyBit)%0A    %7B%0A%09assertMults(numerator, b.denominator());%0A%09assertMults(b.numerator(), denominator);%0A%09assertMults(denominator, b.denominator());%0A
 %0A%09in
@@ -1103,32 +1103,117 @@
 tional b)%0A    %7B%0A
+%09assertMults(numerator, b.numerator());%0A%09assertMults(denominator, b.denominator());%0A%0A
 %09return new Rati
@@ -1334,32 +1334,117 @@
 tional b)%0A    %7B%0A
+%09assertMults(numerator, b.denominator());%0A%09assertMults(denominator, b.numerator());%0A%0A
 %09return new Rati

Add assertions