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

Write a class name Fraction that is made up of two double for numerator and deno

ID: 3694618 • Letter: W

Question

Write a class name Fraction that is made up of two double for numerator and denonminator. A Fraction class should have the following methods:

(a): Two constructors- one that is a defualt constructor that sets the numerator to 0 and the denominato to 1 and another that accepts 2 paraemeters and set the variables to these paremeters.

(b). Two getters and two setters methods

(c) A method name add_Fraction that accepts a fraction object as the parameter, add this fraction object to the calling Fraction object and returns the resulting Fraction object.

(d) A method name print(Fraction() that will print to the screen (console) the calling Fraction.

(e) A method name multiply multiply_Fraction that accepts a Fraction object as the parameter multipy this Fraction object to the calling Fraction object and returns the resulting Fraction object.

The call would produce the following console output.

Fraction f1 = new Fraction(6,8);

Fraction f2 = new Fraction (1,2);

f1.add_Fraction(f2);

f1.print_Fraction();

The fraction f1 is 10/8

f1.multiply_Fraction(f2);

f1.print_Fraction();

The fraction f1 is 10/16

Explanation / Answer

Print_fraction is not needed… hence I left that method on the code.

import java.io.*;

class Fraction1

{

    private final int numerator1;

    private final int denominator1;

    Fraction1() {

        numerator1= 0;

        denominator1 = 1;

    }

    Fraction1(int numerator1, int denominator1) {

        this.numerator1= numerator1;

        if (denominator1 == 0) {

            throw new IllegalArgumentException("check the Denominator that cannot be zero.");

        } else {

            this.denominator1 = denominator1;

        }

    }

    public String getFraction1() {

        return numerator1+ "/" + denominator1;

    }

    public double getFractionDecimal1() {

        return ((double) numerator1) / denominator1;

    }

    public String addFraction1(Fraction1 otherFraction)

{

      

        int a = this.numerator1;

        int b = this.denominator1;

        int c = otherFraction.numerator1;

        int d = otherFraction.denominator1;

        return ((a*d) + (c*b)) + "/" + (b*d);

    }

    public String multiplyByFraction1(Fraction1 otherFraction)

{

     

        int a = this.numerator1;

        int b = this.denominator1;

        int c = otherFraction.numerator1;

        int d = otherFraction.denominator1;

        return (a*c) + "/" + (b*d);

    }

}

class TestFraction1

{

    public static void main(String[] args) {

        Fraction1 f1 = new Fraction1(6,8);

        Fraction1 f2 = new Fraction1(1,2);

        System.out.println("F1 + F2: " + f1.addFraction1(f2));

        System.out.println("F2 + F1: " + f2.addFraction1(f1) + " ");

        System.out.println("F1 * F2: " + f1.multiplyByFraction1(f2));

        System.out.println("F2 * F1: " + f2.multiplyByFraction1(f1) + " ");

     

    }

}

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