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

Fraction Java Program Write a Fraction class that is, write your own class that

ID: 3766091 • Letter: F

Question

Fraction Java Program

Write a Fraction class that is, write your own class that will represent a fraction in your program. Each variable of type Fraction represents a single fraction. That means that the class should have at least two member variables that should be private: the numerator and the denominator of the fraction. The class also must have the following public member functions Fraction(int n, int d); // constructor that defines a fraction n/d Fraction) // constructor that defines a default fraction of 0/1 Fraction(Scanner s) // constructor that defines a fraction via Scanner input . double toDecimal(); // returns the decimal value of the fractiorn . String toString); /I returns the string form of the fraction o "numerator" if denominator is 1 "numerator/denominator (decimal, with three decimal places)" otherwise · int get.Numerator(); // returns the numerator of the fraction int ) I/ returns the denominator of the fraction . Fraction plus (Fraction f); // returns fraction + parameter . Fraction minus (Fraction f); // returns fraction - parameter Fraction times (Fraction f) // returns fraction parameter Fraction divides (Fraction f) // returns fraction / parameter You have been provided a private method: int[l simplifyEraction(int[] f); // returns simplified version of parameter Using this method, all constructors should make sure that the fraction is in reduced form. You may not change the public API- that is, any methods not listed above must be made private.

Explanation / Answer

class Fraction
{
int n,d;
Fraction(int n,int d)
{
n=this.n;
d=this.d;
}

Fraction()
{
d=1;
n=0;
}

Fraction(Scanner sc)
{
sc =new Scanner(System.in);
int n=sc.nextInt();
int d=sc.nextInt();
Fraction(n,d);
}

double toDecimal()
{
double r=(double)n/d;
return r;
}
String toString()
{
return (n/1 + "/" + d/1);
}
int getNumerator()
{
return n;
}
int getDenominator()
{
return d;
}

Fraction plus(Fraction f)
{
Fraction r = new Fraction((n * f2.d) +
         (f2.n * d),
         (d * f2.d));   
    return r;
}

Fraction minus(Fraction f)
{
Fraction r = new Fraction((n * f2.d) -
         (f2.n * d),
         (d * f2.d));   
    return r;
}

Fraction times(Fraction f)
{
Fraction r = new Fraction((n * f2.n),(d * f2.d));   
    return r;
}
Fraction divide(Fraction f)
{
Fraction r = new Fraction((n * f2.d),(d * f2.n));   
    return r;
}

}

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