/** * @Homework: Assignment 3 * @Exercise: 6.3 in textbook, page 342 * @Author: Richard Parry * @Version: 1.0 - July 20, 1998 * @Filename: Rational.java * @School: UCSD, Summer 98 Java 1 class * * Description: * This program Rational accepts two rational numbers given * by the user and performs aritmetic operations: add, substract, multiply, * and divide. * * Usage: * r1 = new Ration(numerator, denominator); // create object * r2 = new Ration(numerator, denominator); // create object * * Ration result = Rational.add(r1, r2); // to add * Ration result = Rational.sub(r1, r2); // to subtract * Ration result = Rational.mul(r1, r2); // to multiply * Ration result = Rational.div(r1, r2); // to divide * r1.toString(); // convert to string **/ // declare class public class Rational { // private instance variable private int numerator, denominator; /** This is a "no-argument" Constructor Method with default values * in case no initialzers are provided. This ensure that each Rational * object starts in a consistent state (the default state). * @param none * @return none * @exception none **/ public Rational() { this.numerator = 1; this.denominator = 2; } /** * This is the constructor. It initializes the numerator and * denominator variables. **/ public Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } /** * An accessor method. Returns the numerator part of the rational number. * Note that there is no setNumerator() method to set the numerator part. * This means that the Rational class is "immutable". **/ public int getNumerator() { return numerator; } /** * An accessor method. Returns the demoninator part of the rational number. **/ public int getDenominator() { return denominator; } /** * This method converts a Ration number to a string. This is a method of * Object that we override so that rational numbers can be meaningfully * converted to strings, and so they can conveniently be printed out with * System.out.println() and related methods. **/ public String toString() { return numerator + "/" + denominator; } /** * This method accepts a rational number and converts a float (decimal) * If the numerator is zero, then force results to zero. This solves * the problem of divide by zero AND the special case problem of * 0/0. This is not an elegant solution, but is straight forward. **/ public static float rational2Decimal( Rational rationalNum ) { float floatNumerator = rationalNum.getNumerator(); float floatDenominator = rationalNum.getDenominator(); if ( floatNumerator == 0 ) { return 0; } else { return floatNumerator / floatDenominator; } } /** * Reduce method, is a method that accepts a rational number consisting * of a numerator and denominator and reduces it. **/ private static Rational reduce( Rational ration ) { int smallest, largest; int gcd = 1; // need to initialize // use accessor, to get numerator and denominator int n = ration.getNumerator(); int d = ration.getDenominator(); /** to reduce find smallest number then modulus divide * if no remainder, then see it will also divide into * the larger number, if so it is a possible candidate as * a divisor so save it. Continue looking and save the highest * value, since it will be the largest common denominator **/ smallest = Math.abs( Math.min(n, d) ); // no negative values largest = Math.max(n, d); for (int i = 2; i <= smallest; i++) { if( smallest % i == 0 ) { if( largest % i == 0 ) { gcd = i; // save greatest common denominator } } } // return reduced rational number Rational reducedNumber = new Rational( n/gcd, d/gcd ); return reducedNumber; } /** * This is a static method. It takes two Rational numbers, adds them, * and returns the result as a third number. Because it is static, there is * no "current instance" or "this" object. use it like this: * * Rational sum = Rational.add(r1, r2) * * where r1 and r2 are rational numbers consisting of a numerator * and denominator. Example above is for addition, similar * method calls are used for other arithmetic operations. **/ public static Rational add(Rational rat1, Rational rat2) { // add rational numbers int denom = rat1.denominator * rat2.denominator; int numer = rat1.numerator * rat2.denominator + rat2.numerator * rat1.denominator; // make new object for results, reduce, and return rational number Rational r3 = new Rational( numer, denom ); return r3.reduce( r3 ); } // Static method to substract public static Rational sub(Rational rat1, Rational rat2) { int denom = rat1.denominator * rat2.denominator; int numer = rat1.numerator * rat2.denominator - rat2.numerator * rat1.denominator; // for special case of zero numerator, force denominator to zero // I assume that 1/2 - 1/2 expressed as a rational number is 0/0 if ( numer == 0 ) { denom = 0; } // make new object for results, reduce, and return rational number Rational r3 = new Rational( numer, denom ); return r3.reduce( r3 ); } // Static method to multiple public static Rational mul(Rational rat1, Rational rat2) { int denom = rat1.denominator * rat2.denominator; int numer = rat1.numerator * rat2.numerator; // make new object for results, reduce, and return rational number Rational r3 = new Rational( numer, denom ); return r3.reduce( r3 ); } // Static method to divide public static Rational div(Rational rat1, Rational rat2) { int numer = rat1.numerator * rat2.denominator; int denom = rat1.denominator * rat2.numerator; // make new object for results, reduce, and return rational number Rational r3 = new Rational( numer, denom ); return r3.reduce( r3 ); } } // end class declaration