Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In a file called TestFastRationalSeries.java write a program that uses the FastRational class below to compute the following summation: 1/2 + 2/3 + 3/4 +

In a file called TestFastRationalSeries.java write a program that uses the FastRational class below to compute the following summation:

1/2 + 2/3 + 3/4 + ... + 98/99 + 99/100

First display the result using its toString method, and then using its doubleValue method. You will discover that the output is incorrect:

Series sum = 1349247664274259951/1822963237492290880

Series sum = 0.7401398100217947

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

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

6th International Edition

061921323X, 978-0619213237

More Books

Students also viewed these Databases questions