Question: Indicate five errors in the following Fraction class. Include no more than one error per constructor/method. (5 pts.) public class Fraction { private int numer;
Indicate five errors in the following Fraction class. Include no more than one error per
constructor/method. (5 pts.)
public class Fraction { private int numer;
private int denom;
public Fraction() { // no-arg constructor
numer = 0;
denom = 1;
}
public Fraction(int numerator, int denominator) {
numerator = numer;
denominator = denom;
}
public Fraction(Fraction fraction) { // copy constructor
numer = frac.getNumer();
denom = frac.getDenom();
}
// getters and setters
public void getNumer() {
return numer;
}
// ^ Cant return a void!
public void setNumer(int x) {
numer == x;
}
public int getDenom() {
return denom;
}
public void setDenom(int x) {
if (x != 0)
denom = x;
else
throw ArithmeticException(); // missing "new"
}
// Special Methods
public String toString() {
return numer + "/" + denom;
}
// Relational operator methods
public String equals(Fraction rFrac) {
Fraction temp_frac1 = new Fraction(getNumer(), getDenom());
Fraction temp_frac2 =
new Fraction(rFrac.getNumer(), rFrac.getDenom());
temp_frac1 = temp_frac1.reduce();
temp_frac2 = temp_frac2.reduce();
if ((temp_frac1.numer == temp_frac2.numer) &&
(temp_frac1.denom == temp_frac2.denom))
return "Fractions found equal";
else
return "Fraction not equal";
}
public Fraction lessThan(Fraction rFrac) {
Fraction tempFrac1 = new Fraction(getNumer() * rFrac.getDenom(),
getDenom() * rFrac.getDenom());
Fraction tempFrac2 = new Fraction(rFrac.getNumer() * getDenom(),
rFrac.getDenom() * getDenom());
return tempFrac1.getNumer() < tempFrac2.getNumer();
}
public boolean greaterThan(Fraction rFrac) {
return equals(rFrac) && !lessThan(rFrac);
}
public Fraction add(Fraction rFrac) {
Fraction tempFrac1 = new Fraction(getNumer() * rFrac.getDenom(),
getDenom() * rFrac.getDenom());
Fraction tempFrac2 = new Fraction(rFrac.getNumer() * getDenom(),
rFrac.getDenom() * getDenom());
return new Fraction(tempFrac1.getNumer() + tempFrac2.getNumer(),
tempFrac1.getDenom()).reduce();
}
public Fraction subtract(Fraction rFrac){
int num1 =((this.numer * rFrac.denom) - (rFrac.numer * this.denom));
int num2 = this.denom * rFrac.denom;
Fraction x = new Fraction (num1, num2);
return x.reduce();
}
public Fraction multiply (Fraction frac1, Fraction frac2) {
int num1 = frac1.numer * frac2.numer;
int num2 = frac1.denom * frac2.denom;
Fraction x = new Fraction (num1, num2);
return x.reduce();
}
// Other Methods
public Fraction reduce() {
// assume implemented correctly
}
// Private Methods
private int gcd(int n1, int n2) {
// assume implemented correctly
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
