// // RationalNumbers1.java // Driver to exercise the use of multiple Rational objects // and test the use of the compareTo method. // import Rational1; public class RationalNumbers1 { //----------------------------------------------------------------- // Creates some rational number objects and performs various // operations on them. //----------------------------------------------------------------- public static void main (String[] args) { Rational1 r1 = new Rational1 (6, 8); Rational1 r2 = new Rational1 (1, 3); System.out.println ("First rational number: " + r1); System.out.println ("Second rational number: " + r2); if (r1.equals(r2)) System.out.println ("r1 and r2 are equal."); else System.out.println ("r1 and r2 are NOT equal."); Rational1 r3 = r1.add(r2); Rational1 r4 = r1.subtract(r2); Rational1 r5 = r1.multiply(r2); Rational1 r6 = r1.divide(r2); System.out.println ("r1 + r2: " + r3); System.out.println ("r1 - r2: " + r4); System.out.println ("r1 * r2: " + r5); System.out.println ("r1 / r2: " + r6); if (r3.compareTo(r4) == 0) System.out.println ("r3 and r4 are equal"); else if(r3.compareTo(r4) < 0) System.out.println ("r3 is less than r4"); else System.out.println ("r3 is larger than r4"); if (r4.compareTo(r5) == 0) System.out.println ("r4 and r5 are equal"); else if(r4.compareTo(r5) < 0) System.out.println ("r4 is less than r5"); else System.out.println ("r4 is larger than r5"); if (r5.compareTo(r6) == 0) System.out.println ("r5 and r6 are equal"); else if(r5.compareTo(r6) < 0) System.out.println ("r5 is less than r6"); else System.out.println ("r5 is larger than r6"); } }