merge branch develop to main #1

Merged
Meraj merged 6 commits from develop into main 2026-05-18 23:33:27 +00:00
Showing only changes of commit cd2006ec94 - Show all commits
+18 -15
View File
@@ -7,7 +7,8 @@ public class Rational {
// Constructor // Constructor
public Rational(int numerator, int denominator) { public Rational(int numerator, int denominator) {
if (denominator == 0) { if (denominator == 0) {
throw new IllegalArgumentException("Denominator cannot be zero"); // throw new IllegalArgumentException("Denominator cannot be zero");
System.out.println("Error: denominator cannot be zero");
} }
this.numerator = numerator; this.numerator = numerator;
this.denominator = denominator; this.denominator = denominator;
@@ -54,42 +55,44 @@ public class Rational {
// TODO: Instance method - this - other // TODO: Instance method - this - other
// Formula: a/b - c/d = (a*d - c*b) / (b*d) // Formula: a/b - c/d = (a*d - c*b) / (b*d)
public Rational subtract(Rational other) { public Rational subtract(Rational other) {
// YOUR CODE HERE int newNumerator = this.numerator * other.denominator - other.numerator * this.denominator;
return null; // Remove this line when implemented int newDenominator = this.denominator * other.denominator;
return new Rational(newNumerator, newDenominator);
} }
// TODO: Static method - r1 - r2 // TODO: Static method - r1 - r2
public static Rational subtract(Rational r1, Rational r2) { public static Rational subtract(Rational r1, Rational r2) {
// YOUR CODE HERE return r1.subtract(r2);
return null; // Remove this line when implemented
} }
// TODO: Instance method - this * other // TODO: Instance method - this * other
// Formula: (a/b) * (c/d) = (a*c) / (b*d) // Formula: (a/b) * (c/d) = (a*c) / (b*d)
public Rational multiply(Rational other) { public Rational multiply(Rational other) {
// YOUR CODE HERE int newNumerator = this.numerator * other.numerator;
return null; // Remove this line when implemented int newDenominator = this.denominator * other.denominator;
return new Rational(newNumerator, newDenominator);
} }
// TODO: Static method - r1 * r2 // TODO: Static method - r1 * r2
public static Rational multiply(Rational r1, Rational r2) { public static Rational multiply(Rational r1, Rational r2) {
// YOUR CODE HERE return r1.multiply(r2);
return null; // Remove this line when implemented
} }
// TODO: Instance method - this / other // TODO: Instance method - this / other
// Formula: (a/b) ÷ (c/d) = (a*d) / (b*c) // Formula: (a/b) ÷ (c/d) = (a*d) / (b*c)
public Rational divide(Rational other) { public Rational divide(Rational other) {
// YOUR CODE HERE int newNumerator = this.numerator * other.denominator;
// HINT: Division is multiplication by reciprocal int newDenominator = this.denominator * other.numerator;
// Don't forget to check if other.numerator is zero! if (newDenominator == 0) {
return null; // Remove this line when implemented System.out.println("Can't divide: new denominator is zero");
return null;
}
return new Rational(newNumerator, newDenominator);
} }
// TODO: Static method - r1 / r2 // TODO: Static method - r1 / r2
public static Rational divide(Rational r1, Rational r2) { public static Rational divide(Rational r1, Rational r2) {
// YOUR CODE HERE return r1.divide(r2);
return null; // Remove this line when implemented
} }
// PROVIDED - String representation // PROVIDED - String representation