Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rational fractions are of teh form a/b, where a and b are integers and b does no

ID: 3641491 • Letter: R

Question

Rational fractions are of teh form a/b, where a and b are integers and b does not equal zero. In this exercise, by "fractions" we mean rational fractions. Suppose a/b and c/d are fractions. Arithmetic operations on fractions are defined by the following rules:
a/b + c/d = (ad + bc) / bd
a/b - c/d = (ad - bc) / bd
a/b * c/d = ac / bd
(a/b) / (c/d) = ad/bc, where c/d is not equal to zero
Fractions are compared as follows: a/b op c/d if ad op bc, where op is any of the relational operations. For example, a/b < c/d if ad < bc.

Design the class Fraction that can be used to manipulate fractions in a program. Among others, the class Fraction must include methods to add, subtract, multiply, and divide fractions. When you add, subtract, multiply, or divide fractions, your answer need not be in the lowest terms. Also, override the method toString so that the fractions can be output using the output statement.

Write a java program that, using the class Fraction, performs operations on fractions.

Explanation / Answer

I'm not sure whether or not you wanted the driver class and the Fraction class to be in the same file or not, so I kept them as separate files with the names Fraction.java and FractionTest.java (the driver). Make sure you save both of them with these names or you may get compiler errors. I went about this problem by making each fraction an object of the class Fraction with the instance variables "numerator" and "denominator". I included a few more methods such as setNumerator, setDenominator, getNumerator and getDenominator along with three constructors that accept different numbers of parameters as well. I have the driver class listed first and then the Fraction class listed after separated by hyphens. (If it looks too messy in the answer, just paste it into a text editor and it should look fine). Hope this is what you needed!

----------------------------------------------------------------------

//File: FractionTest.java

import java.util.Scanner;
public class FractionTest
{
    public static void main(String[] args)
    {
        int numerator, denominator, wholeNumber;
        Scanner scan = new Scanner(System.in);

        //------------------------------
        // Two argument constructor test
        //------------------------------
        System.out.print("Enter numerator: ");
        numerator = scan.nextInt();

        System.out.print("Enter denominator: ");
        denominator = scan.nextInt();

        Fraction f1 = new Fraction(numerator, denominator);

        //--------------------------------------------------
        // getNumerator() and getDenominator() method tests
        //--------------------------------------------------
        System.out.println("The numerator you entered was: " + f1.getNumerator());
        System.out.println("The denominator you entered was: " + f1.getDenominator());
        System.out.println();

        //--------------------------------------------------
        // setNumerator() and setDenominator() method tests
        //--------------------------------------------------
        System.out.print("Enter a new numerator: ");
        numerator = scan.nextInt();

        System.out.print("Enter a new denominator: ");
        denominator = scan.nextInt();

        f1.setNumerator(numerator);
        f1.setDenominator(denominator);

        System.out.println("The new numerator you entered was: " + f1.getNumerator());
        System.out.println("The new denominator you entered was: " + f1.getDenominator());
        System.out.println();

        //------------------------------
        // One argument constructor test
        //------------------------------
        System.out.print("Enter a whole number: ");
        wholeNumber = scan.nextInt();
        Fraction f2 = new Fraction(wholeNumber);
        System.out.println("The whole number you entered is equal to: "+f2);

        //------------------------------
        // No argument constructor test
        //------------------------------
        Fraction f3 = new Fraction();
        System.out.println("Zero is the same as: "+ f3);

        //------------------------------
        // .add() method test
        //------------------------------
        System.out.println("Time to do some math...");
        System.out.print("Enter the first numerator: ");
        numerator = scan.nextInt();
        System.out.print("Enter the first denominator: ");
        denominator = scan.nextInt();
        f1.setNumerator(numerator);
        f1.setDenominator(denominator);

        System.out.print("Enter the second numerator: ");
        numerator = scan.nextInt();
        System.out.print("Enter the second denominator: ");
        denominator = scan.nextInt();
        f2.setNumerator(numerator);
        f2.setDenominator(denominator);

        f3 = Fraction.add(f1, f2);
        System.out.println(f1 + " plus " + f2 + " equals " + f3);

        //------------------------------
        // .subtract() method test
        //------------------------------
        f3 = Fraction.subtract(f1, f2);
        System.out.println(f1 + " minus " + f2 + " equals " + f3);
        //------------------------------
        // .multiply() method test
        //------------------------------
        f3 = Fraction.multiply(f1, f2);
        System.out.println(f1 + " times " + f2 + " equals " + f3);
        //------------------------------
        // .divide() method test
        //------------------------------
        f3 = Fraction.divide(f1, f2);
        System.out.println(f1 + " divided by " + f2 + " equals " + f3);
    }


}

----------------------------------------------------------------------------------

// File Fraction.java

public class Fraction
{
    //-------------------------
    // instance variables
    //-------------------------
    private int numerator, denominator;

    //-------------------------------
    // Constructor with two arguments
    //-------------------------------
    public Fraction(int n, int d)
    {
        numerator = n;
        denominator = d;
    }

    //------------------------------
    // Constructor with one argument
    // (Creates a whole number ratio
    // if one argument is used)
    //------------------------------
    public Fraction(int wholeNumber)
    {
        numerator = wholeNumber;
        denominator = 1;
    }

    //------------------------------------------------
    // Constructor with no arguments
    // (Creates 0/1 fraction if no
    // arguments are passed in)
    //------------------------------------------------
    public Fraction()
    {
        numerator = 0;
        denominator = 1;
    }

    //--------------------------------
    // .getNumerator() accessor method
    // (used to return the numerator)
    //--------------------------------

    public int getNumerator()
    {
        return numerator;
    }

    //--------------------------------
    // .getDenominator() accessor method
    // (used to return the denominator)
    //--------------------------------
    public int getDenominator()
    {
        return denominator;
    }

    //--------------------------------
    // .setNumerator() mutator method
    // (to explicitly set the numerator)
    //--------------------------------
    public void setNumerator(int n)
    {
        numerator = n;
    }

    //------------------------------------
    // .setDenominator() mutator method
    // (to explicitly set the denominator)
    //------------------------------------
    public void setDenominator(int d)
    {
        denominator = d;
    }

    //------------------------------
    //      Addition method
    // a/b + c/d = (ad + bc) / bd
    //------------------------------
    public static Fraction add(Fraction f1, Fraction f2)
    {
            Fraction f3 = new Fraction();
            f3.numerator = ((f1.getNumerator()*f2.getDenominator())+(f2.getNumerator()+f1.getDenominator()));
            f3.denominator = (f1.getDenominator()*f2.getDenominator());
            return f3;
    }

    //-----------------------------
    //      Subtraction method
    // a/b - c/d = (ad - bc) / bd
    //-----------------------------
    public static Fraction subtract(Fraction f1, Fraction f2)
    {
            Fraction f3 = new Fraction();
            f3.numerator = ((f1.getNumerator()*f2.getDenominator())-(f2.getNumerator()+f1.getDenominator()));
            f3.denominator = (f1.getDenominator()*f2.getDenominator());
            return f3;
    }

    //------------------------
    // Multiplication method
    // a/b * c/d = ac / bd
    //------------------------
    public static Fraction multiply(Fraction f1, Fraction f2)
    {
        Fraction f3 = new Fraction();
        f3.numerator = (f1.getNumerator() * f2.getNumerator());
        f3.denominator = (f1.getDenominator() * f2.getDenominator());
        return f3;
    }

    //-----------------------
    //     Division method
    // (a/b) / (c/d) = ad/bc
    //-----------------------
    public static Fraction divide(Fraction f1, Fraction f2)
    {
        Fraction f3 = new Fraction();
        f3.numerator = (f1.getNumerator() * f2.getDenominator());
        f3.denominator = (f1.getDenominator() * f2.getNumerator());
        return f3;
    }

    //-------------------
    // .toString() method
    //-------------------
    public String toString()
    {

        return (numerator + "/" + denominator);
    }
}