144 lines
4.3 KiB
Java
144 lines
4.3 KiB
Java
package Rational;
|
|
|
|
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");
|
|
}
|
|
this.numerator = numerator;
|
|
this.denominator = denominator;
|
|
simplify();
|
|
}
|
|
|
|
// GCD helper for simplification
|
|
private int gcd(int a, int b) {
|
|
a = Math.abs(a);
|
|
b = Math.abs(b);
|
|
while (b != 0) {
|
|
int temp = b;
|
|
b = a % b;
|
|
a = temp;
|
|
}
|
|
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)
|
|
public Rational subtract(Rational other) {
|
|
// YOUR CODE HERE
|
|
int NewNumerator = ((this.numerator)*other.denominator)-((this.denominator)*other.numerator) ;
|
|
int NewDenominator = (this.denominator)*other.denominator;
|
|
|
|
return new Rational(NewNumerator ,NewDenominator);
|
|
|
|
|
|
// Remove this line when implemented
|
|
}
|
|
|
|
// TODO: Static method - r1 - r2
|
|
public static Rational subtract(Rational r1, Rational r2) {
|
|
// YOUR CODE HERE
|
|
int NewNumerator = (r1.numerator* r2.denominator)-(r1.denominator* r2.numerator) ;
|
|
int NewDenominator = r1.denominator* r2.denominator ;
|
|
|
|
return new Rational(NewNumerator ,NewDenominator);
|
|
}
|
|
|
|
// TODO: Instance method - this * other
|
|
// Formula: (a/b) * (c/d) = (a*c) / (b*d)
|
|
public Rational multiply(Rational other) {
|
|
// YOUR CODE HERE
|
|
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) {
|
|
int NewNumerator = r1.numerator* r2.numerator ;
|
|
int NewDenominator = r1.denominator* r2.denominator ;
|
|
|
|
return new Rational(NewNumerator ,NewDenominator);
|
|
// YOUR CODE HERE
|
|
//return null; // Remove this line when implemented
|
|
}
|
|
|
|
// TODO: Instance method - this / other
|
|
// Formula: (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 ArithmeticException("cannot divide ") ;
|
|
}
|
|
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
|
|
if (r2.numerator == 0 )
|
|
{
|
|
throw new ArithmeticException("cannot divide ") ;
|
|
}
|
|
int NewNumerator = r1.numerator * r2.denominator ;
|
|
int NewDenominator = r1.denominator * r2.numerator ;
|
|
return new Rational(NewNumerator ,NewDenominator);
|
|
|
|
|
|
//return null; // Remove this line when implemented
|
|
}
|
|
|
|
// PROVIDED - String representation
|
|
@Override
|
|
public String toString() {
|
|
if (denominator == 1) {
|
|
return numerator + "";
|
|
}
|
|
return numerator + "/" + denominator;
|
|
}
|
|
|
|
// PROVIDED - Decimal value
|
|
public double toDouble() {
|
|
return (double) numerator / denominator;
|
|
}
|
|
}
|