I'm having trouble understanding how to properly read the JavaDoc page for BigInteger. The part that is particularly confusing deals with the constructor summary
In my book, there is a class example called Rational that has 3 different constructors:
and different methods:
It has two instance variables:
I have to rewrite the Rational class example in my book using:
BigInteger num
BigInteger den
as the instance variables.
The problem is I don't understand:
Am i looking for the parameters in the constructors to select which constructor I want to use? I'm just not sure what I'm reading. Is there a particular technique or method I should be using when reading the Javadoc page for BigInteger?
http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#BigInteger(byte[])
It sounds like you are confused about the BigInteger
constructors on the javadoc; the truth is, from what you say, you only need the one needed to convert int
to BigInteger
(through String)! You just need to change your constructors in Rational
to use BigInteger. If the Rational
constructors take in int
datatypes, simply convert them within the constructor to BigInteger
type and assign to your BigInteger
instance variables. If you look at the methods, there are equivalents to the basic operations you need (add, subtract, etc) for the other methods in the class.
EDIT: I'll give a small example. I assume your Rational(int x, int y)
constructor looks something like this:
public Rational (int x, int y) {
num = x;
dem = y;
}
If num
and den
are now BigIntegers, you'll need to change it so x and y are converted to BigIntegers. If you look at the BigInteger
constructors, there are none that take int directly. But we have BigInteger(String val)
, and int can be converted to String
.
public Rational (int x, int y) {
num = new BigInteger(Integer.toString(x));
dem = new BigInteger(Integer.toString(y));
}
With this idea you can figure out the rest on your own.