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

Modify setters so that they ignore inappropriate values Implement the equals() m

ID: 3542379 • Letter: M

Question

Modify setters so that they ignore inappropriate values

Implement the equals() method inherited from the top-level Object class

Implement less than and greater than methods

Implement add, subtract, and multiply methods



public class Fraction {

private int numer;

private int denom;


public Fraction() { // no-arg constructor

numer = 0;

denom = 1;

}


public Fraction(int numer, int denom) {

this.numer = numer;

this.denom = denom;

}


public Fraction(Fraction frac) { // copy constructor

numer = frac.getNumer();

denom = frac.getDenom();

}


// getters and setters

public int getNumer() {

return numer;

}


public void setNumer(int x) {

numer = x;

}


public int getDenom( {

return denom;

}


public void setDenom(int x) {

denom = x;

}



// Special Methods

public String toString() {

return numer + "/" + denom;

}


// Other Methods

public Fraction reduce() {

Fraction temp = new Fraction();


int GCD = gcd(numer, denom);


temp.setNumer(numer / GCD);

temp.setDenom(denom / GCD);


return temp;

}


// Private Methods

private int gcd(int n1, int n2)

{

int M, N, R;


if (n1 < n2)

{

N = n1;

M = n2;

}

else

{

N = n2;

M = n1;

}


R = M % N;


while (R != 0)

{

M = N;

N = R;


R = M % N;

}


return N;


}

}


FRACTION CLASS TEST DRIVER



public class FractionDriver {

public static void main(String[] args) {


// test constructors (and getters)

Fraction frac0 = new Fraction();

System.out.println("TESTING NO-ARG CONSTRUCTOR");

System.out.println("Numer = " + frac0.getNumer());

System.out.println("Denom = " + frac0.getDenom());


System.out.println("TESTING int/int CONSTRUCTOR");

Fraction frac1 = new Fraction(2,4);

System.out.println("Numer = " + frac1.getNumer());

System.out.println("Denom = " + frac1.getDenom());


System.out.println("TESTING Fraction CONSTRUCTOR");

Fraction frac2 = new Fraction(frac1);

System.out.println("Numer = " + frac2.getNumer());

System.out.println("Denom = " + frac2.getDenom());


// equal check

if (frac1.equals(frac2))

{

System.out.println("frac1 and frac2 found equal");

}

else

{

System.out.println("frac1 and frac2 NOT equal");

}

// test reduce

Fraction reduced_frac1 = frac1.reduce();

System.out.println("Reduced frac1 = " + reduced_frac1);


// test copy constructor

if (frac1.getNumer() == frac2.getNumer() &&

frac1.getDenom() == frac2.getDenom() &&

frac1 != frac2)

{

System.out.println("Copy constructor working");

}

else

System.out.println("PROBLEM with copy constructor");


// test setters

frac2.setNumer(8);

frac2.setDenom(12);

System.out.println("Numer = " + frac2.getNumer());

System.out.println("Denom = " + frac2.getDenom());


// System.out.println("GCD of 2/4 = " + frac1.gcd(1,4));


}

}


THIS IS WHAT I HAVE SO FAR I'M NOT SURE IF I'M RIGHT.




import java.util.*;

        

public class Fraction {

private int numer;

     private int denom;

    

     public Fraction() { // no-arg constructor

        numer = 0;

         denom = 1;

}

    

     public Fraction(int numer, int denom) {

         this.numer = numer;

         this.denom = denom;

     }

    

     public Fraction(Fraction frac) { // copy constructor

         numer = frac.getNumer();

         denom = frac.getDenom();

     }


      // getters and setters

     public int getNumer() {

         return numer;

     }

    

     public void setNumer(int x) {

         numer = x;

     }

    

     public int getDenom()  {

         return denom;

     }

    

     public void setDenom(int x) {

        

            denom = x;

     if (denom == 0)

        throw new IllegalArgumentException("Second argument " + denom + " cannot be zero.");

        }

// Special Methods

     public String toString() {

         return numer + "/" + denom;

     }

    public Fraction add(Fraction f2){


         Fraction temp = new Fraction();


         temp.numer = ((numer * f2.denom) + (denom * f2.numer));

         temp.denom = (denom * f2.denom);

    

        return temp;

        }

    public Fraction subtract(Fraction f2){


        Fraction temp = new Fraction();


        temp.numer = ((numer * f2.denom) - (denom * f2.numer));

        temp.denom = (denom * f2.denom);


        return temp;

        }

    public Fraction Multiply(Fraction f2){

        

        Fraction temp = new Fraction();


        temp.numer = (numer * f2.denom);

        temp.denom = (denom * f2.denom);


        return temp;

        }

    public Fraction Divide(Fraction f2){


        Fraction temp = new Fraction();


        temp.numer = (numer * f2.denom);

        temp.denom = (denom * f2.numer);


        return temp;


        }


     // Other Methods

     public Fraction reduce() {

         Fraction temp = new Fraction();

        

         int GCD = gcd(numer, denom);

        

         temp.setNumer(numer / GCD);

         temp.setDenom(denom / GCD);

        

         return  temp;

     }

// Private Methods

     private int gcd(int n1, int n2)

     {

         int M, N, R;

        

         if (n1 < n2)

         {

             N = n1;

             M = n2;

         }

         else

         {

             N = n2;

            M = n1;

         }

        

         R = M % N;

        

         while (R != 0)

         {

             M = N;

             N = R;

            

             R = M % N;

         }

        

         return N;

        

        }

public static void main(String[] args) {

        

         // test constructors (and getters)

         Fraction frac0 = new Fraction();

         System.out.println("TESTING NO-ARG CONSTRUCTOR");

         System.out.println("Numer = " + frac0.getNumer());

         System.out.println("Denom = " + frac0.getDenom());

        

         System.out.println("TESTING int/int CONSTRUCTOR");

         Fraction frac1 = new Fraction(2,4);

         System.out.println("Numer = " + frac1.getNumer());

         System.out.println("Denom = " + frac1.getDenom());

        

         System.out.println("TESTING Fraction CONSTRUCTOR");

         Fraction frac2 = new Fraction(frac1);

         System.out.println("Numer = " + frac2.getNumer());

         System.out.println("Denom = " + frac2.getDenom());

        

         // equal check

         if (frac1.equals(frac2))

         {

             System.out.println("frac1 and frac2 found equal");

         }

         else

         {

             System.out.println("frac1 and frac2 NOT equal");

         }

             // test reduce

         Fraction reduced_frac1 = frac1.reduce();

         System.out.println("Reduced frac1 = " + reduced_frac1);

        

         // test copy constructor

         if (frac1.getNumer() == frac2.getNumer() &&

             frac1.getDenom() == frac2.getDenom() &&

               frac1 != frac2)

         {

             System.out.println("Copy constructor working");

         }

         else

             System.out.println("PROBLEM with copy constructor");

        

         // test setters

         frac2.setNumer(8);

         frac2.setDenom(12);

         System.out.println("Numer = " + frac2.getNumer());

         System.out.println("Denom = " + frac2.getDenom());

        

         System.out.println("GCD of 2/4 = " + frac1.gcd(1,4));

       

     }

}



Explanation / Answer

please rate - thanks


any questions ask

I changes setting denominators, so both check for 0, reduced all fractions

and added more tests in the main



import java.util.*;

public class Fraction {

private int numer;

private int denom;

public Fraction() { // no-arg constructor

numer = 0;

denom = 1;

}

public Fraction(int numer, int denom) {

this.numer = numer;
if (denom == 0)
{ this.denom=1;
    System.out.println("Second argument " + denom + " cannot be zero.-denominator set to 1");
}
else
   this.denom=denom ;





}

public Fraction(Fraction frac) { // copy constructor

numer = frac.getNumer();

denom = frac.getDenom();

}


// getters and setters

public int getNumer() {

return numer;

}

public void setNumer(int x) {

numer = x;

}

public int getDenom() {

return denom;

}

public void setDenom(int x) {

if (x == 0)
{ denom=1;
    System.out.println("denominator cannot be zero.-denominator set to 1");
}
else
   denom = x;


}

// Special Methods

public String toString() {

return numer + "/" + denom;

}

public Fraction add(Fraction f2){


Fraction temp = new Fraction();


temp.numer = ((numer * f2.denom) + (denom * f2.numer));

temp.denom = (denom * f2.denom);
temp=temp.reduce();
return temp;

}

public Fraction subtract(Fraction f2){


Fraction temp = new Fraction();


temp.numer = ((numer * f2.denom) - (denom * f2.numer));

temp.denom = (denom * f2.denom);

temp=temp.reduce();
return temp;

}

public Fraction Multiply(Fraction f2){

Fraction temp = new Fraction();


temp.numer = (numer * f2.denom);

temp.denom = (denom * f2.denom);

temp=temp.reduce();
return temp;

}

public Fraction Divide(Fraction f2){


Fraction temp = new Fraction();


temp.numer = (numer * f2.denom);

temp.denom = (denom * f2.numer);

temp=temp.reduce();
return temp;


}


// Other Methods

public Fraction reduce() {

Fraction temp = new Fraction();
int GCD = gcd(numer, denom);

temp.setNumer(numer / GCD);

temp.setDenom(denom / GCD);
return temp;

}

// Private Methods

private int gcd(int n1, int n2)

{

int M, N, R;

if (n1 < n2)

{

N = n1;

M = n2;

}

else

{

N = n2;

M = n1;

}

R = M % N;

while (R != 0)

{

M = N;

N = R;

R = M % N;

}

return N;

}

public static void main(String[] args) {

// test constructors (and getters)

Fraction frac0 = new Fraction();

System.out.println("TESTING NO-ARG CONSTRUCTOR");

System.out.println("Numer = " + frac0.getNumer());

System.out.println("Denom = " + frac0.getDenom());

System.out.println("TESTING int/int CONSTRUCTOR");

Fraction frac1 = new Fraction(2,4);

System.out.println("Numer = " + frac1.getNumer());

System.out.println("Denom = " + frac1.getDenom());
System.out.println("TESTING 0 denominator");
Fraction frac3 = new Fraction(2,0);

System.out.println("Numer = " + frac3.getNumer());

System.out.println("Denom = " + frac3.getDenom());


System.out.println("TESTING Fraction CONSTRUCTOR");

Fraction frac2 = new Fraction(frac1);

System.out.println("Numer = " + frac2.getNumer());

System.out.println("Denom = " + frac2.getDenom());

// equal check

if (frac1.equals(frac2))

{

System.out.println("frac1 and frac2 found equal");

}

else

{

System.out.println("frac1 and frac2 NOT equal");

}

// test reduce

Fraction reduced_frac1 = frac1.reduce();

System.out.println("Reduced frac1 = " + reduced_frac1);

// test copy constructor

if (frac1.getNumer() == frac2.getNumer() &&

frac1.getDenom() == frac2.getDenom() &&

frac1 != frac2)

{

System.out.println("Copy constructor working");

}

else

System.out.println("PROBLEM with copy constructor");

// test setters

frac2.setNumer(8);

frac2.setDenom(12);

System.out.println("Numer = " + frac2.getNumer());

System.out.println("Denom = " + frac2.getDenom());

System.out.println("GCD of 2/4 = " + frac1.gcd(2,4));
System.out.println("TESTING addition 2/4+8/12");
System.out.println(frac1.add(frac2));
System.out.println("TESTING subtraction 2/4-8/12");
System.out.println(frac1.subtract(frac2));

System.out.println("TESTING multiplication 2/4*8/12");
System.out.println(frac1.Multiply(frac2));
System.out.println("TESTING division 2/4 / 8/12");
System.out.println(frac1.Divide(frac2));
}

}


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