Assignment #1: Simple Classes

Implement a very minimal BigRational class. A BigRational stores a numerator and a denominator. For the purposes of this assignment, you may assume that there are no negative numbers, no zero denominators, etc. For this assignment you need to only implement the following methods: The internal data representation consists of two java.math.BigInteger objects representing the numerator and denominator, respectively. The rational number is always kept in reduced form.

Sample test program

class Assign1
{
    public static void main( String [ ] args )
    {
        BigRational r1 = new BigRational( "123456789023466" );         // Denominator is 1
        BigRational r2 = new BigRational( "1234567890234660", "10" );
        BigRational r3 = new BigRational( "3", "4" );
        BigRational r4 = new BigRational( "2", "3" );
        BigRational r5 = r3.add( r4 );                                 // 17/12
        BigRational r6 = new BigRational( "10", "120" );               // 1/12

        System.out.println( r1.equals( r2 ) );   // Should be true!
        System.out.println( r5.add( r6 ) );      // 3/2
        System.out.println( r5 );                // 17/12
    }
}

BigRational Sketch

Here is a sketch of BigRational.
import java.math.BigInteger;

public class BigRational
{
    /*
     * Constructor, assumes no funny stuff, so no error checks.
     * Reduces to lowest form by dividing numerator
     * and denominator  by their gcd.
     */
    public BigRational( String n, String d )
    {
        numerator = new BigInteger( n );
        denominator = new BigInteger( d ); 
        reduce( );
    }

    private void reduce( )
    {
        BigInteger gcd = numerator.gcd( denominator );
        numerator = numerator.divide( gcd );
        denominator = denominator.divide( gcd );
    }


    // Additional constructors at your discretion

    /*
     * Returns a new BigRational representing this+other
     * Make sure to reduce
     */
    public BigRational add( BigRational other )
    {
    }

    public String toString( )
    {
    }

    public boolean equals( Object other )
    {
    }

    private BigInteger numerator;
    private BigInteger denominator;
}