HW3 all Right

This commit is contained in:
2025mohseni
2026-06-04 23:56:47 +03:30
parent 6a01e43eb2
commit bd0d2de7d5
5 changed files with 125 additions and 128 deletions
+18 -32
View File
@@ -4,7 +4,6 @@ public class Rational {
private int numerator;
private int denominator;
// Constructor
public Rational(int numerator, int denominator) {
if (denominator == 0) {
throw new IllegalArgumentException("Denominator cannot be zero");
@@ -14,7 +13,6 @@ public class Rational {
simplify();
}
// GCD helper for simplification
private int gcd(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
@@ -26,73 +24,62 @@ public class Rational {
return a;
}
// Simplify the fraction
private void simplify() {
int gcd = gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
// Keep denominator positive
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
// 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);
}
// COMPLETED - Static method: r1 + r2
public static Rational add(Rational r1, Rational r2) {
return r1.add(r2);
}
// TODO: Instance method - this - other
// Formula: a/b - c/d = (a*d - c*b) / (b*d)
// 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 newNumerator = this.numerator * other.denominator - other.numerator * this.denominator;
int newDenominator = this.denominator * other.denominator;
return new Rational(newNumerator, newDenominator);
}
// TODO: 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)
// (a/b) * (c/d) = (a*c) / (b*d)
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;
return new Rational(newNumerator, newDenominator);
}
// TODO: 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)
// (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 IllegalArgumentException("Cannot divide by zero");
}
int newNumerator = this.numerator * other.denominator;
int newDenominator = this.denominator * other.numerator;
return new Rational(newNumerator, newDenominator);
}
// TODO: 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
@Override
public String toString() {
if (denominator == 1) {
@@ -101,7 +88,6 @@ public class Rational {
return numerator + "/" + denominator;
}
// PROVIDED - Decimal value
public double toDouble() {
return (double) numerator / denominator;
}