Find any Java Fraction class on the Internet Add any methods

Find any Java Fraction class on the Internet. Add any methods from the Lecture slides that are not implemented. Add a static “main” method that tests every single method, including constructors, for correctness.

Here\'s what I have so far:

public class Fraction { public static final Fraction ZERO = new Fraction(); //constants

private int numerator, denominator; //instance variables

public Fraction() { numerator=0; denominator=1;} //default constructor

public Fraction(int num, int denom) {numerator = num; denominator = denom;}

public Fraction(Fraction toCopy) { //copy constructor

this.numerator = toCopy.numerator; /* … */}

public Fraction(String constant) {return Fraction.parse(constant); } /*optional*/

public int getNumerator() {return numerator;} //getter or accessor

public Fraction set(int num, int denom) //setter

{numerator = num; denominator = denom; return this;}

public Fraction reduce() { /* … */ return this;} //mutator

public Fraction add(Fraction f) { /* … */ return this;} //mutator

public int compareTo(Fraction f) { /* … */ return 0;} //predicate or test

public boolean equals(Object o) { /* … */ return true; } //predicate or tests

public static Fraction parse(String input) { /* … */ return ZERO;} //converts toString output

public String toString() { /* … */ return “3/4”;} //enables System.out.print }

Those were all the method from the lecture slide plus any others I could find. I just need them tested in a main method now and I\'m not sure how to do that.

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

############## Fraction.java ##########

public class Fraction {

   /**

   * instance variable

   */

   public int numerator;

   /**

   * instance variable

   */

   public int denominator;

   // copy constructor

   public Fraction(Fraction other) {

       numerator = other.numerator;

       denominator = other.denominator;

   }

   /**

   * param n

   * param d

   */

   public Fraction(int n, int d) {

       if(d == 0){

           System.out.println(\"Denominator can not be negative\");

           System.exit(0);

       }

       // finding gcd of n and d

       int gcd = reduce(n, d);

       // storing numerator and denominator in reduced form

       numerator = n/gcd;

       denominator = d/gcd;

   }

   // toString method

   public String toString(){

       return numerator+\"/\"+denominator;

   }

   // equal method

   public boolean equals(Object other)

   {

       if( other != null && ! (other instanceof Fraction ) ) return false;

       Fraction that = (Fraction)other;

       if(numerator == that.numerator && denominator == that.denominator)

           return true;

       else

           return false;

   }

   /**

   * function to calculate gcd of numerator and denominator

   */

   private int reduce(int numerator, int denominator) {

       int min = numerator;

       int gcd;

       if (numerator > denominator) {

           min = denominator;

       }

       for (gcd = min; gcd > 1; gcd--) {

           if (numerator % gcd == 0 && denominator % gcd == 0) {

               break;

           }

       }

       return gcd;

   }

   public Fraction add(Fraction other){

       int num = numerator*other.denominator + denominator*other.numerator;

       int deno = numerator*denominator;

       int gcd = reduce(num, deno);

       return new Fraction(num/gcd, deno/gcd);

   }

   public Fraction set(int num, int denom) //setter

   {numerator = num; denominator = denom; return this;}

   public int compareTo(Fraction f) {

       double f1 = (double)numerator/(double)denominator;

       double f2 = (double)f.numerator/(double)f.denominator;

       if(f1 < f2)

           return -1;

       else if(f1 > f2)

           return 1;

       else

           return 0;

   } //predicate or test

}

############## FractionTest.java ################

public class FractionDriver {

  

   public static void main(String[] args) {

      

       Fraction a = new Fraction(1,2);

Fraction b = new Fraction(3,4);

Fraction c = new Fraction(b);

System.out.println(\"a:\"+a.toString());

System.out.println(\"b:\"+b.toString());

System.out.println(\"c:\"+c.toString());

  

a.add(b);

a = a.add(b);

System.out.println(\"a:\"+a.toString());

System.out.println(\"b:\"+b.toString());

System.out.println(\"c:\"+c.toString());

System.out.println(\"a.equals(b):\" + a.equals(b));

System.out.println(\"b.equals(c):\" + b.equals(c));

      

   }

  

}

/*

Sample output:

a:1/2

b:3/4

c:3/4

a:5/1

b:3/4

c:3/4

a.equals(b):false

b.equals(c):true

*/

Find any Java Fraction class on the Internet. Add any methods from the Lecture slides that are not implemented. Add a static “main” method that tests every sing
Find any Java Fraction class on the Internet. Add any methods from the Lecture slides that are not implemented. Add a static “main” method that tests every sing
Find any Java Fraction class on the Internet. Add any methods from the Lecture slides that are not implemented. Add a static “main” method that tests every sing
Find any Java Fraction class on the Internet. Add any methods from the Lecture slides that are not implemented. Add a static “main” method that tests every sing
Find any Java Fraction class on the Internet. Add any methods from the Lecture slides that are not implemented. Add a static “main” method that tests every sing

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site