Question
//Definition of Fraction class class Fraction { //Declaration of private data members private int num; private int denom; //Definition of parameterized constructor public Fraction(int n,
//Definition of Fraction class class Fraction { //Declaration of private data members private int num; private int denom;
//Definition of parameterized constructor public Fraction(int n, int d) { num = n; this.setDenom(d); this.reduceFraction(); this.adjustSign(); }
//Definition of setNum method that is used to set the value of num variable public void setNum(int n) { num = n; }
//Definition of setNum method that is used to set the value of denom variable public void setDenom(int d) { //If condition to check if the value of denominator is zero if(d!=0) { denom = d; } //if value is zero reseting the value to 1 and printing message to screen else{ System.out.println("For Fraction: "+this.toString() + ": "); System.out.println("Denominator cannot be zero; denominator value is being reset to one"); denom = 1; System.out.println("The Fraction is now : " + this.toString()); } }
//Definition of getter method for num public int getNum() { return num; } //Definition of getter method for denom public int getDenom() { return denom; }
//Definition of multiply method that takes a fraction object as input and performs the fraction multiplication with calling object public Fraction multiply(Fraction f2) { int newNum, newDenom;
newNum = (this.getNum() * f2.getNum()); newDenom = (this.getDenom() * f2.getDenom());
Fraction res = new Fraction(newNum, newDenom); //reducing the resulting fraction into simplest form res.reduceFraction(); return res; } //Definition of toString() method that prints the fraction with format public String toString() { String strFraction;
if(denom ==1) { strFraction = "( " + num + " )"; } else { strFraction = "( " + num + " / " + denom + " )"; } return strFraction; }
public void reduceFraction() { int d = __gcd(this.num, this.denom); this.num = this.num / d; this.denom = this.denom / d; }
public int __gcd(int a, int b) { if (b == 0) return a; return __gcd(b, a % b); }
public void adjustSign() { if(this.denom
public Fraction divide(Fraction f2) { int newNum, newDenom;
newNum = this.num * f2.denom; newDenom = this.denom * f2.num; Fraction res = new Fraction(newNum, newDenom); res.reduceFraction(); return res; }
public Fraction add(Fraction f2) { int newNum, newDenom; int gcd = __gcd(this.denom, f2.denom); // Denominator of resulting fraction would be the LCM of both denominators // finding LCM of both denominator using GCD // LCM * GCD = a * b newDenom = (this.denom * f2.denom) / gcd; // Changing the fractions to have same denominator // Numerator of the resulting fraction can now be calculated newNum = (this.num)*(newDenom/this.denom) + (f2.num)*(newDenom/f2.denom);
Fraction res = new Fraction(newNum, newDenom); //reducing the resulting fraction into simplest form res.reduceFraction(); return res; }
public Fraction substract(Fraction f2) { int newNum, newDenom; int gcd = __gcd(this.denom, f2.denom); // Denominator of resulting fraction would be the LCM of both denominators // finding LCM of both denominator using GCD // LCM * GCD = a * b newDenom = (this.denom * f2.denom) / gcd; // Changing the fractions to have same denominator // Numerator of the resulting fraction can now be calculated newNum = (this.num)*(newDenom/this.denom) - (f2.num)*(newDenom/f2.denom); Fraction res = new Fraction(newNum, newDenom); //reducing the resulting fraction into simplest form res.reduceFraction(); return res; }
//definition of getDouble method that returns the floating point value of the fraction public double getDouble() { double res = (double)this.num / (double)this.denom; return res; } }
Much of the code is now correct, but there are still bugs, primarily in the add( ) and subtract( ) methods. Attached is a screenshot to illustrate how to fix the add( ) method; you would do something similar for the subtract( ) method. Note that when you access the information is a field, you should use the getter( ) method to do this.can somone make this correction to the code above?
public Fraction add(Fraction F2) int newNum, newDenom; // int gcd = gcd(this.denom, F2. denom); BUG HERE; THIS IS WRONG PLACE TO DO THIS // Denominator of resulting fraction would be the LCM of both denominators // finding LCM of both denominator using GCD //LCMGCD=ab // newDenom = (this.denom F2.denom) / gcd; BUG HERE; WRONG PLACE TO DIVIDE BY GCD newDenom = (this.getDenom()* F2.getDenom()); // Changing the fractions to have same denominator // Numerator of the resulting fraction can now be calculated // newNum = (this.num)*(newDenom/this. denom) + (F2.num)*(newDenom/F2.denom); BUG HERE, FIXED NEXT LINE newNum = (this.getNum()* F2.getDenom()) + (F2.getNum() * this.getDenom()); Fraction res = new Fraction(newNum, newDenom); // reducing the resulting fraction into simplest form // res.reduceFraction(); NOT NEEDED; WAS ALREADY DONE IN CONSTRUCTOR return res; \}Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started