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.

The class HugeInteger must contain the following method:

public HugeInteger add(HugeInteger h): Returns a new HugeInteger representing the sum of this HugeInteger and h.

The class HugeInteger must contain the following constructors:

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.

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.

Please clearly comment and explain the code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!