The goal for the assignment was to write a Java application for performing arithmetic with fractions or rational numbers. Rational numbers were represented with integer numerators and integer denominators. First the class called Rational was designed with two instance variables, one for numberator num and one for denominator den. Two constructors were designed one which takes in a numerator and a denominator and another one which does not take any arguments. Once the constructors were done, then the get and set methods for the instance variables were written. Since the variables were made private, objects and methods outsite this class will have to go through the get and set methods to modify the values of the variables. The methods for the various arithmetic functions were written. Each of these functions takes two rational objects and does the operation. The sum, subtract and multiply operations returns a rational object while the compare method returns an integer. The reduce method for reducing the values into the lowest values was written with the help of the gcd method which calculates the greatest common denominator. Finally a test stub in the form of a main function was also written to test these functions.
Once the class was tested another class called RationalTest was written with the help of Console.java file downloaded from the class resources. This test application prompts for entering the rational numbers, gets the numbers with the help of the Console.readInt function and puts them into an array of Rational objects. It then calls two other functions present in this function which operates on an array of Rational objects. One function finds the sum of the elements and another one finds the maximum of the elements. It prints out the result onto the console window. These functions actually makes use of the arithmetic functions in the Rational class which are defined as static functions. Static functions are helpful because they don't need to be instantiated before using them.
Java Source and Output