/* CPSC 215 Lab 2, 9/9/99 Due Thursday 9/16/99 This lab asks you to implement a package for computing with rational numbers in Java. You may work with a PARTNER, but ALTERNATE typing in the solution for the different parts of the assignment. Given the following program skeleton to start with, you are to implement the following functions. Don't just implement one after the other, study the problems and decide on the best order to proceed. 1. Euclid's GCD algorithm. If you implment it correctly, you'll notice that it works even for negative numbers. Use Euclid's algorithm to implement a function that reduces fractions. 2. Modify the "toString" method so that it prints rationals in the following manner: 4/1 should be printed as "4", 0/2 should be "0" 9/2 should be printed as "4+1/2" 1/2 should still be printed as "1/2", NOT "0+1/2". At no time can non-reduced numbers be printed (there's a simple way to make sure of this). 3. Define another constructor method for rationals that takes 3 arguments, the whole part and the fraction parts. Do NOT, however, introduce another variable into the rational class in addition to numerator and denominator. 4. The "least common multiple" (lcm) of two numbers can also be computed using the gcd. The algorithm is as follows: given the numbers A and B, let C be their gcd. the lcm of A and B is A*(B/C). Implement the lcm function. You'll need this for rational addition. 5. Implement rational addition and multiplication, making sure that the resulting rational is reduced. Also implement methods to test for rational equality and inequality. (remember how to cross-multiply?) 6. Modify the algorithm you wrote last week for sorting an array of integers into an algorithm that sorts an array of rationals, using the rational inequality method. (This is a simple change). 7. Test and demonstrate your functions in the 'main' method. You can just make up the numbers; you don't have to take any input. */ /* ----- Program Skeleton ----- */ /* Note the organization of this program: the "rational" class is nested inside the "fractions" class. This allows methods in "rational" to access methods in "fractions" directly, (e.g, without calling fractions.gcd). It also allows any program importing "fractions" to also import "rational". */ public class fractions { /* Complete the implementation of Euclid's Algorithm here: */ public static int gcd(int a, int b) // note that this method is 'static' public static int lcm(int a, int b) // least common multiple public static void main(String[] args) // demonstrate here { rational a, b, c; a = new rational(9,24); b = new rational(4,2); c = new rational(6,16); System.out.println(" b + c = " + b.add(c)); // etc ... } public static class rational // note nested class definition. This allows { private int numerator = 0; // "rational" to be public with the same file. private int denominator = 1; rational(int n, int d) // constructor { numerator = n; if (d != 0) denominator = d; else denominator = 1; } // alternative constructor with three arguments rational(int whole, int n, int d) % You'll have to modify this: public String toString() // allows printing by System.out.print { return (numerator + "/" + denominator); } /* use the gcd function to reduce the fraction: */ public void reduce() /* implement rational addition, multiplication, equality and inequality: */ public rational add(rational r) public rational mult(rational r) public boolean equals(rational r) // note this is not static { return (numerator*r.denominator)==(denominator*r.numerator); } public boolean lessthan(rational r) // r.lessthan(s) if r < s } // end of class rational } // end of class fractions