For this programming assignment, you are to complete the Fraction class as descr
ID: 3676978 • Letter: F
Question
For this programming assignment, you are to complete the Fraction class as described below. You are to test your class with the provided test driver.
Modifications to make
Modify the given Fraction class as follows:
Modify setters so that they ignore inappropriate values.
Implement the equals() method.
Implement lessThan() and greaterThan() methods.
Implement add(), subtract(), and multiply() methods.
In case of subtract(), please check the number is positive or negative. If the number is negative, regard its numerator is negative and mark the signal flag on and perform the operation accordingly, and apply it to the final result.
Modify Test Driver Program, class FractionDriver, to test the added new methods including lessThan(), greaterThan(), add(), subtract(), and multiply() methods.
Class Fraction
public Fraction(int numer, int denom) {
this.numerator = numer;
this.denominator = denom;
}
public Fraction(Fraction f) {
this.numerator = f.getNumerator();
this.denominator = f.getDenominator();
}
// reduce : convert the "this" fraction to an equivalent one
// based on a greatest common denominator
public Fraction reduce() {
Fraction temp = new Fraction();
int GCD = gcd(numer, denom);
temp.setNumerator(numerator / GCD);
temp.setDenominator(denominator / GCD);
return temp;
}
// gcd : greatest common denominator, used to reduce fractions
private int gcd(int n1, int n2) {
int M, N, R;
if (n1 < n2) {
N = n1;
M = n2;
} else {
N = n2;
M = n1;
}
R = M % N;
while (R != 0) {
M = N;
N = R;
R = M % N;
}
return N;
}
// lcd : lowest common denominator used to add and subtract fractions
private int lcd(int n1, int n2) {
int factor = n1;
while ((n1%n2)!=0) {
n1 += factor;
}
return n1;
}
// convert : convert a fraction to an equivalent one based on
// a lowest common denominator
private Fraction convert(int common) {
Fraction result = new Fraction();
int factor = common / denominator;
result.nemerator = numerator * factor;
result.denominator = common;
return result;
}
...
public class FractionDriver {
public static void main(String[] args) {
// test constructors (and getters)
Fraction frac0 = new Fraction();
System.out.println("TESTING NO-ARG CONSTRUCTOR");
System.out.println("Numer = " + frac0.getNumerator());
System.out.println("Denom = " + frac0.getDenominator());
System.out.println("TESTING int/int CONSTRUCTOR");
Fraction frac1 = new Fraction(2,4);
System.out.println("Numer = " + frac1.getNumerator());
System.out.println("Denom = " + frac1.getDenominator());
System.out.println("TESTING Fraction CONSTRUCTOR");
Fraction frac2 = new Fraction(frac1);
System.out.println("Numer = " + frac2.getNumerator());
System.out.println("Denom = " + frac2.getDenominator());
// equal check --> You should declare equals() method inside Fraction class
if (frac1.equals(frac2)) {
System.out.println("frac1 and frac2 found equal");
} else {
System.out.println("frac1 and frac2 NOT equal");
}
// test reduce
Fraction reduced_frac1 = frac1.reduce();
System.out.println("Reduced frac1 = " + reduced_frac1);
// test copy constructor
if (frac1.getNumerator() == frac2.getNumerator() &&
frac1.getDenominator() == frac2.getDenominator() &&
frac1 != frac2) {
System.out.println("Copy constructor working");
} else
System.out.println("PROBLEM with copy constructor");
// test setters
frac2.setNumerator(8);
frac2.setDenominator(12);
System.out.println("Numer = " + frac2.getNumerator());
System.out.println("Denom = " + frac2.getDenominator());
// System.out.println("GCD of 2/4 = " + frac1.gcd(1,4));
// add new statements to test methods including lessThan(), greaterThan(), add(), subtract(), and multiply() methods.
}
}
Explanation / Answer
public class Fraction {
// member variables
private int numerator, denominator; // stores the fraction data
// Constructors
public Fraction() {// non-argument constructor
numerator = 0;
denominator = 1;
}
public Fraction(int numer, int denom) {
this.numerator = numer;
this.denominator = denom;
}
public Fraction(Fraction f) {
this.numerator = f.getNumerator();
this.denominator = f.getDenominator();
}
// Getters and Setters
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
public void setNumerator(int num) {
numerator = num;
}
public void setDenominator(int den) {
denominator = den;
}
public String toString() {
return numerator + "/" + denominator;
}
// reduce : convert the "this" fraction to an equivalent one
// based on a greatest common denominator
public Fraction reduce() {
Fraction temp = new Fraction();
int GCD = gcd(getNumerator(), getDenominator());
temp.setNumerator(numerator / GCD);
temp.setDenominator(denominator / GCD);
return temp;
}
// gcd : greatest common denominator, used to reduce fractions
public int gcd(int n1, int n2) {
int M, N, R;
if (n1 < n2) {
N = n1;
M = n2;
} else {
N = n2;
M = n1;
}
R = M % N;
while (R != 0) {
M = N;
N = R;
R = M % N;
}
return N;
}
// lcd : lowest common denominator used to add and subtract fractions
private int lcd(int n1, int n2) {
int factor = n1;
while ((n1 % n2) != 0) {
n1 += factor;
}
return n1;
}
// convert : convert a fraction to an equivalent one based on
// a lowest common denominator
private Fraction convert(int common) {
Fraction result = new Fraction();
int factor = common / denominator;
result.numerator = numerator * factor;
result.denominator = common;
return result;
}
/**
* operation(+): addition between fractions
*
* @param b
* @return result of addition
*/
public Fraction addition(Fraction b) {
int numerator = (this.numerator * b.denominator)
+ (b.numerator * this.denominator);
int denominator = this.denominator * b.denominator;
return new Fraction(numerator, denominator);
}
/**
* operation(+): addition between fractions
*
* @param a
* @param b
* @return result of addition
*/
public static Fraction addition(Fraction a, Fraction b) {
int numerator = (a.numerator * b.denominator)
+ (b.numerator * a.denominator);
int denominator = a.denominator * b.denominator;
return new Fraction(numerator, denominator);
}
/**
* @param b
* @return
*/
public Fraction subtraction(Fraction b) {
return addition(this, new Fraction(-b.numerator, b.denominator));
}
/**
* if this and b is lessthan
*
* @param b
* @return
*/
public boolean lessThan(Fraction b) {
double frac1Val = (double) this.numerator / (double) this.denominator;
double frac2Val = (double) b.numerator / (double) b.denominator;
if (frac1Val > frac2Val)
return false;
else
return true;
}
/**
* if this and b is lessthan
*
* @param b
* @return
*/
public boolean greaterThan(Fraction b) {
double frac1Val = (double) this.numerator / (double) this.denominator;
double frac2Val = (double) b.numerator / (double) b.denominator;
if (frac1Val < frac2Val)
return false;
else
return true;
}
/**
* method to multiply
*
* @param b
* @return
*/
public Fraction multiply(Fraction b) {
// check preconditions
if ((denominator == 0) || (b.denominator == 0))
throw new IllegalArgumentException("invalid denominator");
// create new fraction to return as product
Fraction product = new Fraction();
// calculate product
product.numerator = numerator * b.numerator;
product.denominator = denominator * b.denominator;
// reduce the resulting fraction
product = product.reduce();
return product;
}
/**
* method to devide
*
* @param b
* @return
*/
public Fraction divide(Fraction b) {
// check preconditions
if ((denominator == 0) || (b.numerator == 0))
throw new IllegalArgumentException("invalid denominator");
// create new fraction to return as result
Fraction result = new Fraction();
// calculate result
result.numerator = numerator * b.denominator;
result.denominator = denominator * b.numerator;
// reduce the resulting fraction
result = result.reduce();
return result;
}
@Override
public boolean equals(Object obj) {
return this.toString().equals(((Fraction) obj).toString());
}
} // end of class Fraction
public class FractionDriver {
public static void main(String[] args) {
// test constructors (and getters)
Fraction frac0 = new Fraction();
System.out.println("TESTING NO-ARG CONSTRUCTOR");
System.out.println("Numer = " + frac0.getNumerator());
System.out.println("Denom = " + frac0.getDenominator());
System.out.println("TESTING int/int CONSTRUCTOR");
Fraction frac1 = new Fraction(2, 4);
System.out.println("Numer = " + frac1.getNumerator());
System.out.println("Denom = " + frac1.getDenominator());
System.out.println("TESTING Fraction CONSTRUCTOR");
Fraction frac2 = new Fraction(frac1);
System.out.println("Numer = " + frac2.getNumerator());
System.out.println("Denom = " + frac2.getDenominator());
// equal check --> You should declare equals() method inside Fraction
// class
if (frac1.equals(frac2)) {
System.out.println("frac1 and frac2 found equal");
} else {
System.out.println("frac1 and frac2 NOT equal");
}
// test reduce
Fraction reduced_frac1 = frac1.reduce();
System.out.println("Reduced frac1 = " + reduced_frac1);
// test copy constructor
if (frac1.getNumerator() == frac2.getNumerator()
&& frac1.getDenominator() == frac2.getDenominator()
&& frac1 != frac2) {
System.out.println("Copy constructor working");
} else
System.out.println("PROBLEM with copy constructor");
// test setters
frac2.setNumerator(8);
frac2.setDenominator(12);
System.out.println("Numer = " + frac2.getNumerator());
System.out.println("Denom = " + frac2.getDenominator());
System.out.println("GCD of 2/4 = " + frac1.gcd(1, 4));
// Testing of lessThan(), greaterThan(), add(), subtract(), divide() and
// multiply() methods.
System.out.println("Addition of " + frac1 + " and " + frac2 + " "
+ frac1.addition(frac2));
System.out.println("Subtraction of " + frac1 + " and " + frac2 + " "
+ frac1.subtraction(frac2));
System.out.println("Multiplication of " + frac1 + " and " + frac2 + " "
+ frac1.multiply(frac2));
System.out.println("Devison of " + frac1 + " and " + frac2 + " "
+ frac1.divide(frac2));
System.out.println("Lessthan of " + frac1 + " and " + frac2 + " "
+ frac1.lessThan(frac2));
System.out.println("Greaterthan of " + frac1 + " and " + frac2 + " "
+ frac1.greaterThan(frac2));
}
}
OUTPUT:
TESTING NO-ARG CONSTRUCTOR
Numer = 0
Denom = 1
TESTING int/int CONSTRUCTOR
Numer = 2
Denom = 4
TESTING Fraction CONSTRUCTOR
Numer = 2
Denom = 4
frac1 and frac2 found equal
Reduced frac1 = 1/2
Copy constructor working
Numer = 8
Denom = 12
GCD of 2/4 = 1
Addition of 2/4 and 8/12 56/48
Subtraction of 2/4 and 8/12 -8/48
Multiplication of 2/4 and 8/12 1/3
Devison of 2/4 and 8/12 3/4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.