develope #1
@@ -41,9 +41,9 @@ public class Rational {
|
||||
|
||||
// COMPLETED - Instance method: this + other
|
||||
public Rational add(Rational other) {
|
||||
int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator;
|
||||
int newDenominator = this.denominator * other.denominator;
|
||||
return new Rational(newNumerator, newDenominator);
|
||||
int newNum = this.numerator * other.denominator + other.numerator * this.denominator;
|
||||
int newDenom = this.denominator * other.denominator;
|
||||
return new Rational(newNum, newDenom);
|
||||
}
|
||||
|
||||
// COMPLETED - Static method: r1 + r2
|
||||
@@ -54,42 +54,53 @@ public class Rational {
|
||||
// TODO: Instance method - this - other
|
||||
// Formula: a/b - c/d = (a*d - c*b) / (b*d)
|
||||
public Rational subtract(Rational other) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int newNum = this.numerator * other.numerator - other.numerator * this.denominator;
|
||||
int newDenom = other.numerator * this.denominator;
|
||||
return new Rational(newNum, newDenom);
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 - r2
|
||||
public static Rational subtract(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int newNum = r1.numerator * r2.denominator - r2.numerator * r1.denominator;
|
||||
int newDenom = r1.denominator * r2.denominator;
|
||||
return new Rational(newNum, newDenom);
|
||||
}
|
||||
|
||||
// TODO: Instance method - this * other
|
||||
// Formula: (a/b) * (c/d) = (a*c) / (b*d)
|
||||
public Rational multiply(Rational other) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int newNum = this.numerator * other.numerator;
|
||||
int newDenom = this.denominator * other.denominator;
|
||||
return new Rational(newNum, newDenom);
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 * r2
|
||||
public static Rational multiply(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int newNum = r1.numerator * r2.numerator;
|
||||
int newDenom = r1.denominator * r2.denominator;
|
||||
return new Rational(newNum, newDenom);
|
||||
}
|
||||
|
||||
// TODO: Instance method - this / other
|
||||
// Formula: (a/b) ÷ (c/d) = (a*d) / (b*c)
|
||||
public Rational divide(Rational other) {
|
||||
// YOUR CODE HERE
|
||||
// HINT: Division is multiplication by reciprocal
|
||||
// Don't forget to check if other.numerator is zero!
|
||||
return null; // Remove this line when implemented
|
||||
if (other.numerator == 0){
|
||||
throw new ArithmeticException("Cannot divide by zero");
|
||||
}
|
||||
int newNum = this.numerator * other.denominator;
|
||||
int newDenom = this.denominator * other.numerator;
|
||||
return new Rational(newNum, newDenom);
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 / r2
|
||||
public static Rational divide(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
if (r2.numerator == 0){
|
||||
throw new ArithmeticException("Cannot divide by zero");
|
||||
}
|
||||
int newNum = r1.numerator * r2.denominator;
|
||||
int newDenom = r1.denominator * r2.numerator;
|
||||
|
||||
return new Rational(newNum, newDenom);
|
||||
}
|
||||
|
||||
// PROVIDED - String representation
|
||||
|
||||
Reference in New Issue
Block a user