From 919e7bc9f7a55400d5ae0bccb9ac000fb31cd842 Mon Sep 17 00:00:00 2001 From: Ramtin_Jafari Date: Sun, 26 Apr 2026 18:33:18 +0330 Subject: [PATCH] implement subtract static and instance method --- src/main/java/Rational/Rational.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/Rational/Rational.java b/src/main/java/Rational/Rational.java index 7bd840e..75f1f34 100644 --- a/src/main/java/Rational/Rational.java +++ b/src/main/java/Rational/Rational.java @@ -51,17 +51,20 @@ 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.denominator * 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