develope #1
@@ -41,9 +41,9 @@ public class Rational {
|
|||||||
|
|
||||||
// COMPLETED - Instance method: this + other
|
// COMPLETED - Instance method: this + other
|
||||||
public Rational add(Rational other) {
|
public Rational add(Rational other) {
|
||||||
int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator;
|
int newNum = this.numerator * other.denominator + other.numerator * this.denominator;
|
||||||
int newDenominator = this.denominator * other.denominator;
|
int newDenom = this.denominator * other.denominator;
|
||||||
return new Rational(newNumerator, newDenominator);
|
return new Rational(newNum, newDenom);
|
||||||
}
|
}
|
||||||
|
|
||||||
// COMPLETED - Static method: r1 + r2
|
// COMPLETED - Static method: r1 + r2
|
||||||
@@ -54,42 +54,53 @@ 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 newNum = this.numerator * other.numerator - other.numerator * this.denominator;
|
||||||
return null; // Remove this line when implemented
|
int newDenom = other.numerator * this.denominator;
|
||||||
|
return new Rational(newNum, newDenom);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
int newNum = r1.numerator * r2.denominator - r2.numerator * r1.denominator;
|
||||||
return null; // Remove this line when implemented
|
int newDenom = r1.denominator * r2.denominator;
|
||||||
|
return new Rational(newNum, newDenom);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 newNum = this.numerator * other.numerator;
|
||||||
return null; // Remove this line when implemented
|
int newDenom = this.denominator * other.denominator;
|
||||||
|
return new Rational(newNum, newDenom);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
int newNum = r1.numerator * r2.numerator;
|
||||||
return null; // Remove this line when implemented
|
int newDenom = r1.denominator * r2.denominator;
|
||||||
|
return new Rational(newNum, newDenom);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
if (other.numerator == 0){
|
||||||
// HINT: Division is multiplication by reciprocal
|
throw new ArithmeticException("Cannot divide by zero");
|
||||||
// Don't forget to check if other.numerator is zero!
|
}
|
||||||
return null; // Remove this line when implemented
|
int newNum = this.numerator * other.denominator;
|
||||||
|
int newDenom = this.denominator * other.numerator;
|
||||||
|
return new Rational(newNum, newDenom);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
if (r2.numerator == 0){
|
||||||
return null; // Remove this line when implemented
|
throw new ArithmeticException("Cannot divide by zero");
|
||||||
|
}
|
||||||
|
int newNum = r1.numerator * r2.denominator;
|
||||||
|
int newDenom = r1.denominator * r2.numerator;
|
||||||
|
|
||||||
|
return new Rational(newNum, newDenom);
|
||||||
}
|
}
|
||||||
|
|
||||||
// PROVIDED - String representation
|
// PROVIDED - String representation
|
||||||
|
|||||||
Reference in New Issue
Block a user