it has to be as many digits as possible not just 40 digits, I\'m wodering how to
ID: 3877673 • Letter: I
Question
it has to be as many digits as possible not just 40 digits, I'm wodering how to do a linked list to do this problem
DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is only from -263 to 263-1. What if we need to manipulate integer values beyond this range? ily large integer numbers. This class must implement arithmetic operations on integers such as addition, subtraction, multiplication and comparison. You have to implement this class without using Java predefined classes, unless specified otherwise. Additionally, you have to 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. SPECIFICATIONS The class HugeInteger must contain at least the following methods 1) public HugeInteger add (HugeInteger h): Returns a new HugeInteger repre- senting the sum of this HugeInteger and h. 2) public HugeInteger subtract (HugeInteger h): Returns a new HugeInteger representing the difference between this HugeInteger andh.Explanation / Answer
Hi.. I have written the java code for hugeinteger class.
HugeInteger.java
import java.math.BigInteger;
public class HugeInteger {
BigInteger value;
public BigInteger getValue() {
return value;
}
public void setValue(BigInteger value) {
this.value = value;
}
public HugeInteger(int val) {
super();
this.value = BigInteger.valueOf(val);
}
public HugeInteger(String val) {
BigInteger ab = new BigInteger(val);
this.value = ab;
/*super();
this.value = BigInteger.valueOf(value);*/
}
public HugeInteger(){
}
public String toString(){
return this.value.toString();
}
public HugeInteger add(HugeInteger h){
HugeInteger a = new HugeInteger();
BigInteger b=this.value;
a.value = b.add(h.value);
return a;
}
public HugeInteger substract(HugeInteger h){
HugeInteger a = new HugeInteger();
BigInteger b=this.value;
a.value = b.subtract(h.value);
return a;
}
public HugeInteger multiply(HugeInteger h){
HugeInteger a = new HugeInteger();
BigInteger b=this.value;
a.value = b.multiply(h.value);
return a;
}
public int compareTo(HugeInteger h){
HugeInteger a = new HugeInteger();
BigInteger b=this.value;
return b.compareTo(h.value);
}
}
TestInteger.java
import java.util.Random;
public class TestInteger {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number;
Random r = new Random();
number = r.nextInt();
HugeInteger ab = new HugeInteger(number);
HugeInteger bb = new HugeInteger("12345678913384934324372857457548754757834549875484");
System.out.println("Big Integer Add: "+ab.add(bb).toString());
System.out.println("Big Integer Substract: "+ab.substract(bb).toString());
System.out.println("Big Integer Multiply: "+ab.multiply(bb).toString());
System.out.println("Compare Values: "+ab.compareTo(bb));
}
}
Output:
Big Integer Add: 12345678913384934324372857457548754757836370107125
Big Integer Substract: -12345678913384934324372857457548754757832729643843
Big Integer Multiply: 22471995387769755869930432625813057710359740326348586989244
Compare Values: -1
Please check the above code and let me know any issues. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.