Reputation: 21
public class Number {
private int numerator;
private int denominator;
public Number(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public Number() {
this.numerator = 0;
this.denominator = 1;
}
public int getDenominator() {
return denominator;
}
public int getNumerator() {
return numerator;
}
public String toString() {
if (numerator == 0) {
return "0";
} else if (denominator == 1) {
return denominator + "";
}
return numerator + "/" + denominator;
}
public Number subtract(Number other) {
numerator = (this.getNumerator() * other.getDenominator()) - (other.getNumerator() * this.getDenominator());
denominator = this.getDenominator() * other.getDenominator();
return this;
}
////
public class test {
public static void main(String[] args) {
Number n1 = new Number(1, 2);
Number n2 = new Number(3, 5);
Number n4 = new Number(7, 40);
Number test = n2.subtract(n1);
System.out.println(test);
Number test2 = n4.subtract(n1);
System.out.println(test2);
Number test3 = n1.subtract(n4);
System.out.println(test3);
Number test4 = n2.subtract(n1);
System.out.println(test4);
}
}
I'm having trouble with the Number subtract() method. When I call it from the client, it works some of the time, but after several successive calls, I am getting unexpected outputs. The first two calls work fine, then I start to get results which are way off. I know that I am missing something in the subtract() method, just not sure what it is.
Any help would be appreciated.
Upvotes: 1
Views: 85
Reputation: 21
I ended up solving my own problem but I appreciate everyones help. I know this might not be the most efficient way to solve this problem, but I am only a first year CS student. If anyone has any suggestions to clean this code up, I would love to hear it. Thanks!
public class Number {
private int numerator;
private int denominator;
public Number(int numerator, int denominator) {
reduce(numerator, denominator);
}
public Number() {
this.numerator = 0;
this.denominator = 1;
}
public int getDenominator() {
return denominator;
}
public int getNumerator() {
return numerator;
}
public String toString() {
if (numerator == 0) {
return "0";
} else if (denominator == 1) {
return numerator + "";
}
return numerator + "/" + denominator;
}
public Number add(RationalNumber other) {
int numerator1 = (numerator * other.getDenominator()) + (other.getNumerator() * denominator);
int denominator1 = denominator * other.getDenominator();
return new Number(numerator1, denominator1);
}
public Number subtract(RationalNumber other) {
int numerator1 = (numerator * other.getDenominator()) - (other.getNumerator() * denominator);
int denominator1 = denominator * other.getDenominator();
return new Number(numerator1, denominator1);
}
public Number multiply(RationalNumber other) {
int numerator1 = numerator * other.getNumerator();
int denominator1 = denominator * other.getDenominator();
return new Number(numerator1, denominator1);
}
public Number divide(RationalNumber other) {
int numerator1 = numerator * other.getDenominator();
int denominator1 = denominator * other.getNumerator();
return new Number(numerator1, denominator1);
}
private void reduce(int numerator, int denominator) {
int factor = 1;
int temp = 0;
if (Math.abs(numerator) > Math.abs(denominator)) {
temp = Math.abs(numerator);
} else if (Math.abs(denominator) > Math.abs(numerator)) {
temp = Math.abs(denominator);
}
for (int i = temp; i > 0; i --) {
if ((numerator %i == 0) && (denominator %i == 0)) {
factor = i;
i = 0;
}
}
numerator = numerator / factor;
denominator = denominator / factor;
rightForm(numerator, denominator);
}
private void rightForm(int numerator, int denominator) {
if (numerator > 0 && denominator < 0) {
this.numerator = numerator - numerator - numerator;
this.denominator = Math.abs(denominator);
} else if (numerator < 0 && denominator < 0) {
this.numerator = Math.abs(numerator);
this.denominator = Math.abs(denominator);
} else {
this.numerator = numerator;
this.denominator = denominator;
}
}
}
Upvotes: 0
Reputation: 10789
If you use subtract method on object, change last line to return this
.
By the way, your substraction for ratio types is wrong.
Upvotes: 2