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

Consider a class Fraction of fractions. Each fraction is signed and has a numera

ID: 3914109 • Letter: C

Question

Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal, and convert a fraction to a string. Your class should handle denominators that are zero. Fractions should always occur in lowest terms, and the class should be responsible for this requirement. For example, if the user tries to create a fraction such as 4/8, the class should set the fraction to 1/2. Likewise, the results of all arithmetic operations should be in lowest terms. Note that a fraction can be improper-that is, have a numerator that is larger than its denominator. Such a fraction, however, should be in lowest terms. Design the class Fraction. Begin by writing a CRC card for this class. Then write a Java interface that declares each public method To reduce a fraction such as 4/8 to lowest terms, you need to divide both the numerator and the denominator by their greatest common denominator. The greatest common denominator of 4 and 8 is 4, so when you divide the numerator and denominator of 4/8 by 4, you get the fraction 1/2. The following recursive algorithm finds the greatest common denominator of two positive integers: Algorithm gcd(integerOne, integerTwo) if (integerOne % integer,Two-:0) result-integerTwo else results gcd(integerTwo, integerOne % integerTwo) return result It will be easier to determine the correct sign of a fraction if you force the fraction's denominator to be positive. However, your implementation must handle negative denominators that the client might provide Write a program that adequately demonstrates your class

Explanation / Answer

Hello, I have answered similar question before, so I’m referring my own solution here. Made proper changes as needed. Also defined an interface for declaring all the necessary methods, and a test class to test the working of all methods thoroughly. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Fraction.java

public class Fraction implements FractionInterface {

                private int numerator;

                private int denominator;

                /**

                * constructor with arguments to initialize numerator and denominator after

                * proper validation and reduction

                */

                public Fraction(int numerator, int denominator) {

                                if (denominator == 0) {

                                                /**

                                                * making sure denominator never be 0 , if an invalid input is

                                                * given, setting denominator to 1

                                                */

                                                denominator = 1;

                                }

                                /**

                                * if denom is less than 0, making it positive and changing the sign of

                                * numerator

                                */

                                if (denominator < 0) {

                                                denominator = Math.abs(denominator);

                                                numerator = 0 - numerator;

                                }

                                /**

                                * setting validated values to the variables

                                */

                                this.numerator = numerator;

                                this.denominator = denominator;

                                simplify();// reducing to lowest terms

                }

                /**

                * A private method to calculate the greatest common divisor of two numbers

                */

                private int gcd(int x, int y) {

                                x=Math.abs(x);

                                y=Math.abs(y);

                                if (x % y == 0)

                                                return y;

                                return gcd(y, x % y);

                }

                /**

                * method to simplify the rational number. This method will find the gcd and

                * divides it from both numerator and denominator

                */

                private void simplify() {

                               

                                /**

                                * Finding the GCD

                                */

                                int gcd = gcd(numerator, denominator);

                                /**

                                * removing the GCD from numer..and denom..

                                */

                                numerator = numerator / gcd;

                                denominator = denominator / gcd;

                }

                /**

                * method to return a string representation of a number

                */

                public String toString() {

                                return numerator + "/" + denominator;

                }

                /**

                * method to add a rational number to the current number and returns it

                */

                @Override

                public FractionInterface add(FractionInterface other) {

                                Fraction b = (Fraction) other;

                                int num = (this.numerator * b.denominator)

                                                                + (this.denominator * b.numerator);

                                int den = this.denominator * b.denominator;

                                Fraction rational = new Fraction(num, den);

                                return rational;

                }

                /**

                * method to subtract a rational number from the current number and returns

                * it

                */

                @Override

                public FractionInterface sub(FractionInterface other) {

                                Fraction b = (Fraction) other;

                                int num = (this.numerator * b.denominator)

                                                                - (this.denominator * b.numerator);

                                int den = this.denominator * b.denominator;

                                Fraction rational = new Fraction(num, den);

                                return rational;

                }

                /**

                * method to multiply a rational number to the current number and returns it

                */

                @Override

                public FractionInterface mult(FractionInterface other) {

                                Fraction b = (Fraction) other;

                                int num = this.numerator * b.numerator;

                                int den = this.denominator * b.denominator;

                                Fraction rational = new Fraction(num, den);

                                return rational;

                }

                /**

                * method to divide a rational number from the current number and returns it

                */

                @Override

                public FractionInterface divide(FractionInterface other) {

                                Fraction b = (Fraction) other;

                                if (b.numerator == 0) {

                                                System.out

                                                                                .println("Unexpected Error: The denominator cannot be zero");

                                                return null;

                                }

                                /**

                                * Finding the recipocal of second number and multiplying it

                                */

                                b = (Fraction) b.invert();

                                return mult(b);

                }

                /**

                * method to find and return the reciprocal of a rational number

                */

                @Override

                public FractionInterface invert() {

                                if (numerator == 0) {

                                                System.out.println("No inverse for zero");

                                                return null;

                                }

                                int newNumerator = this.denominator;

                                int newDenominator = Math.abs(this.numerator);

                                if (this.numerator < 0) {

                                                newNumerator = 0 - newNumerator;

                                }

                                return new Fraction(newNumerator, newDenominator);

                }

                /**

                * checks if a Fraction number is less than this number

                */

                @Override

                public boolean lessThan(FractionInterface other) {

                                Fraction f = (Fraction) other;

                                double value1 = numerator / denominator;

                                double value2 = f.numerator / f.denominator;

                                if (value1 < value2) {

                                                return true;

                                } else {

                                                return false;

                                }

                }

                /**

                * checks if a Fraction number is greater than this number

                */

                @Override

                public boolean greaterThan(FractionInterface other) {

                                Fraction f = (Fraction) other;

                                double value1 = numerator / denominator;

                                double value2 = f.numerator / f.denominator;

                                if (value1 > value2) {

                                                return true;

                                } else {

                                                return false;

                                }

                }

                /**

                * checks if a Fraction number is equal to this number

                */

                @Override

                public boolean equals(FractionInterface other) {

                                if (other instanceof Fraction) {

                                                Fraction f = (Fraction) other;

                                                if (this.numerator == f.numerator

                                                                                && this.denominator == f.denominator) {

                                                                return true;

                                                }

                                }

                                return false;

                }

}

// FractionInterface.java

public interface FractionInterface {

                FractionInterface add(FractionInterface other);

                FractionInterface sub(FractionInterface other);

                /**

                * method to multiply a rational number to the current number and returns it

                */

                FractionInterface mult(FractionInterface other);

                /**

                * method to divide a rational number from the current number and returns it

                */

                FractionInterface divide(FractionInterface other);

                /**

                * method to find and return the reciprocal of a rational number

                */

                FractionInterface invert();

                /**

                * checks if a Fraction number is less than this number

                */

                boolean lessThan(FractionInterface other);

                /**

                * checks if a Fraction number is greater than this number

                */

                boolean greaterThan(FractionInterface other);

                /**

                * checks if a Fraction number is equal to this number

                */

                boolean equals(FractionInterface other);

}

// Test.java

import java.util.Scanner;

public class Test {

                public static void main(String[] args) {

                                /**

                                * getting inputs for two fractions

                                */

                                Scanner scanner=new Scanner(System.in);

                                Fraction f1,f2;

                               

                                System.out.print("Enter numerator for first fraction: ");

                                int num1=Integer.parseInt(scanner.nextLine());

                               

                                System.out.print("Enter denominator for first fraction: ");

                                int den1=Integer.parseInt(scanner.nextLine());

                               

                                f1=new Fraction(num1, den1);

                               

                                System.out.print("Enter numerator for second fraction: ");

                                int num2=Integer.parseInt(scanner.nextLine());

                               

                                System.out.print("Enter denominator for second fraction: ");

                                int den2=Integer.parseInt(scanner.nextLine());

                               

                                f2=new Fraction(num2, den2);

                                /**

                                * demonstrating all the functions we defined

                                */

                                System.out.println("First fraction f1: "+f1);

                                System.out.println("Second fraction f2: "+f2);

                                System.out.println("f1+f2: "+f1.add(f2));

                                System.out.println("f1-f2: "+f1.sub(f2));

                                System.out.println("f1*f2: "+f1.mult(f2));

                                System.out.println("f1/f2: "+f1.divide(f2));

                                System.out.println("f1 reciprocal: "+f1.invert());

                                System.out.println("f2 reciprocal: "+f2.invert());

                                System.out.println("f1 < f2: "+f1.lessThan(f2));

                                System.out.println("f1 > f2: "+f1.greaterThan(f2));

                                System.out.println("f1 == f2: "+f1.equals(f2));

                               

                               

                }

}

/*OUTPUT*/

Enter numerator for first fraction: 2

Enter denominator for first fraction: 5

Enter numerator for second fraction: 30

Enter denominator for second fraction: 15

First fraction f1: 2/5

Second fraction f2: 2/1

f1+f2: 12/5

f1-f2: -8/5

f1*f2: 4/5

f1/f2: 1/5

f1 reciprocal: 5/2

f2 reciprocal: 1/2

f1 < f2: true

f1 > f2: false

f1 == f2: false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote