public class Fraction { private int num, dem; // the numerator and deminator of the fraction public Fraction() // default constructor { num = 0; dem = 1; } public Fraction(int n, int d) // constructor with two arguments { num = n; dem = d; } public Fraction add(Fraction f) { Fraction sum = new Fraction(); sum.num = (num * f.dem) + (f.num * dem); sum.dem = dem * f.dem; return sum; } public Fraction divide(int n) // divide the fraction by the number { Fraction temp = new Fraction(); temp.num = num; temp.dem = n * dem; return temp; } public Fraction reduce() // reduce fraction to lowest terms { int gcd; if (num > dem) gcd = greatestCommonDivisor(num, dem); else gcd = greatestCommonDivisor(dem, num); Fraction temp = new Fraction(); temp.num = num/gcd; temp.dem = dem/gcd; return temp; } private int greatestCommonDivisor(int a, int b) { if (a % b == 0) //base case return b; else return greatestCommonDivisor(b, a%b); } public String toString() { return num + "/" + dem; } }