Question
The range of integers that can be represented in Java using a primitive data type is only from -2^63 to (2^63)-1. Write a class called
The range of integers that can be represented in Java using a primitive data type is only from -2^63 to (2^63)-1. Write a class called HugeInteger which is able to represent arbitrarily large integer values. You have to implment this class without using Java predefined classes unless specified otherwise. Additionaly, you must measure experimentally the running times of the operations implemented in your HugeInteger class and compare them with the measured running times of the corresponding operations provided by java.math.BigInteger class.
The class HugeInteger must contain at least the following methods:
1. public HugeInteger add(HugeInteger h): Returns a new HugeInteger representing the sum of this HugeInteger and h.
2. public HugeInteger subtract(HugeInteger h): Returns a new HugeInteger representing the difference between this HugeInteger and h.
3. public HugeInteger multiply(HugeInteger h): Returns a new HugeInteger representing the product between this HugeInteger and h.
4. public int compareTo(HugeInteger h): Returns a new HugeInteger representing the product between this HugeInteger and h.
5. public String toString(): Returns a string represenitng the sequence of digits corresponding to the decimal representation of this HugeInteger.
The class HugeInteger must contain the following constructors:
1. public HugeInteger(String val) creates a HugeInteger from the decimal String representation val. The string contains an optional minus sign at the beginning followed by one or more decimal digits. No other characters are allowed in the string.
1. public HugeInteger(int n) creates a random HugeInteger of n digits, the first digit being different from 0; n has to be larger or equal to 1.
Each constructor must throw an exception if the argument passed to the constructor does not comply to the specifications. You may use Java API methods for string manipulation and for pseudo-random number generation.
Also write a program HugeIntTiming.java to estimate the running time of each operation. For fixed n this program should instantiate random integers of size n (ie. of n decimal digits) and should then run each of the operations on the integers measuring the amount of time required to perform each operation. To ensure accuracy, you should run reach operation many times on a given pair of integers. Also you should run the measurements on at least 100 different pairs of random integers.
Do not forget to design a test procedure to demonstrate functionality of the HugeInteger class.
Please cleary comment and explain code.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started