Answer to Question #298840 in Java | JSP | JSF for Ekoyy

Question #298840

2. Fraction/Mixed Fraction

by CodeChum Admin

 

Given the class Fraction in the code editor. Fill in the operations and display for the class Fraction. For plus and minus, think cross multiplication. times is straightforward, while divide has to do with reciprocals. toString should return the fraction in string format ("nume/deno" - as a String) but if denominator is 1, then the string should just contain the numerator.


More info here, Important!

https://pastebin.com/VRkEAe6g


1
Expert's answer
2022-02-17T11:52:45-0500
public class Fraction {
    int nume, deno;

    Fraction() {
        System.out.println("Fraction default constructor");
        nume = 0;
        deno = 1;
    }

    Fraction(int n, int d) {
        System.out.println("Fraction overloaded constructor");
        nume = n;
        deno = d;
    }

    Fraction plus(Fraction f) {
        return new Fraction(nume * f.deno + f.nume * deno, deno * f.deno);
    }

    Fraction minus(Fraction f) {
        return new Fraction(nume * f.deno - f.nume * deno, deno * f.deno);
    }

    Fraction times(Fraction f) {
        return new Fraction(nume * f.nume, deno * f.deno);
    }

    Fraction divide(Fraction f) {
        return new Fraction(nume * f.deno, deno * f.nume);
    }

    @Override
    public String toString() {
        return deno == 1 ? Integer.toString(nume) : nume + "/" + deno;
    }
}


public class MixedFraction extends Fraction {
    int wholePart;

    public MixedFraction() {
        wholePart = 0;
        nume = 0;
        deno = 1;
        System.out.println("MixedFraction default constructor");
    }

    public MixedFraction(int wholePart) {
        this.wholePart = wholePart;
        nume = 0;
        deno = 1;
        System.out.println("MixedFraction first overloaded constructor");
    }

    public MixedFraction(int nume, int deno) {
        wholePart = 0;
        this.nume = nume;
        this.deno = deno;
        System.out.println("MixedFraction second overloaded constructor");
    }

    public MixedFraction(int wholePart, int nume, int deno) {
        this.wholePart = wholePart;
        this.nume = nume;
        this.deno = deno;
        System.out.println("MixedFraction third overloaded constructor");
    }

    MixedFraction plus(MixedFraction mf) {
        int thisNume = wholePart * deno + nume;
        int otherNume = mf.wholePart * mf.deno + mf.nume;
        int sumNume = thisNume * mf.deno + otherNume * deno;
        int sumDeno = deno * mf.deno;
        if (sumNume == sumDeno) {
            return new MixedFraction(1, 0, 1);
        }
        return new MixedFraction(sumNume / sumDeno, sumNume % sumDeno, sumDeno);
    }

    MixedFraction minus(MixedFraction mf) {
        int thisNume = wholePart * deno + nume;
        int otherNume = mf.wholePart * mf.deno + mf.nume;
        int sumNume = thisNume * mf.deno - otherNume * deno;
        int sumDeno = deno * mf.deno;
        if (sumNume == sumDeno) {
            return new MixedFraction(1, 0, 1);
        }
        return new MixedFraction(sumNume / sumDeno, sumNume % sumDeno, sumDeno);
    }

    MixedFraction times(MixedFraction mf) {
        int thisNume = wholePart * deno + nume;
        int otherNume = mf.wholePart * mf.deno + mf.nume;
        int sumNume = thisNume * otherNume;
        int sumDeno = deno * mf.deno;
        if (sumNume == sumDeno) {
            return new MixedFraction(1, 0, 1);
        }
        return new MixedFraction(sumNume / sumDeno, sumNume % sumDeno, sumDeno);
    }

    MixedFraction divide(MixedFraction mf) {
        int thisNume = wholePart * deno + nume;
        int otherNume = mf.wholePart * mf.deno + mf.nume;
        int sumNume = thisNume * mf.deno;
        int sumDeno = deno * otherNume;
        if (sumNume == sumDeno) {
            return new MixedFraction(1, 0, 1);
        }
        return new MixedFraction(sumNume / sumDeno, sumNume % sumDeno, sumDeno);
    }

    @Override
    public String toString() {
        return (wholePart > 0 ? wholePart + " " : "") + (nume == 0 && deno == 1 ? "" : super.toString());
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS