Question Presented:
Write a program that computes the following summation series using the rational class:
1/2+2/3+3/4+...+98/99+99/100
You will discover that the output is incorrect because of integer overflow. Evaluate the sum using doubles, the Rational class, and the BigINteger Rational class.

So far this is what I have come up with:

By BigInteger Rational Class:

``````import java.math.*;

public class BigIntRational implements Comparable

//data field for numerator
BigInteger numerator;
//data field for denominator
BigInteger denominator;

//construct a BigInteger rational with default BigInteger properties
public BigIntRational()
{
this(new BigInteger("1"), new BigInteger("0"));
}

//construct a BigInteger rational with specified BigInteger numerator and BigInteger denominator
public BigIntRational(BigInteger numerator, BigInteger denominator)
{BigInteger gcd = gcd(numerator, denominator);
BigInteger zero = BigInteger.ZERO;
this.numerator = (denominator.compareTo(zero) == 1? new BigInteger("1"):new BigInteger("1")).multiply(numerator.divide(gcd));
this.denominator = denominator.divide(gcd);
}

//find the GCD of two BigInteger numbers
private static BigInteger gcd(BigInteger n, BigInteger d)
{
BigInteger n1 = n.abs();
BigInteger n2 = d.abs();
BigInteger gcd = new BigInteger("1");
while(n1.compareTo(n2) != 0 )
{
if(n1.compareTo(n2)>0)
n1=n1.subtract(n2);
else
n2=n2.subtract(n1);
}
return n1;
}

//return BigInteger numerator
public BigInteger getNumerator()
{
return numerator;
}

//return BigInteger denominator
public BigInteger getDenominator()
{
return denominator;
}

//add a BigInteger rational number to this BigInteger rational
{
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new BigIntRational(n,d);
}

//subtract a BigInteger rational number from this BigInteger rational
public BigIntRational subtract(BigIntRational secondRational)
{
BigInteger n = numerator.multiply(secondRational.getDenominator()).subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new BigIntRational(n,d);
}

//multiply a BigInteger rational number by this BigInteger rational
public BigIntRational multiply(BigIntRational secondRational)
{
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new BigIntRational(n,d);
}

//divide a BigInteger rational number by this BigInteger rational
public BigIntRational divide(BigIntRational secondRational)
{
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.getNumerator());
return new BigIntRational(n,d);
}

//override the toString method
public String toString()
{
return numerator + "/" + denominator;
}

//override the equals method
public boolean equals(Object o)
{
if(this.subtract((BigIntRational)o).getNumerator().equals("0"))
return true;
else
return false;
}

//override and implement the abstract intValue method
public int intValue()
{
return (int)doubleValue();
}

//override and implement the abstract floatValue method
public float floatValue()
{
return (float)doubleValue();
}
//override and implement the abstract doubleValue method
public double doubleValue()
{
return numerator.divide(denominator).doubleValue();
}

//override and implement the abstract longValue method
public long longValue()
{
return (long)doubleValue();
}

//override and implement the abstract compareTo method
public int compareTo(Object o)
{
if(this.subtract((BigIntRational)o).getNumerator().compareTo(new BigInteger("0")) > 0)
return 1;
else if(this.subtract((BigIntRational)o).getNumerator().compareTo(new BigInteger("0")) < 0)
return -1;
else
return 0;
}
}
``````

My Rational Class:

`````` public class Rational extends Number implements Comparable<Rational> {
// Data fields for numerator and denominator
private long numerator = 0;
private long denominator = 1;

/** Construct a rational with default properties */
public Rational() {
this(0, 1);
}

/** Construct a rational with specified numerator and denominator */
public Rational(long numerator, long denominator) {
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) {
long n1 = Math.abs(n);
long n2 = Math.abs(d);
/*    int gcd = 1;

for (int k = 1; k <= n1 && k <= n2; k++) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k;
}

return gcd;
*/
if(n2 == 0)
return n1;
else
return gcd(n2, n1 % n2);
}

/** Return numerator */
public long getNumerator() {
return numerator;
}

/** Return denominator */
public long getDenominator() {
return denominator;
}

/** Add a rational number to this rational */
long n = numerator * secondRational.getDenominator() +
denominator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

/** Subtract a rational number from this rational */
public Rational subtract(Rational secondRational) {
long n = numerator * secondRational.getDenominator()
- denominator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

/** Multiply a rational number to this rational */
public Rational multiply(Rational secondRational) {
long n = numerator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

/** Divide a rational number from this rational */
public Rational divide(Rational secondRational) {
long n = numerator * secondRational.getDenominator();
long d = denominator * secondRational.numerator;
return new Rational(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((Rational)(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(Rational o) {
if (this.subtract(o).getNumerator() > 0)
``````

My BigInteger Rational Test:

``````import java.math.*;

public class TestBigIntRational
{
public static void main(String[] args)
{
//create BigInteger numerator1
BigInteger numerator1 = new BigInteger("1");
//create BigInteger denominator1
BigInteger denominator1 = new BigInteger("2");

//create BigInteger numerator2
BigInteger numerator2 = new BigInteger("99");
//create BigInteger denominator2
BigInteger denominator2 = new BigInteger("100");

//create BigIntRational r1
BigIntRational r1 = new BigIntRational(numerator1, denominator1);
//create BigIntRational r2
BigIntRational r2 = new BigIntRational(numerator2, denominator2);

//print out the BigIntRational numbers r1 and r1
System.out.println("The two BigInteger rational numbers are: r1 = " + r1 + " and r2 = " + r2 );
//print out the addition of r1 and r2
//print out the subtraction of r1 and r2
System.out.println("Subtraction of two BigInteger rational numbers is " + r1.subtract(r2));
//print out the multiplication of r1 and r2
System.out.println("Multiplication of two BigInteger rational numbers is " + r1.multiply(r2));
//print out the division of r1 and r2
System.out.println("Division of two BigInteger rational numbers is " + r1.divide(r2));
//print out true if r1 equals r2 and false if r1 doesn't equal r2
System.out.println("Are the two BigInteger rational numbers equal? " + r1.equals(r2));
//print out the comparision of r1 and r2
System.out.println("Comparision of two BigInteger rational numbers: " + r1.compareTo(r2));
}
}

My Summation:

import java.math.*;

public class Summation
{
public double RationalSum (Rational r1, Rational r2)
{
double total = 0.0;
int n;
int d;
for(n = r1.numerator, d = r1.denominator; n < r2.numerator; n++, d++)
total += (double)n/d;
}
public BigInteger BigIntSum (BigInteger bir1, BigInteger bir2)
{
BigInteger Bigtotal = BigInteger.ZERO;
int bin;
int bid;
for(bin = bir1.numerator, bid = bir1.denominator; bin < bir2.numerator; bin++, bid++)
Bigtotal += (double)bin/bid;
return Bigtotal;
}

public static void main(String[] args)
{
//create Rational r1
Rational r1 = new Rational (1, 2);
//create Rational r2
Rational r2 = new Rational (99,100);

//create BigInteger numerator1
BigInteger numerator1 = new BigInteger("1");
//create BigInteger denominator1
BigInteger denominator1 = new BigInteger("2");

//create BigInteger numerator2
BigInteger numerator2 = new BigInteger("99");
//create BigInteger denominator2
BigInteger denominator2 = new BigInteger("100");

//create BigIntRational bir1
BigIntRational bir1 = new BigIntRational(numerator1, denominator1);
//create BigIntRational bir2
BigIntRational bir2 = new BigIntRational(numerator2, denominator2);

}}
``````

What my Professor gave as an example:

``````   import java.math.*;

public class Ex15_22
{
public static void main(String[] args)
{
double sum = 0;
for(int i = 1; i < 41; i++)
sum += i/(i + 1.0);

System.out.printf("Double value = %.15f\n", sum);

Rational total = new Rational();
for(int i = 1; i < 41; i++)
{
Rational term = new Rational(i, i+1);
}

System.out.printf("Rational value = %s\n", total.toString());
System.out.printf("Value (double) = %.15f\n", total.doubleValue());

}
}
``````

Yes this is an assignment, I have put in the work but I have hit a brick wall with how to solve this. For the first part of my assignment I wrote the BigInteger Rational class and wrote the test, now I need help with the second part.

Thank YOU

I'm not entirely clear what your question is here.

Yes, what exactly is the question? Which part of the exercise can't you do?
Is it just to extend the prof's code to perform the same calculation using your new class?