From ff17c1de49dde055f7bc1c51cbbd3fd7da8f1263 Mon Sep 17 00:00:00 2001 From: Ramtin_Jafari Date: Sun, 26 Apr 2026 18:41:40 +0330 Subject: [PATCH] implement divide static and instance method --- src/main/java/Rational/Rational.java | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/Rational/Rational.java b/src/main/java/Rational/Rational.java index f335bfa..8b32d61 100644 --- a/src/main/java/Rational/Rational.java +++ b/src/main/java/Rational/Rational.java @@ -83,19 +83,24 @@ public class Rational { 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) { + return null; + } + + 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