Compare commits
6
Commits
main
...
ce0861233b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce0861233b | ||
|
|
f1ae92771a | ||
|
|
31cebd8cc6 | ||
|
|
ff17c1de49 | ||
|
|
14fe385b17 | ||
|
|
919e7bc9f7 |
@@ -51,45 +51,56 @@ public class Rational {
|
||||
return r1.add(r2);
|
||||
}
|
||||
|
||||
// TODO: Instance method - this - other
|
||||
// Formula: a/b - c/d = (a*d - c*b) / (b*d)
|
||||
// COMPLETED - Instance method - this - other
|
||||
public Rational subtract(Rational other) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int newNumerator = (this.numerator * other.denominator - other.numerator * this.denominator);
|
||||
int newDenominator = this.denominator * other.denominator;
|
||||
|
||||
Rational result = new Rational(newNumerator, newDenominator);
|
||||
result.simplify();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 - r2
|
||||
// COMPLETED - Static method - r1 - r2
|
||||
public static Rational subtract(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
return r1.subtract(r2);
|
||||
}
|
||||
|
||||
// TODO: Instance method - this * other
|
||||
// Formula: (a/b) * (c/d) = (a*c) / (b*d)
|
||||
// COMPLETED - Instance method - this * other
|
||||
public Rational multiply(Rational other) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int newNumerator = this.numerator * other.numerator;
|
||||
int newDenominator = this.denominator * other.denominator;
|
||||
|
||||
Rational result = new Rational(newNumerator, newDenominator);
|
||||
result.simplify();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 * r2
|
||||
// COMPLETED - Static method - r1 * r2
|
||||
public static Rational multiply(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
return r1.multiply(r2);
|
||||
}
|
||||
|
||||
// TODO: Instance method - this / other
|
||||
// Formula: (a/b) ÷ (c/d) = (a*d) / (b*c)
|
||||
// COMPLETED - Instance method - this / other
|
||||
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("Caught division by zero");
|
||||
}
|
||||
|
||||
int newNumerator = this.numerator * other.denominator;
|
||||
int newDenominator = this.denominator * other.numerator;
|
||||
|
||||
Rational result = new Rational(newNumerator, newDenominator);
|
||||
result.simplify();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 / r2
|
||||
// COMPLETED - Static method - r1 / r2
|
||||
public static Rational divide(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
return r1.divide(r2);
|
||||
}
|
||||
|
||||
// PROVIDED - String representation
|
||||
|
||||
Reference in New Issue
Block a user