Question

I'm having trouble working with BigIntegers. I'm having trouble with the add method in the Rational class. In the Rational(int x, int y) constructor I'm trying to convert the parameters datatype int into the instance variable datatype of BigInteger though the use of thetoString(int n) method.

  1. Am I doing the conversion correctly inside the Rational(int x, int y) constructor?
  2. They way the add method is written I'm getting an error under all of n.num and n.den. I don't understand why I'm getting that error. Am I not correctly using the add method from the BigInteger class? http://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html

Suppose one class has the following

Rational a = new Rational(1,2);
Rational b = new Rational(1,3);
Rational c = new Rational(1,6);
Rational sum = a.add(b).add(c);
println(sum);

and the Rational class includes

import acm.program.*;
import java.math.*;

public class Rational{

    public Rational(int x, int y) {
        num = new BigInteger(toString(x));
        den = new BigInteger(toString(y));
    }

    public toString(int n) {
        return toString(n); 
    }

    public BigInteger add(BigInteger n) {
        return new BigInteger(this.num * n.den + n.num * this.den, this.den *  n.den)
    }

    /*  private instance variables  */
    private BigInteger num; 
    private BigInteger den;
}
Was it helpful?

Solution

To convert an int to BigInteger I would use BigInteger.valueOf(int).

Also, you cannot use operators with BigIntegers, you must use its own methods. Your methos should be like this:

public Rational add(Rational n) {
    return new Rational(
             this.num.multiply(n.den).add(n.num.multiply(this.den)).intValue(),
             this.den.multiply(n.den).intValue());
}

OTHER TIPS

1) Am I doing the conversion correctly inside the Rational(int x, int y) constructor?

You can use

BigInteger num = BigInteger.valueOf(x);

Making a String first is is not required.

2. They way the add method is written I'm getting an error .....

Your add method is wrong and its not clear what your are trying to acheive in your add method. But if your want to do addition in BigInteger you should use BigInteger#add method and for multiplication between BigInteger you should use BigInteger#multiply method.

A simple error:

public Rational add(Rational n) {
    return new Rational(
        this.num.multiply(n.den).add(n.num.multiply(this.den)),
        this.den.multiply(n.den));
}

Also, when creating a new BigInteger you should use the valueOf(int) method instead of converting to String

To stop the denominators blowing up exponentially, I would use the lowest common multiple of the two denominators as the denominator of the result, not their product. This would look like this.

public Rational add(Rational rhs) {
    BigInteger commonFactor = den.gcd(rhs.den);
    BigInteger resultNumerator = 
        num.multiply(rhs.den).add(rhs.num.multiply(den)).divide(commonFactor);
    BigInteger resultDenominator = den.multiply(rhs.den).divide(commonFactor);

    return new Rational(resultNumerator, resultDenominator);
}

To use this exactly how I've written it, you'll need a new constructor that takes two BigInteger arguments; but you probably want that anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top