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

Indicate five errors in the following Fraction class. Include no more than one e

ID: 3800339 • Letter: I

Question

Indicate five errors in the following Fraction class. Include no more than one error per

       constructor/method. (5 pts.)

    private int numer;

    private int denom;

    public Fraction() { // no-arg constructor

        numer = 0;

        denom = 1;

    }

    public Fraction(int numerator, int denominator) {

        numerator = numer;

        denominator = denom;

    }

    public Fraction(Fraction fraction) { // copy constructor

        numer = frac.getNumer();

        denom = frac.getDenom();

    }

    // getters and setters

    public void getNumer() {

        return numer;

    }

    // ^ Cant return a void!

    public void setNumer(int x) {

        numer == x;

    }

    public int getDenom() {

        return denom;

    }

    public void setDenom(int x) {

        if (x != 0)

            denom = x;

        else

            throw ArithmeticException(); // missing "new"

    }

    // Special Methods

    public String toString() {

        return numer + "/" + denom;

    }

    // Relational operator methods

    public String equals(Fraction rFrac) {

        Fraction temp_frac1 = new Fraction(getNumer(), getDenom());

       Fraction temp_frac2 =

                    new Fraction(rFrac.getNumer(), rFrac.getDenom());

        temp_frac1 = temp_frac1.reduce();

        temp_frac2 = temp_frac2.reduce();

        if ((temp_frac1.numer == temp_frac2.numer) &&

               (temp_frac1.denom == temp_frac2.denom))

            return "Fractions found equal";

      else

            return "Fraction not equal";

    }

   

   public Fraction lessThan(Fraction rFrac) {

       Fraction tempFrac1 = new Fraction(getNumer() * rFrac.getDenom(),

                                         getDenom() * rFrac.getDenom());

       Fraction tempFrac2 = new Fraction(rFrac.getNumer() * getDenom(),

                                          rFrac.getDenom() * getDenom());

       return tempFrac1.getNumer() < tempFrac2.getNumer();

    }

    public boolean greaterThan(Fraction rFrac) {

        return equals(rFrac) && !lessThan(rFrac);

    }

   

    public Fraction add(Fraction rFrac) {

      Fraction tempFrac1 = new Fraction(getNumer() * rFrac.getDenom(),

                                   getDenom() * rFrac.getDenom());

        Fraction tempFrac2 = new Fraction(rFrac.getNumer() * getDenom(),

                                   rFrac.getDenom() * getDenom());

       return new Fraction(tempFrac1.getNumer() + tempFrac2.getNumer(),

                             tempFrac1.getDenom()).reduce();

    }

    public Fraction subtract(Fraction rFrac){

        int num1 =((this.numer * rFrac.denom) - (rFrac.numer * this.denom));

        int num2 = this.denom * rFrac.denom;

        Fraction x = new Fraction (num1, num2);

        return x.reduce();

    }

    public Fraction multiply (Fraction frac1, Fraction frac2) {

        int num1 = frac1.numer * frac2.numer;

        int num2 = frac1.denom * frac2.denom;

        Fraction x = new Fraction (num1, num2);

        return x.reduce();

    }

    // Other Methods

    public Fraction reduce() {

       // assume implemented correctly

    }

     // Private Methods

    private int gcd(int n1, int n2) {

    // assume implemented correctly

    }

}

Explanation / Answer

Hi, Please find my answer.

Please read my commnet. I have labeled with 'ERROR' in code.

public class Fraction {

   private int numer;

   private int denom;

   public Fraction() { // no-arg constructor

       numer = 0;

       denom = 1;

   }

   public Fraction(int numerator, int denominator) {

       numerator = numer;

       denominator = denom;

   }

   public Fraction(Fraction fraction) { // copy constructor

       numer = frac.getNumer(); // ERROR 1: frac is not defined, it should be fraction

       denom = frac.getDenom();

   }

   // getters and setters

   public void getNumer() { // ERROR 2: return type should be int

       return numer;

   }

   // ^ Cant return a void!

   public void setNumer(int x) {

       numer == x; // ERROR 3:, == is equality operator, it should be assignment operator(=)

   }

   public int getDenom() {

       return denom;

   }

   public void setDenom(int x) {

       if (x != 0)

           denom = x;

       else

           throw ArithmeticException(); // ERROR 4: missing new operator

   }

   // Special Methods

   public String toString() {

       return numer + "/" + denom;

   }

   // Relational operator methods

   public String equals(Fraction rFrac) {

       Fraction temp_frac1 = new Fraction(getNumer(), getDenom());

       Fraction temp_frac2 =

               new Fraction(rFrac.getNumer(), rFrac.getDenom());

       temp_frac1 = temp_frac1.reduce();

       temp_frac2 = temp_frac2.reduce();

       if ((temp_frac1.numer == temp_frac2.numer) &&

               (temp_frac1.denom == temp_frac2.denom))

           return "Fractions found equal";

       else

           return "Fraction not equal";

   }

   public Fraction lessThan(Fraction rFrac) { // ERROR 5: return tupe should be boolean

       Fraction tempFrac1 = new Fraction(getNumer() * rFrac.getDenom(),

               getDenom() * rFrac.getDenom());

       Fraction tempFrac2 = new Fraction(rFrac.getNumer() * getDenom(),

               rFrac.getDenom() * getDenom());

       return tempFrac1.getNumer() < tempFrac2.getNumer();

   }

   public boolean greaterThan(Fraction rFrac) {

       return equals(rFrac) && !lessThan(rFrac);

   }

   public Fraction add(Fraction rFrac) {

       Fraction tempFrac1 = new Fraction(getNumer() * rFrac.getDenom(),

               getDenom() * rFrac.getDenom());

       Fraction tempFrac2 = new Fraction(rFrac.getNumer() * getDenom(),

               rFrac.getDenom() * getDenom());

       return new Fraction(tempFrac1.getNumer() + tempFrac2.getNumer(),

               tempFrac1.getDenom()).reduce();

   }

   public Fraction subtract(Fraction rFrac){

       int num1 =((this.numer * rFrac.denom) - (rFrac.numer * this.denom));

       int num2 = this.denom * rFrac.denom;

       Fraction x = new Fraction (num1, num2);

       return x.reduce();

   }

   public Fraction multiply (Fraction frac1, Fraction frac2) {

       int num1 = frac1.numer * frac2.numer;

       int num2 = frac1.denom * frac2.denom;

       Fraction x = new Fraction (num1, num2);

       return x.reduce();

   }

   // Other Methods

   public Fraction reduce() {

       // assume implemented correctly

   }

   // Private Methods

   private int gcd(int n1, int n2) {

       // assume implemented correctly

   }

}