Question

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

Was it helpful?

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.

OTHER TIPS

how to read the constructor summary

  • Copy/Paste the dot notation under java.lang.object and prepend "new  " to create

which to use when I rewrite the Rational class in my book

  • Use the one which matches your data type. If it does not match, use the one which takes a String, then use toString() to convert what you have

what the Field Summary is and what that describes

  • The field summary lists properties of the object which store type specific metadata

what the Field Detail is and what that describes

  • The field detail describes the intent of each field

what the Method Summary is and what that describes

  • The method summary lists methods of the object which process specific data

what the Method Detail is and what that describes

  • The method detail describes the intent of each method

The use link returns all cross-references of the current class to map to other classes which extend, implement, or use it.

Code which generates JavaDoc includes the following annotations:

  • @link
  • @see
  • @serial
  • @serialField
  • @deprecated
  • @param
  • @author
  • @exception
  • @throws
  • @linkplain
  • @returns
  • @since
  • @value
  • @version

References

Each constructor is a method used to create an instance of the BigInteger class and can each take different parameters. This is how Java can differentiate between which constructor you intended to use. When you create a new BigInteger such as:

BigInteger myInt = new Rational(int num1, int num2)

Java knows you were using the constructor

Rational(int x, int y)

by matching the type and order of your passed parameters. Usually there are mutator and accessor methods to update values which belong to an instance of your class. Each of the fields are variables which belong to each instance of a BigInteger. So when you create a new BigInteger, each instance has its own field variables which are specific to the class.

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