Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ASAP please!! I am expecting something different answers from CHATGPT. Problem 2 (Adapted from Programming Exercises 13.18 - Use the FastRational class, 15 points): Download

ASAP please!! I am expecting something different answers from CHATGPT.

Problem 2 (Adapted from Programming Exercises 13.18 - Use the FastRational class, 15 points): Download FastRational.java from Canvas. It is the same as the Rational.java code from Chapter 13 except that it uses a faster way from Chapter 22 to compute GCDs. In a file called TestFastRationalSeries.java write a program that uses the FastRational class 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 The actual sum should quite a bit larger. The culprit is integer overflow

FastRational.java

image text in transcribed
Page of 3 - ZOOM + // A rational number class that uses Euclid's Algorithm to quickly compute // greatest common divisors. 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 god = gcd(numerator, denominator) ; this . numerator = ((denominator > 0) ? 1 : -1) * numerator / god; this . denominator = Math . abs (denominator ) / gcd; /** Find GCD of two numbers */ private static long god( long n, long d) { return fastGod (n, d) ; /** Use Euclid's algorithm to compute a GCD. private static long fastGod( 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 ( ) { urn numerator; ** Return denominator */ public long getDenominator ( ) { return denominator; } ** Add a rational number to this rational */

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions