// fraction.py // Walker M. White (wmw2) // October 7, 2012 /** * Instance is a Fraction n/d * * The purpose of this class is to illustrate the differences between * Python and Java classes */ class Fraction { // ALL functions must be a method inside of a class. private int numerator = 0; // must be an int private int denominator = 1; // must be an int > 0 // PROPERTIES /** Fraction numerator (getter) */ public int getNumerator() { return this.numerator; // returns the field } /** Fraction numerator (setter) */ public void setNumerator(int value) { // assign to field this.numerator = value; } /** * Fraction denominator (getter) * Invariant: Must be an int > 0. */ public int getDenominator() { return this.denominator; // returns the field } /** * Fraction denominator (setter) * Invariant: Must be an int > 0. */ public void setDenominator(int value) { // assign to field assert value > 0 : value+" is not positive"; // Types cannot catch everything. this.denominator = value; } /** * Constructor: Creates a new Fraction n/d * Precondition: d > 0 */ public Fraction(int n,int d) { this.setNumerator(n); this.setDenominator(d); } /** Returns: this Fraction as a string 'n/d' */ public String toString() { return this.getNumerator()+"/"+this.getDenominator(); } /** * Returns: Product of this and other as a new Fractions * Does not modify contents of self or other */ public Fraction mul(Fraction other) { int top = this.getNumerator()*other.getNumerator(); int bot = this.getDenominator()*other.getDenominator(); return new Fraction(top,bot); } /** * Returns: Sum of this and other as a new Fraction * Does not modify contents of self or other */ public Fraction add(Fraction other) { int bot = this.getDenominator()*other.getDenominator(); int top = (this.getNumerator()*other.getDenominator()+ this.getDenominator()*other.getNumerator()); return new Fraction(top,bot); } /** * Returns: True if this, other are equal Fractions. * False if not equal, or other not a Fraction */ public boolean equals(Object other) { if (!(other instanceof Fraction)) { return false; } // Must change static type to agree with dynamic type Fraction frac = (Fraction)other; // Cross multiply int left = this.getNumerator()*frac.getDenominator(); int rght = this.getDenominator()*frac.getNumerator(); return left == rght; } /** * Returns: a negative integer if this < other, * zero if this == other, * a positive integer if this > other * * Used to implement comparison operations. */ public int compareTo(Fraction other) { // Cross multiply int left = this.getNumerator()*other.getDenominator(); int rght = this.getDenominator()*other.getNumerator(); return left - rght; } /** * Normalizes this fraction so that numerator * and denominator have no common divisors. */ public void reduce() { int g = this.gcd(this.getNumerator(),this.getDenominator()); this.setNumerator(this.getNumerator()/g); this.setDenominator(this.getDenominator()/g); } /** * Returns: Greatest common divisor of x and y. * This was a function; is now a hidden method. */ private int gcd(int a, int b) { while (b != 0) { int t = b; b = a % b; a = t; } return a; } }