CS 1063 Lab 2:  Use and Construct an Object from the java.math Package

The BigInteger Class


Objectives

Hand-in Requirements

All laboratories will be submitted electronically through WebCT.  Zip up your entire project folder to submit as the source.  (Right click on the laboratory folder and follow the SentTo link.) The laboratory folder should include the following:

Details

The BigInteger class located in the java.math package provides arbitrary-precision integer arithmetic.  Another class located in the package is the BigDecimal class.  You will be testing a few methods located in the BigInteger class.  An int value cannot represent arbitrarily large integer numbers.  ints have a range of -2,147,483,648 to +2,147,483,647.  Anything larger or smaller would result in erroneous calculations.  The following is part of the public interface for the BigInteger class that we will test.

Constructor: Methods:

Setup

Enter, compile, and run the statements that perform the steps below.

  1. Construct and initialize (Instantiate) two BigInteger objects named n1 and n2.

    n1 =   100000000000000
    n2 = -200000000000000

    Example:

    BigInteger n1 = new BigInteger("100000000000000");
  2. Print n1 and n2 using the toString method.

    Example:

    System.out.println("n1 = " + n1);
  3. Test the add method by adding n1 + n2 and storing the result in BigInteger sum.  Print sum with an appropriate label.

    Example:

    BigInteger sum = n1.add(n2);
    System.out.println("Sum = " + sum);
  4. Test the subtract method by subtracting n1 - n2 and storing the result in BigInteger diff1 then subtract n2 - n1 and storing the result in BigInteger diff2.  Print diff1 and diff2f with appropriate labels.

    Example:  To perform n1 - n2

    BigInteger diff1 = n1.subtract(n2);
  5. Test the multiply method by multiplying n1 by n2 and storing the result in BigInteger prod.  Print prod with an appropriate label.

  6. Test the divide method by dividing n1 by n2 and storing the result in q1 and then divide n2 by n1 and storing the result in q2.  Print q1 and q2 with appropriate labels.

  7. Test the abs (absolute value) method by taking the absolute value of n2 and storing the result in BigInteger absValue.  Print absValue with an appropriate label.

  8. Test the equals method by placing n1.equals(n2) in a System.out.println method.  Be sure and label the output.

  9. Test the gcd (greatest common divisor) method by finding the gcd of n1 and n2 and storing the result in BigInteger g.  Print g with appropriate labels.

  10. Test the max method by finding the max of n1 and n2 and storing the result in BigInteger max.  Print max with an appropriate label. 

  11. Test the min method by finding the min of n1 and n2 and storing the result in BigInteger min.  Print min with an appropriate label. 

  12. Test the mod (remainder) method by finding the mod of n1 and n2 and storing the result in BigInteger rem1 and then finding the mod of n2 mod n1 and storing the result in rem2.  Print rem1 and rem2 with appropriate labels.  This can be tricky because the mod operator only works on positive numbers.

    Example 1:

    BigInteger rem1 = n1.abs().mod( n2.abs() );

    OR

    Example 2:

    BigInteger n1Pos = n1.abs();
    BigInteger n2Pos = n2.abs();
    BigInteger rem1 = n1Pos.mod( n2Pos );
  13. Test the pow method by raising n1 to the power of 2 and storing the result in p1.  Raise n2 to the power of 3 and store the result in p2.  Print p1 and p2 with appropriate labels.