Question

DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is only from-263 to 263-1. Wha

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are...

    I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are integers that have no size restriction. Recall that the int type has a range of -2,147,483,648 to 2,147,483,647. The long type runs from -263 to 263 - 1. Sometimes these numbers are not large enough. One example application that may need larger numbers is public-key cryptography where very large integers are used to make decryption hard. A large integer may be represented as a...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • In this assignment, you are asked to implement a Java class named HugeInt for storing huge...

    In this assignment, you are asked to implement a Java class named HugeInt for storing huge integers and performing mathematical operations on them. This class must have a private variable of type LinkedList〈Byte〉 which stores digits of a huge integer and ten of the following public methods (choose ten methods out of 12): public void setValue (int value): gets an integer (like 1234) as input parameter and stores its digits in the linked list in the same order as they...

  • A Java Program Purpose:         Work with Abstract classes. Purpose:         Abstract classes are import because they ensure...

    A Java Program Purpose:         Work with Abstract classes. Purpose:         Abstract classes are import because they ensure than any children which inherit from it must implement certain methods. This is done to enforce consistency across many different classes. Problem:        Implement a class called Student. This class needs to be abstract and should contain a String for the name, and a String for the major (and they should be private). The class must have a constructor to set the name and major....

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • Implement the following class in Java: Class name: People Constructor Summary: public People (Str...

    Implement the following class in Java: Class name: People Constructor Summary: public People (String name) Methods: public People(String name) public void setName(String name) public String getName() Class name: Truck Constructor Summary: public Truck (int licensePlate, int onBoard, People people) Initially, the truck does not contain any on board the vehicle. If the number of people on board the vehicle is a negative number, the value of onBoard should be stored as zero. Methods: public int getLicensePlate() Returns license plate of...

  • The following code must be written and run with no errors in java eclipse Description of...

    The following code must be written and run with no errors in java eclipse Description of the code to be written: A resistor is an electronic part colour-coded with three coloured bands to indicate its resistance value. This program returns the resistance of a Resistor object instantiated with three Strings, representing the three colour bands of a real resistor. Start by creating an abstract class 'AbstractResistor' that implements the Comparable interface. This class should have the following three private methods:...

  • Java Project 1. The largest positive integer of type int is 2,147,483,647. Another integer type, long,...

    Java Project 1. The largest positive integer of type int is 2,147,483,647. Another integer type, long, represents integers up to 9,223,372,036,854,775,807. Imagine that you want to represent even larger integers. For example, cryptography uses integers having more than 100 digits. Design and implement a class Huge of very large nonnegative integers. The largest integer should contain at least 30 digits. Use a deque to represent the value of an integer. Provide operations for the class that * Set the value...

  • Adv. Java program - create a math quiz

    Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:AdditionSubtractionMultiplicationDivisionModuloExponentThe following source files are already complete and CANNOT be changed:MathQuiz.java (contains the main() method)OperationType.java (defines an enumeration for valid math operations and related functionality)The following source files are incomplete and need to be coded to meet the indicated requirements:MathQuestionable.javaA Random object called RANDOM has already been declared and assigned, and you must not change this assignment in any way.Declare and assign an interface integer called MAX_SMALL and assign it the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT