In: Computer Science
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs to support simple arithmetic functions including taking the sum, difference, and the product of the division of the two.
Do not include a main() method in the Fraction class.
The Fraction class will implement the following class methods:
Fraction add (Fraction f1, Fraction f2); // f1 + f2 and returns a new Fraction Fraction sub (Fraction f1, Fraction f2); // f1 - f2 and returns a new Fraction Fraction mul (Fraction f1, Fraction f2); // f1 * f2 and returns a new Fraction Fraction div (Fraction f1, Fraction f2); // f1 / f2 and returns a new Fraction
It will also implement the following instance methods:
Fraction(int n, int d); // constructor where *n* is numerator and *d* is the denominator Fraction reduce(); // Fraction is lowest common form String strVal(); // String representation, e.g., 3/4
if the denominator is 0, treat it as 1 for now until we cover exceptions in more depth during this course. If you're already comfortable with exceptions, then ignore the previous statement and throw an exception in the constructor if the given denominator is 0.
Note that you do not need to deal with mixed numbers. In other words, you can deal with fractions as fractions and do not need to deal with whole numbers plus fractional parts. Also, the Fraction class should work fine when the fraction is improper, i.e., when the numerator is >= the denominator.
The following is sample code from a set of unit tests or client program that uses the Fraction class.
// Client code Fraction f1 = new Fraction(1, 2); Fraction f2 = new Fraction(3, 2); Fraction sum = Fraction.add(f1, f2); Fraction whole = sum.reduce(); System.out.println(whole.strVal()); // More examples Fraction f3 = new Fraction(3, 4); Fraction f4 = new Fraction(1, 3); Fraction f5 = Fraction.sub(f3, f4); System.out.println(f5.strVal());
In order to reduce a fraction, you will need to find the Greatest Common Divisor (GCD) between the numerator and the denominator. To do this, you can use Euclid's algorithm. If you are already comfortable with recursion, you can use a recursive version. If not (we cover recursion later in this course), you can use the following to compute the GCD.
private int gcd(int num1, int num2) { int tmp; num1 = abs(num1); num2 = abs(num2); while (num1 > 0) { tmp = num1; num1 = num2 % num1; num2 = tmp; } return num2; }
Once you've computed the GCD, finding the reduced fraction is simple. Do an Internet search to figure it out.
Additionally, in order to add and subtract Fractions, you'll need to calculate the Least Common Denominator (LCD). Part of "doing software" is to find examples and information on the Internet. In order to calculate the LCD, you will need the Least Common Multiple (LCM) and the Greatest Common Divisor (GDC).
file-name: Fraction.java ------------------------------------------------ import static java.lang.Math.abs; /* Fraction class * It has two member variables 1. numerator 2.denominator *I have created gcd and LCM methods which help us in calculation fractions. */ public class Fraction { // instance variables int numerator; int denominator; // constructor Fraction( int n , int d) { this.numerator = n; if( d == 0) { d = 1; } this.denominator = d; } // add method public static Fraction add(Fraction f1, Fraction f2) { int den = LCMofDen(f1,f2); int den1 = f1.denominator; int den2 = f2.denominator; int num1 = f1.numerator; int num2 = f2.numerator; int num = num1*(den/den1) + num2*(den/den2); Fraction temp = new Fraction(num,den); temp = temp.reduce(); return temp; }// add method ended // sub method ( performs subtraction of fractions ) public static Fraction sub(Fraction f1, Fraction f2) { int den = LCMofDen(f1,f2); int den1 = f1.denominator; int den2 = f2.denominator; int num1 = f1.numerator; int num2 = f2.numerator; int num = num1*(den/den1) - num2*(den/den2); Fraction temp = new Fraction(num,den); temp = temp.reduce(); return temp; } // sub method ended // mul method: // performs multiplication of fractions public Fraction mul(Fraction f1, Fraction f2) { int den1 = f1.denominator; int den2 = f2.denominator; int num1 = f1.numerator; int num2 = f2.numerator; int num = num1*num2; int den = den1*den2; Fraction temp = new Fraction(num,den); temp = temp.reduce(); return temp; } // mul method ended // div method: public Fraction div(Fraction f1, Fraction f2) { int den1 = f1.denominator; int den2 = f2.denominator; int num1 = f1.numerator; int num2 = f2.numerator; int num = num1*den2; int den = den1*num2; Fraction temp = new Fraction(num,den); temp = temp.reduce(); return temp; } // div method ended /* This method LCMofDen it calculates , LCM of denominators ( only useful with addition and subtraction of fractions ) */ private static int LCMofDen( Fraction f1, Fraction f2 ) { int den1 = f1.denominator; int den2 = f2.denominator; int resGCD = gcd(den1,den2); int den = (den1*den2)/resGCD; return den; } // end of LCM method /* This method reduces the fraction into simple form * we use gcd of numerator and denominator. * GCD of numerator and denominator gives us the highest common divisor * and we divide numerator and denominator with that divisor * so, we convert them into simplest form. */ public Fraction reduce() { int num1 = this.numerator; int num2 = this.denominator; int hcf = gcd(num1, num2); this.numerator = num1/hcf; this.denominator = num2/hcf; return new Fraction(this.numerator , this.denominator); } // reduce method ended /* calculates the gcd of two numbers */ private static int gcd(int num1, int num2) { int tmp; num1 = abs(num1); num2 = abs(num2); while (num1 > 0) { tmp = num1; num1 = num2 % num1; num2 = tmp; } return num2; } // end of gcd method // to print the fractions in understandable way public String strVal() { return this.numerator +"/"+this.denominator ; } } // END of Fraction class
--------------------------------------------------------------------------------------------------------------------------------------
file-name: Main.java
-----------------------------------
/*
This Main method acts as a Tester class to test Fraction class.
I did not use any Scanner methods, I just took example input as given in question.
If you want I can add code that takes user input and calculate.
*/
public class Main {
public static void main(String[] args) {
Fraction f1 = new Fraction(1,2);
Fraction f2 = new Fraction(3,2);
Fraction sum = Fraction.add(f1,f2);
Fraction whole = sum.reduce();
System.out.println(whole.strVal());
Fraction f3 = new Fraction(3,4);
Fraction f4 = new Fraction(1,3);
Fraction f5 = Fraction.sub(f3,f4);
System.out.println(f5.strVal());
} // main( ) method ended
} // Main class ended
--------------------------------------------------------------------------------------------
OUTPUT :
THANK YOU !! , If you like my effort please upvote. Let me know if you have any doubts in comment section