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

The Float and Double classes (and the respective primitive types) offer a good r

ID: 3745095 • Letter: T

Question

The Float and Double classes (and the respective primitive types) offer a good representation of real values suitable for many numeric problems. However, the finite precision of those types can sometimes lead to surprising results (try to square 0.1 as float or double :( )1 . In some cases, such problems can be remedied by using a Fraction class. Implement a class Fraction that models fractions, where numerator and denominator are represented as int (or Integer) and offers arithmetic operations. A Fraction object can be created with a constructor that takes a numerator and a denominator as arguments. The Fraction class offers the public methods defined below. Note, the results of arithmetic operations should be simplified. e.g., multiplying two fractions 4/3 and 3/5 should return 4/5 (not 12/15 )^2 .

• Choose appropriate types for the methods’ arguments and return values.

• Add instance variables to represent numerator and denominator.

• Implement the methods.

• Implement a tester class that exercises the methods. Do not only test behavior that is well defined. e.g., what does happen, if you create a fraction 1/0?

k x Fraction is a class modeling fractions where numerator * and denominator are represented as integral number public class Fraction kx * Constructs a new Fraction object. * Oparam init initial value public Fraction(/* SOMETYPEnum, / SOMETYPE / denom) { /* YOUR CODE ) k* * Computes " this+that" and returns the result in a new object * Oparam that the left-hand side operand * Oreturn a new object representing this+that public /* SOMETYPE */ add(/* SOMETYPE that) { /x YOUR CODE*/ k* * Computes " this-that" and returns the result in a new object * Oparam that the left-hand side operand * Oreturn a new object representing this-that public /* SOMETYPE */ sub(/* SOM ETYPE */ that) { /* YOUR CODE */ } k* * Computes "this*that" and returns the result in a new object * Oparam that the left-hand side operand A recent article on issues with floating point arithmetic is available here: https ://cacm.acm.org/magazines/ 2017/8/219594-small-data-computing/fulltext https://en.wikipedia.org/wiki/Greatest_common_divisor

Explanation / Answer

class Fraction
{
private int numerator;
private int denominator;
public Fraction() //default constructor
{
numerator = 0;
denominator = 1;
}
public Fraction(int num,int denom) //parameterized constructor
{
  numerator = num;
  if(denominator > 0)// validation check
  denominator = denom;
  else
  denominator = 1;
}
public Fraction add(Fraction f1,Fraction f2) // add two fractions
{
  
  this.numerator = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
  this.denominator = f1.denominator * f2.denominator;
  return this;
}
public Fraction sub(Fraction f1,Fraction f2) // subtract a fraction from another
{

  this.numerator = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
  this.denominator = f1.denominator * f2.denominator;
  return this;
}
public Fraction mul(Fraction f1,Fraction f2) //multiply two fractions
{
  
  this.numerator = f1.numerator * f2.numerator;
  this.denominator = f1.denominator * f2.denominator;
  return this;
  
}
public Fraction div(Fraction f1,Fraction f2) //division of fractions
{
  this.numerator = f1.numerator * f2.denominator;
  this.denominator = f1.denominator * f2.numerator;
  return this;
}

public Fraction getQuotient()
{
  this.numerator = this.numerator/this.denominator;
  this.denominator = 1;
  return this;
}

public Fraction getRemainder()
{
  this.numerator = this.numerator%this.denominator;
  this.denominator = 1;
  return this;
}

public double doubleValue()
{
  return (double)(numerator)/denominator;
}
public String toString()   //override toString() method
{
return " "+numerator+"/"+denominator;
}


}
class FractionDemo
{
public static void main (String[] args)
{
  Scanner scan = new Scanner(System.in);
  System.out.println("Enter the numerator and denominator of f1 ->");
  int num1 = scan.nextInt();
  int den1 = scan.nextInt();
  Fraction f1 = new Fraction(num1,den1);
  
  System.out.println("Enter the numerator and denominator of f2 ->");
  int num2 = scan.nextInt();
  int den2 = scan.nextInt();
  Fraction f2 = new Fraction(num2,den2);
  
  System.out.println("f1 = "+f1.toString());
  System.out.println("f2 = "+f2.toString());
  
  
  
  Fraction f3 = new Fraction();
  Fraction f4 = new Fraction();
  Fraction f5 = new Fraction();
  Fraction f6 = new Fraction();
  
  System.out.println("The sum of fraction f1 and fraction f2 is "+ f3.add(f1,f2));
  System.out.println("The difference of fraction f1 and fraction f2 is "+ f4.sub(f1,f2));
  System.out.println("The multiplication of fraction f1 and fraction f2 is "+ f5.mul(f1,f2));
  System.out.println("The division of fraction f1 and fraction f2 is "+ f6.div(f1,f2));

  Fraction f7 = new Fraction(8,3);
  System.out.println("Quotient of f7 : "+f7.getQuotient());
  Fraction f8 = new Fraction(8,5);
  System.out.println("Remainder of f7 : "+f8.getRemainder());
  Fraction f9 = new Fraction(6,7);
  System.out.println("Double value of f7 : "+f9.doubleValue());
  
   
  // create a fraction with denominator =0
  
  Fraction f10 = new Fraction(1,0);
  
  System.out.println("Fraction f10 : "+f10);// it will create the fraction unless we made a check in constructor
  
  
}
}

Output:

Enter the numerator and denominator of f1 -> 7 8
Enter the numerator and denominator of f2 ->4 5
f1 = 7/8
f2 = 4/5
The sum of fraction f1 and fraction f2 is 67/40
The difference of fraction f1 and fraction f2 is 3/40
The multiplication of fraction f1 and fraction f2 is 28/40
The division of fraction f1 and fraction f2 is 35/32
Quotient of f7 : 2/1
Remainder of f7 : 3/1
Double value of f7 : 0.8571428571428571

Do ask if any doubt. Please upvote.

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