Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

****JAVA******* As Rats are added and multiplied, their numerators and denominators can become too big to represent using long integers, which were used in the

****JAVA*******

As Rats are added and multiplied, their numerators and denominators can become too big to represent using long integers, which were used in the original Rat class. You are to modify the Rat class so that it solves this problem and provides rational number objects with arbitrarily large numerators and denominators.

Here are the changes I have already made to the Rat class from lecture:

I have completed all of the implementations

I have renamed the class to BigRat

I have added one additional unimplemented constructor as detailed below

I have removed the toDouble method

You will need to change the representation, reimplement the existing methods and constructors, and implement the new constructor. (You will no longer need the gcd method and can get rid of it if you like.) Java has a class called BigInteger (in the java.math package) that provides objects that represent arbitrarily long integers. You are to use BigInteger objects, instead of longs, to represent the numerator and denominator in your BigRat class.

Do not change the headers of any of the constructors or methods. In particular, the two constructors that take longs as parameters should continue to take longs as parameters.

I strongly suggest that you begin by studying the specification of the BigInteger class, which you can find here (Links to an external site.)Links to an external site.. You will not need to use (or understand) most of the constructors or methods, but some will be very useful. There are, for example, methods for performing the four basic arithmetic operations as well as gcd. Do not call over a TA and ask him or her to explain how BigIntegers work. Try figuring it out for yourself by reading and experimenting before asking for help. Being able to teach yourself about language and library features is a crucial skill that you should develop.

-----from lecture

package rational;

/**

* Provides rational number (fraction) objects. The rational arithmetic provided by Rational objects is subject to

* integer overflow if the numerator and/or denominator becomes too large.

*/

public class Rat

{

/**

* Represents the fraction num/den. den > 0 gcd(den, |num|) = 1

*/

private long num;

private long den;

/**

* Creates the rational number 0

*/

public Rat ()

{

this(0, 1);

}

/**

* Creates the rational number n

*/

public Rat (long n)

{

this(n, 1);

}

/**

* If d is zero, throws an IllegalArgumentException. Otherwise creates the rational number n/d

*/

public Rat (long n, long d)

{

if (d == 0)

{

throw new IllegalArgumentException();

}

if (d < 0)

{

d = -d;

n = -n;

}

long g = gcd(d, Math.abs(n));

num = n / g;

den = d / g;

}

/**

* Returns the sum of this and r Rat x = new Rat(5, 3); Rat y = new Rat(1, 5); Rat z = x.add(y); a/b + c/d = (ad +

* bc) / bd

*/

public Rat add (Rat r)

{

return null;

}

/**

* Returns the difference of this and r a/b - c/d = (ad - bc) / bd

*/

public Rat sub (Rat r)

{

return null;

}

/**

* Returns the product of this and r Rat x = new Rat(5, 3); Rat y = new Rat(1, 5); Rat z = x.mul(y); a/b * c/d =

* ac/bd

*/

public Rat mul (Rat r)

{

return null;

}

/**

* If r is zero, throws an IllegalArgumentException. Otherwise, returns the quotient of this and r. a/b / c/d = ad /

* bc

*/

public Rat div (Rat r)

{

return null;

}

/**

* Returns a negative number if this < r, zero if this = r, a positive number if this > r To compare a/b and c/d,

* compare ad and bc

*/

public int compareTo (Rat r)

{

return 0;

}

/**

* Returns a string version of this in simplest and lowest terms. Examples: 3/4 => "3/4" 6/8 => "3/4" 2/1 => "2" 0/8

* => "0" 3/-4 => "-3/4"

*/

public String toString ()

{

if (num == 0)

{

return "0";

}

else if (den == 1)

{

return num + "";

}

else

{

return num + "/" + den;

}

}

/**

* Converts this to the closest double

*/

public double toDouble ()

{

return 0.0;

}

/**

* Returns the greatest common divisor of a and b, where a >= 0 and b > 0.

*/

public static long gcd (long a, long b)

{

while (b > 0)

{

long remainder = a % b;

a = b;

b = remainder;

}

return a;

}

}

-----assignment

package rational;

import java.math.BigInteger;

/**

* Provides rational number (fraction) objects. The rational arithmetic provided by Rational objects is subject to

* integer overflow if the numerator and/or denominator becomes too large.

*/

public class BigRat

{

/**

* The numerator of this Rat. The gcd of |num| and den is always 1.

*/

private long num;

/**

* The denominator of this Rat. den must be positive.

*/

private long den;

/**

* Creates the rational number 0

*/

public BigRat ()

{

this(0);

}

/**

* Creates the rational number n.

*

* DO NOT MODIFY THE HEADER OF THIS CONSTRUCTOR. IT SHOULD TAKE A SINGLE LONG AS ITS PARAMETER

*/

public BigRat (long n)

{

this(n, 1);

}

/**

* If d is zero, throws an IllegalArgumentException. Otherwise creates the rational number n/d

*

* DO NOT MODIFY THE HEADER OF THIS CONSTRUCTOR. IT SHOULD TAKE TWO LONGS AS ITS PARAMETERS

*/

public BigRat (long n, long d)

{

// Denominator must not be zero

if (d == 0)

{

throw new IllegalArgumentException();

}

// Deals with signs

if (d < 0)

{

d = -d;

n = -n;

}

// Deal with lowest terms

long g = gcd(Math.abs(n), d);

num = n / g;

den = d / g;

}

/**

* If d is zero, throws an IllegalArgumentException. Otherwise creates the rational number n/d

*/

public BigRat (BigInteger r, BigInteger d)

{

}

/**

* Returns the sum of this and r Rat x = new Rat(5, 3); Rat y = new Rat(1, 5); Rat z = x.add(y); a/b + c/d = (ad +

* bc) / bd

*/

public BigRat add (BigRat r)

{

long n = this.num * r.den + this.den * r.num;

long d = this.den * r.den;

return new BigRat(n, d);

}

/**

* Returns the difference of this and r a/b - c/d = (ad - bc) / bd

*/

public BigRat sub (BigRat r)

{

long n = this.num * r.den - this.den * r.num;

long d = this.den * r.den;

return new BigRat(n, d);

}

/**

* Returns the product of this and r Rat x = new Rat(5, 3); Rat y = new Rat(1, 5); Rat z = x.mul(y); a/b * c/d =

* ac/bd

*/

public BigRat mul (BigRat r)

{

return new BigRat(this.num * r.num, this.den * r.den);

}

/**

* If r is zero, throws an IllegalArgumentException. Otherwise, returns the quotient of this and r. a/b / c/d = ad /

* bc

*/

public BigRat div (BigRat r)

{

if (r.num == 0)

{

throw new IllegalArgumentException();

}

else

{

return new BigRat(this.num * r.den, this.den * r.num);

}

}

/**

* Returns a negative number if this < r, zero if this = r, a positive number if this > r To compare a/b and c/d,

* compare ad and bc

*/

public int compareTo (BigRat r)

{

long diff = this.num * r.den - this.den * r.num;

if (diff < 0)

{

return -1;

}

else if (diff > 0)

{

return 1;

}

else

{

return 0;

}

}

/**

* Returns a string version of this in simplest and lowest terms. Examples: 3/4 => "3/4" 6/8 => "3/4" 2/1 => "2" 0/8

* => "0" 3/-4 => "-3/4"

*/

public String toString ()

{

if (den == 1)

{

return num + "";

}

else

{

return num + "/" + den;

}

}

/**

* Returns the greatest common divisor of a and b, where a >= 0 and b > 0.

*/

public static long gcd (long a, long b)

{

while (b > 0)

{

long remainder = a % b;

a = b;

b = remainder;

}

return a;

}

}

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

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions

Question

please dont use chat gpt AI 2 0 .

Answered: 1 week ago