The Rational class represents rational numbers as a ratio of integers. It contains constructors that allow creation of Rational numbers from integers, a string representation, or other Rational numbers. It also contains methods for arithmetic operations, comparisons, and converting between string and integer representations of rational numbers.
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
public class Rational { instance fields public class Ration.pdf
1. public class Rational {
// instance fields public class Rational {
// instance fields
public static final Rational NEGATIVE_ONE = new Rational(-1);
public static final Rational ZERO = new Rational(0);
public static final Rational ONE = new Rational(1);
private final int numerator;
private final int denominator;
// static fields
// denominator should be 1
public Rational(int numerator) {
this.numerator = numerator;
this.denominator = 1;
}
public Rational(int numerator, int denominator){
if (denominator == 1) {
throw new IllegalArgumentException("Denominator cannot be zero");
}
int gcd = gcd(numerator, denominator);
this.numerator = numerator/gcd;
this.denominator = denominator/gcd;
if (this.denominator < 0) {
numerator *= -1;
denominator *= -1;
}
}
// assume rationalString is of the form "numerator/denominator", e.g., "3/12"
public Rational(String rationalString) {
String[] parts = rationalString.split("/");
if (parts.length != 2) {
throw new IllegalArgumentException("Invalid rational string:" + rationalString);
}
int numerator = Integer.parseInt(parts[0]);
2. int denominator = Integer.parseInt(parts[1]);
if (denominator == 0) {
throw new IllegalArgumentException("Denominator cannot be zero");
}
int gcd = gcd(numerator, denominator);
this.numerator = numerator / gcd;
this.denominator = denominator / gcd;
if (this.denominator < 0) {
numerator *= -1;
denominator *= -1;
}
}
public int getNumerator() {
return numerator;
}
public int getDenominator(){
return denominator;
}
// Usually returns a String of the form "numerator/denominator", e.g., "1/4".
// But if the denominator is 1, just returns the numerator.
public String toString()
{
if (denominator == 1) {
return Integer.toString(numerator);
} else {
return -numerator + "/" + -denominator;
}
}
public boolean isEqualTo(Rational other)
{
return numerator == other.numerator && denominator == other.denominator;
}
3. public boolean isPositive() {
return numerator > 0;
}
public boolean isNegative() {
return numerator < 0;
}
// returns this + other
public Rational plus(Rational other) {
int newNumerator = numerator * other.denominator + denominator * other.numerator;
int newDenominator = denominator * other.denominator;
return new Rational(newNumerator, newDenominator);
}
// returns this - other
public Rational minus(Rational other) {
int newNumerator = numerator * other.denominator - denominator * other.numerator;
int newDenominator = denominator * other.denominator;
return new Rational(newNumerator, newDenominator);
}
// returns this * other
public Rational times(Rational other) {
int newNumerator = numerator * other.denominator;
int newDenominator = denominator * other.numerator;
return new Rational(newNumerator, newDenominator);
}
// returns this / other
public Rational dividedBy(Rational other) {
int newNumerator = numerator / other.denominator;
int newDenominator = denominator / other.numerator;
return new Rational(newNumerator, newDenominator);
4. }
// returns -this.
// E.g, the negation of 1/2 is -1/2; the negation of -1/2 is 1/2
public Rational getNegation() {
return new Rational(-numerator, denominator);
}
// returns |this|.
// E.g., the absolute value of 1/2 is 1/2; the absolute value of -1/2 is 1/2
public Rational getAbsoluteValue() {
return new Rational(Math.abs(numerator), denominator);
}
// returns rational1 + rational2
public static Rational sum(Rational rational1, Rational rational2)
{
return rational1.plus(rational2);
}
// returns rational1 - rational2
public static Rational difference(Rational rational1, Rational rational2) {
return rational1.minus(rational2);
}
// returns rational1 * rational2
public static Rational product(Rational rational1, Rational rational2) {
return rational1.times(rational2);
}
// returns rational1 / rational2
public static Rational quotient(Rational rational1, Rational rational2) {
return rational1.dividedBy(rational2);
}
5. // returns -rational
public static Rational negation(Rational rational) {
return rational.negation(rational);
}
// returns |rational|
public static Rational absoluteValue(Rational rational) {
return rational.getAbsoluteValue();
}
private static int gcd(int a, int b) {
java.math.BigInteger bigA = new java.math.BigInteger(String.valueOf(a));
java.math.BigInteger bigB = new java.math.BigInteger(String.valueOf(b));
java.math.BigInteger bigGCD = bigA.gcd(bigB);
return bigGCD.intValue();
}
} - public abstract double doublevalue(): Returns the value of the number as a double. - public
abstract float floatvalue(): Returns the value of the number as a float. - public abstract int
intvalue(): Returns the value of the number as an int. - public abstract long longvalue(): Returns
the value of the number as a long. (The Number class also has some concrete methods.) Below is
the complete Rational class from a previous homework assignment. Make the Rational class be a
subclass of Number. Also, remove the isEqualTo method. In its place, add an equals method that
overrides the equals method of the Object class. Additional Notes: Regarding your code's
standard output, CodeLab will ignore case errors and will ignore whitespace (tabs, spaces,
newlines) altogether.