complete
This commit is contained in:
@@ -4,7 +4,7 @@ public class Rational {
|
|||||||
private int numerator;
|
private int numerator;
|
||||||
private int denominator;
|
private int denominator;
|
||||||
|
|
||||||
// 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");
|
||||||
@@ -14,7 +14,7 @@ public class Rational {
|
|||||||
simplify();
|
simplify();
|
||||||
}
|
}
|
||||||
|
|
||||||
// GCD helper for simplification
|
|
||||||
private int gcd(int a, int b) {
|
private int gcd(int a, int b) {
|
||||||
a = Math.abs(a);
|
a = Math.abs(a);
|
||||||
b = Math.abs(b);
|
b = Math.abs(b);
|
||||||
@@ -26,59 +26,55 @@ public class Rational {
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simplify the fraction
|
|
||||||
private void simplify() {
|
private void simplify() {
|
||||||
int gcd = gcd(numerator, denominator);
|
int gcd = gcd(numerator, denominator);
|
||||||
numerator /= gcd;
|
numerator /= gcd;
|
||||||
denominator /= gcd;
|
denominator /= gcd;
|
||||||
|
|
||||||
// Keep denominator positive
|
|
||||||
if (denominator < 0) {
|
if (denominator < 0) {
|
||||||
numerator = -numerator;
|
numerator = -numerator;
|
||||||
denominator = -denominator;
|
denominator = -denominator;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 newNumerator = this.numerator * other.denominator + other.numerator * this.denominator;
|
||||||
int newDenominator = this.denominator * other.denominator;
|
int newDenominator = this.denominator * other.denominator;
|
||||||
return new Rational(newNumerator, newDenominator);
|
return new Rational(newNumerator, newDenominator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// COMPLETED - Static method: r1 + r2
|
|
||||||
public static Rational add(Rational r1, Rational r2) {
|
public static Rational add(Rational r1, Rational r2) {
|
||||||
return r1.add(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) {
|
public Rational subtract(Rational other) {
|
||||||
int newNumerator = this.numerator * other.denominator - other.numerator * this.denominator;
|
int newNumerator = this.numerator * other.denominator - other.numerator * this.denominator;
|
||||||
int newDenominator = this.denominator * other.denominator;
|
int newDenominator = this.denominator * other.denominator;
|
||||||
return new Rational(newNumerator, newDenominator);
|
return new Rational(newNumerator, newDenominator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Static method - r1 - r2
|
|
||||||
public static Rational subtract(Rational r1, Rational r2) {
|
public static Rational subtract(Rational r1, Rational r2) {
|
||||||
return r1.subtract(r2);
|
return r1.subtract(r2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Instance method - this * other
|
|
||||||
// Formula: (a/b) * (c/d) = (a*c) / (b*d)
|
|
||||||
public Rational multiply(Rational other) {
|
public Rational multiply(Rational other) {
|
||||||
int newNumerator = this.numerator * other.numerator;
|
int newNumerator = this.numerator * other.numerator;
|
||||||
int newDenominator = this.denominator * other.denominator;
|
int newDenominator = this.denominator * other.denominator;
|
||||||
return new Rational(newNumerator, newDenominator);
|
return new Rational(newNumerator, newDenominator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Static method - r1 * r2
|
|
||||||
public static Rational multiply(Rational r1, Rational r2) {
|
public static Rational multiply(Rational r1, Rational r2) {
|
||||||
return r1.multiply(r2);
|
return r1.multiply(r2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Instance method - this / other
|
|
||||||
// Formula: (a/b) ÷ (c/d) = (a*d) / (b*c)
|
|
||||||
public Rational divide(Rational other) {
|
public Rational divide(Rational other) {
|
||||||
if (other.numerator == 0 ) {
|
if (other.numerator == 0 ) {
|
||||||
throw new ArithmeticException("Cannot divide by zero");
|
throw new ArithmeticException("Cannot divide by zero");
|
||||||
@@ -88,12 +84,11 @@ public class Rational {
|
|||||||
return new Rational(newNumerator, newDenominator);
|
return new Rational(newNumerator, newDenominator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Static method - r1 / r2
|
|
||||||
public static Rational divide(Rational r1, Rational r2) {
|
public static Rational divide(Rational r1, Rational r2) {
|
||||||
return r1.divide(r2);
|
return r1.divide(r2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// PROVIDED - String representation
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (denominator == 1) {
|
if (denominator == 1) {
|
||||||
@@ -102,7 +97,6 @@ public class Rational {
|
|||||||
return numerator + "/" + denominator;
|
return numerator + "/" + denominator;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PROVIDED - Decimal value
|
|
||||||
public double toDouble() {
|
public double toDouble() {
|
||||||
return (double) numerator / denominator;
|
return (double) numerator / denominator;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ public class Movie {
|
|||||||
private String country;
|
private String country;
|
||||||
private String director;
|
private String director;
|
||||||
private List<String> actors;
|
private List<String> actors;
|
||||||
private int runtime; // minutes
|
private int runtime;
|
||||||
private String rated;
|
private String rated;
|
||||||
private String released; // date string
|
private String released;
|
||||||
private String plot;
|
private String plot;
|
||||||
private int imdb_votes;
|
private int imdb_votes;
|
||||||
private int metascore;
|
private int metascore;
|
||||||
|
|||||||
Reference in New Issue
Block a user