Fraction class Numerators and denominators are unchangeable

Fraction class

• Numerators and denominators are unchangeable once set by the constructor.

• No denominator will be stored as a 0. (ie, no DivideByZero Exceptions).

• A Fraction is always in reduced form. (reduce in the constructor to ensure this).

Data

• Define a numerator that is public and final.

• Define a denominator that is public and final.

Methods

• Define a constructor that takes a numerator and a denominator

• Define a constructor that takes a Fraction object and makes a copy of it. public Fraction(Fraction other){…}

• Define a toString() function

• Define an add function that takes a fraction, adds it to this, then returns

a new Fraction object that is the result of the addition of the two o public Fraction add(Fraction that) {//add this and that together,;remember to consider the denominator here!

• Define an equals(Object o) function that has the form:

public boolean equals(Object other)

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

Fraction that = (Fraction) other //todo: code goes here

}

Sample Output

a:1\\2

b:3\\4

c:3\\4

a:10\\8

b:3\\4

c:3\\4

a.equals(b):false

b.equals(c):true

//Driver

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));

Solution

Hi, Please find my code.

Please let me know in case of any issue.

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

public class Fraction {

   /**

   * instance variable

   */

   public final int numerator;

   /**

   * instance variable

   */

   public final 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);

   }

  

}


############## FractionDriver.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

*/

Fraction class • Numerators and denominators are unchangeable once set by the constructor. • No denominator will be stored as a 0. (ie, no DivideByZero Exceptio
Fraction class • Numerators and denominators are unchangeable once set by the constructor. • No denominator will be stored as a 0. (ie, no DivideByZero Exceptio
Fraction class • Numerators and denominators are unchangeable once set by the constructor. • No denominator will be stored as a 0. (ie, no DivideByZero Exceptio
Fraction class • Numerators and denominators are unchangeable once set by the constructor. • No denominator will be stored as a 0. (ie, no DivideByZero Exceptio
Fraction class • Numerators and denominators are unchangeable once set by the constructor. • No denominator will be stored as a 0. (ie, no DivideByZero Exceptio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site