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

Assignment 0 Fractions Note: You can work on this assignment individually or wit

ID: 3877993 • Letter: A

Question

Assignment 0 Fractions Note: You can work on this assignment individually or with one partner Objectives: types, objects, classes, "mai" due on September 10 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 : 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, eg. multiplying two fractions should return (not 2 Fraction is a class modeling fractions where numerator * and denominator are represented as integral number public class Fraction * Constructs a new Fraction object. @param init initial value public Fraction(/t SOMETYPE */ num. /* SOMETYPE */ denom) { /YOUR CODE */ } * Computes "this+that" and returns the result in a new object. Oparam that the left-hand side operand *Qreturn a new object representing this+that public /* SOM ETYPE */ add(/-SOMETYPE */ that) { /* YOUR CODE +/ } Computes "this-that" and returns the result in a new object. * Qparam that the left-hand side operand Oreturn a new object representing this-that public SOMETYPEsub SOMETYPE s/ that)YOUR CODE Computes "this*that" and returns the result in a new object Oparam that the left-hand side operand 'A recent article on isues with floating point arithmetic is available here: https://ca.a.org/magazines/ https://en.wikipedia.org/wiki/Greatest common_divisor 2017/8/219594-small-data-computing/fulltext

Explanation / Answer

Given below are the Fraction and FractionTester class needed for the question. Please copy back the comments above methods from your pdf / code template given in the image. Just a suggestion - When you give the question, always provide any code that is in the pdf as text. This way your original order of code is maintained and comments are not lost. So after uploading the pdf / image of question, if you have the text for the code, please paste the text as well.

Hope the answer helped. If it did, please don't forget to rate the answer. Thank you very much.

Fraction.java


public class Fraction {
public Fraction(int num, int denom)
{
numerator = num;
denominator = denom;
}
public Fraction add(Fraction that)
{
//make the denominators same
int resDenominator = this.denominator * that.denominator;
//now adjust the numerators accoringly using the multiplying factor
int resNumerator = this.numerator * that.denominator + that.numerator * this.denominator;
//simplify the result
int gcd = GCD(resNumerator, resDenominator);
resNumerator /= gcd;
resDenominator /= gcd;
return new Fraction(resNumerator, resDenominator);
}
public Fraction sub(Fraction that)
{
//make the denominators same
int resDenominator = this.denominator * that.denominator;
//now adjust the numerators accoringly using the multiplying factor
int resNumerator = this.numerator * that.denominator - that.numerator * this.denominator;
//simplify the result
int gcd = GCD(resNumerator, resDenominator);
resNumerator /= gcd;
resDenominator /= gcd;
return new Fraction(resNumerator, resDenominator);
}
public Fraction mul(Fraction that)
{
int resNumerator = this.numerator * that.numerator;
int resDenominator = this.denominator * that.denominator;
//simplify the result
int gcd = GCD(resNumerator, resDenominator);
resNumerator /= gcd;
resDenominator /= gcd;
return new Fraction(resNumerator, resDenominator);
}
public Fraction div(Fraction that)
{

int resNumerator = this.numerator * that.denominator;
int resDenominator = this.denominator * that.numerator;
//simplify the result
int gcd = GCD(resNumerator, resDenominator);
resNumerator /= gcd;
resDenominator /= gcd;
return new Fraction(resNumerator, resDenominator);
}
//choosing Double for return type , because if denominator is zero, we should return Not a Number
//ie. Double.NaN
public Double getQuotient()
{
if(denominator == 0)
return Double.NaN;
else
return new Double(numerator / denominator);
}
public int getRemainder()
{
if(denominator == 0)
return numerator;
else
return numerator % denominator;
}
public Double doubleValue()
{
if(denominator == 0) //division by zero
return Double.NaN;
else
return new Double(((double)numerator ) / denominator);
}
public String toString()
{
return numerator + "/" + denominator;
}
//returns the Greatest common divisor i.e the biggest number dividing both the numbers, which will be used to simplify the fraction
private int GCD(int num1, int num2)
{
int i = 1;
int gcd = 1;
while( i <= num1 && i <= num2)
{
if(num1 % i == 0 && num2 %i == 0)
gcd = i;
i++;
}
return gcd;
}
private int numerator;
private int denominator;
}

FractionTester.java


public class FractionTester {
public static void main(String[] args) {
Fraction f1 = new Fraction(2, 3);
Fraction f2 = new Fraction(3, 6);
System.out.println("f1 = " + f1);
System.out.println("f2 = " + f2);
System.out.println("f1 + f2 = " + f1 + " + " + f2 + " = " + f1.add(f2));
System.out.println("f1 - f2 = " + f1 + " - " + f2 + " = " + f1.sub(f2));
System.out.println("f1 * f2 = " + f1 + " * " + f2 + " = " + f1.mul(f2));
System.out.println("f1 / f2 = " + f1 + " / " + f2 + " = " + f1.div(f2));
Fraction f3 = new Fraction (7, 3);
System.out.println("f3 = " + f3);
System.out.println("f3.quotient = " + f3.getQuotient());
System.out.println("f3.remainder = " + f3.getRemainder());
System.out.println("f3.doublevalue = " + f3.doubleValue());
Fraction f4 = new Fraction (5, 0);
System.out.println("f4 = " + f4);
System.out.println("f3.quotient = " + f4.getQuotient());
System.out.println("f3.remainder = " + f4.getRemainder());
}
}

output

f1 = 2/3
f2 = 3/6
f1 + f2 = 2/3 + 3/6 = 7/6
f1 - f2 = 2/3 - 3/6 = 1/6
f1 * f2 = 2/3 * 3/6 = 1/3
f1 / f2 = 2/3 / 3/6 = 4/3
f3 = 7/3
f3.quotient = 2.0
f3.remainder = 1
f3.doublevalue = 2.3333333333333335
f4 = 5/0
f3.quotient = NaN
f3.remainder = 5