Search code examples
javaapijavadocanalysismethodology

Learning how to read Javadoc correctly


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:

  • Rational()
  • Rational (int n)
  • Rational(int x, int y)

and different methods:

  • add
  • subtract
  • multiply
  • divide
  • gcd(Greatest common divisor)
  • toString.

It has two instance variables:

  • int num
  • int den

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:

  • how to read the constructor summary
  • which to use when I rewrite the Rational class in my book
  • what the Field Summary is and what that describes

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[])


Solution

  • 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.