Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

With the help of the class below, create a new class called BigRational. Base it on the FastRational class used below, but use java.math.BigInteger for

With the help of the class below, create a new class called BigRational. Base it on the FastRational class used below, but use java.math.BigInteger for both the numerator and denominator. This will require that you modify most or all of the methods from FastRational. For instance, when the operators +, -, *, or / where performed on the numerator or denominator, you will now need to use .add, .substract, .multiply, or .divide, respectively. Note that the BigInteger class comes with a gcd method, so use that for computing gcds. BigRational must extend Number. In a file called TestBigRationalSeries.java write a test program that computes and displays the series 1/2 + 2/3 + 3/4 + ... + 98/99 + 99/100. The results should be:

Series sum = 264414864639329557497913717698145082779489/2788815009188499086581352357412492142272

Series sum = 94.8126224823604

public class FastRational extends Number implements Comparable { private static final long serialVersionUID = 747594659068733876L; // Data fields for numerator and denominator private long numerator = 0; private long denominator = 1; /** Construct a rational with default properties */ public FastRational() { this(0, 1); } /** Construct a rational with specified numerator and denominator */ public FastRational(long numerator, long denominator) { if (denominator == 0) throw new ArithmeticException("Rational number with denominator zero"); long gcd = gcd(numerator, denominator); this.numerator = ((denominator > 0) ? 1 : -1) * numerator / gcd; this.denominator = Math.abs(denominator) / gcd; } /** Find GCD of two numbers */ private static long gcd(long n, long d) { return fastGcd(n, d); } /** Use Euclid's algorithm to compute a GCD. */ private static long fastGcd(long m, long n) // Assumes m and n are non-negative { if (m == 0) return n; if (n == 0) return m; m = Math.abs(m); n = Math.abs(n); while (true) { long r = m % n; if (r == 0) return n; m = n; n = r; } } /** Return numerator */ public long getNumerator() { return numerator; } /** Return denominator */ public long getDenominator() { return denominator; } /** Add a rational number to this rational */ public FastRational add(FastRational secondRational) { long n = numerator * secondRational.getDenominator() + denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new FastRational(n, d); } /** Subtract a rational number from this rational */ public FastRational subtract(FastRational secondRational) { long n = numerator * secondRational.getDenominator() - denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new FastRational(n, d); } /** Multiply a rational number to this rational */ public FastRational multiply(FastRational secondRational) { long n = numerator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new FastRational(n, d); } /** Divide a rational number from this rational */ public FastRational divide(FastRational secondRational) { long n = numerator * secondRational.getDenominator(); long d = denominator * secondRational.numerator; return new FastRational(n, d); } @Override public String toString() { if (denominator == 1) return numerator + ""; else return numerator + "/" + denominator; } @Override // Override the equals method in the Object class public boolean equals(Object other) { if ((this.subtract((FastRational)(other))).getNumerator() == 0) return true; else return false; } @Override // Implement the abstract intValue method in Number public int intValue() { return (int)doubleValue(); } @Override // Implement the abstract floatValue method in Number public float floatValue() { return (float)doubleValue(); } @Override // Implement the doubleValue method in Number public double doubleValue() { return numerator * 1.0 / denominator; } @Override // Implement the abstract longValue method in Number public long longValue() { return (long)doubleValue(); } @Override // Implement the compareTo method in Comparable public int compareTo(FastRational o) { if (this.subtract(o).getNumerator() > 0) return 1; else if (this.subtract(o).getNumerator() < 0) return -1; else return 0; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions