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

Improve the following class Fraction in the ways listed below. public class Frac

ID: 640977 • Letter: I

Question

Improve the following class Fraction in the ways listed below.

public class Fraction {

private int numerator;

private int denominator;

Fraction(int num, int denum) {

numerator = num;

denominator = 1;

}

Fraction(int num) {

numerator = num;

denominator = 1;

}

public String toString() {

return String.format("%d/%d", numerator, denominator);

}

public Fraction times(Fraction f) {

int num = numerator * f.numerator ;

int denum = denominator * f.denominator;

return new Fraction(num, denum);

}

}

1. Improve the contructors so that when a new Fraction is created, it is always in simplified form. For example, if you create a Fraction with the command Fraction frac1 = new Fractino(15,6);, then frac1 will have numerator 5 and denominator 2. Use the following static method in your Fraction class and then just use Fraction.gcd() any time you want the gcd of two ints:

private static int gcd(int num1, int num2) {

if(num2 == 0) {

return num1;

}

return gcd(num2, num1 % num2);

}

2. Encapsulate the .numerator and .denominator fields. Make both fields private and write public accessor methods .getNumerator() and .getDenominator() for accessing those values.

3. Add a .equals() method for telling whether two Fractions have the same value. This method should work even if the two Fractions are not in lowest terms. So for example, the fractions 10/6 and 15/9 should be considered equal. Hint: cross-multiplcation may come in useful here.

4. Add a .plus() method for adding one fraction to another. This means you have to remember how to add fractions, which means finding a common denominator. Note, you don't have to find the least common denominator.

5. Add a static .valueOf() method that takes an int and returns a Fraction version of the given int. So you can convert 5 into a Fraction by writing Fraction.valueOf(5).

6. Overload your methods .times(), .plus(), and .equals() so that they can accept an int for the arguement. This way if you have Fraction frac1 = new Fraction(2,3), you could multiply it by the int 9 by writing frac1.times(9) or test if it's equal to the int 2 by writing frac1.equals(2). Hint: don't re-implement anything. You can convert the int to a Fraction and then use the existing method: this.times(...)

Explanation / Answer

//Fraction.java
public class Fraction
{
  
   //instance variables of class Fraction
   private int numerator;
   private int denominator;
   Fraction(int num, int denum)
   {
       numerator = num;
       denominator = denum;

   }
   //fraction constructor
   Fraction(int num)
   {
       numerator = num;
       denominator = 1;
   }

   //Accssor method to return numerator

   public int getNumerator()
   {
       return numerator;
   }
   //Accssor method to return denominator

   public int getDenominator()
   {
       return denominator;
   }

   public String toString()
   {

       //call the method gcd and divide
       //the numerator and denominator values
       int gcd=gcd(numerator,denominator);      
       return String.format("%d/%d", numerator/gcd, denominator/gcd);
   }

   public Fraction times(Fraction f)
   {
       int num = numerator * f.numerator ;
       int denum = denominator * f.denominator;
       return new Fraction(num, denum);
   }
   private static int gcd(int num1, int num2)
   {
       return num2 == 0 ? num1 : gcd(num2,num1%num2);
   }
   //Returns true if the otherFraction object is equal to
   //the numerator and denominator values
   public boolean equals(Fraction otherFraction)
   {
       return numerator*otherFraction.getDenominator() ==denominator*otherFraction.getNumerator();

   }
   //overloading the method equals that accepts value
  
   public boolean equals(int value)
   {
       return numerator==value&& denominator==value;
   }

   //The method plus accepts a Fraction object and adds the numerator and denominator
   public void plus(Fraction otherFrac)
   {
       numerator= (numerator*otherFrac.getDenominator()+denominator*otherFrac.getNumerator());
       denominator=(denominator*otherFrac.getNumerator());      
   }
   //static version that returns a fraction version.

   public static Fraction valueOf(int num)
   {
       return new Fraction(num, 1);
   }
   //times metods that gets a number and multiplies
   //the fraction

   public void times(int num)
   {
       numerator=numerator*num;
       denominator=num*denominator;
   }

   //Overloading plus method plus accepts value and adds the numerator and denominator
   public void plus(int value)
   {
       numerator= (numerator+value);
       denominator=(denominator+value);      
   }


}

--------------------------------------------------------------------------------

//Tester class


/**
* The java program that tests the class Fraction .
* */
//FractionTester.java
public class FractionTester
{
   public static void main(String[] args)
   {

       //Create an instance of Fraction class
       Fraction fract=new Fraction(15, 6);
       //print its in similified form
       System.out.println(fract.toString());

       //Create an instance of Fraction class
       Fraction fract1=new Fraction(10, 6);
       //Create an instance of Fraction class
       Fraction fract2=new Fraction(15, 9);
      
       System.out.println("Fraction 1: "+fract1.toString());
       System.out.println("Fraction 2: "+fract2.toString());
      
       if(fract1.equals(fract2))
           System.out.println("Fractions are equal.");
       else
           System.out.println("Fractions are not equal.");

       fract1.plus(fract2);
       System.out.println("After adding fraction1 to fraction2,");
       System.out.println(fract1);
      
       System.out.println("Creating fraction version ");      
       Fraction newfraction=Fraction.valueOf(5);
       System.out.println(newfraction);
      
       //adding plus a value 5      
       fract1.plus(5);
       System.out.println(fract1);
      
       //checking equal funtion
      
       Fraction f1=new Fraction(2, 2);
      
       if(f1.equals(2))
           System.out.println("f1 is equlas times of "+f1.getNumerator()+" and "+f1.getDenominator());
      


   }
}


------------------------------------------------------------------------------------------------

Sample output:

5/2
Fraction 1: 5/3
Fraction 2: 5/3
Fractions are equal.
After adding fraction1 to fraction2,
2/1
Creating fraction version
5/1
37/19
f1 is equlas times of 2 and 2

Hope this helps you