import java.util.*; public class Rational { //***************************************** Fields: *****************************************\\ private long n, d; //*************************************** Constructors: **************************************\\ // 297: Danger! d is 0. Not good. public Rational() { this.n = 0; this.d = 0; } // 297: Can d = 0? Yes, if some idiot enters it. // 297: Error checking is your friend. Good. public Rational(long numerator, long denominator) { n = numerator; d = denominator; if (d == 0) {throw new IllegalArgumentException("Denominator is 0; D.N.E."); } else { reduce(); } } public Rational (String rat) { // 297: Bad Sarah! StringTokenizers are "discouraged" StringTokenizer tokens = new StringTokenizer(rat, "/"); if (tokens.countTokens() == 1) { n = Long.parseLong(tokens.nextToken()); d = 1; } else { //Define numerator and denominator n = Long.parseLong(tokens.nextToken()); d = Long.parseLong(tokens.nextToken()); if (d == 0) {throw new IllegalArgumentException("Denominator is 0; D.N.E."); } else { reduce (); } } } //************************************ Instance methods: ***********************************\\ public String toString(){ if (d==1) { return String.valueOf(n); } else if ((n<0)||(d<0)) { // 297: Sort of strange. Does Sarah mean and? n *= -1; d *= -1; return new String (n+"/"+d); } else { return new String(n+"/"+d); } } public long getNumerator() { return n; } public long getDenominator() { return d; } public Rational add(Rational r) { long n1 = (r.getNumerator()*d) + (n*r.getDenominator()); long d1 = (r.getDenominator()*d); return new Rational(n1,d1); } public Rational subtract(Rational r) { long n1 = (n*r.getDenominator()) - (d*r.getNumerator()); long d1 = d*r.getDenominator(); return new Rational(n1,d1); } public Rational multiply(Rational r) { long n1 = n*r.getNumerator(); long d1 = d*r.getDenominator(); return new Rational(n1,d1); } public Rational divide(Rational r) { if (r.getNumerator()==0||r.getDenominator()==0||n==0||d==0) throw new ArithmeticException("Division by zero!"); long n1 = n * r.getDenominator(); long d1 = d * r.getNumerator(); return new Rational (n1,d1); } public void reduce() { long gcd = 1; long big = 0; long little = 0; long temp = 0; /* if (n < d) { big = d; little = n; } else if (d < n) { big = n; little = d; } else if (n == d) { big = d; little = n; } */ if (n <= d) { big = d; little = n; } else { big = n; little = d; } while (little!=0) { temp = big; big = little; little = temp%little; } gcd = big; n /= gcd; // 297: Shorthand for n = n/gcd; d /= gcd; // 297: If both numerator and denominator are negative, // 297: make them both positive. // 297: If denominator is negative and numerator is positive, // 297: multiply both by negative 1. // 297: The two previous statements simplify to // 297: If denom. is negative, mutliply num. and denom by -1. } public int compareTo (Rational t) { long cn1 = t.getDenominator() * n; long cn2 = d * t.getNumerator(); // long commond = r2.getDenominator() * d; if (cn1