In: Computer Science
Problem: HugeIntegerClass
Create a class HugeInteger which uses a 40-element array of digits to store integers as large as 40 digits each. Provide methods Input, toString, Add, and Subtract. For comparing HugeInteger objects, provide the following methods: IsEqualTo, IsNotEqualTo, IsGreaterThan, IsLessThan, IsGreaterThanOrEqualTo and IsLessThanOrEqualTo. Each of these is a method that returns true if the relationship holds between the two HugeInteger objects and returns false if the relationship does not hold. Provide method IsZero. In the Input method, use the string method toCharArray to convert the input string into an array of characters, then iterate through these characters to create your HugeInteger. If you feel ambitious, provide methods Multiply, Divide, and Remainder
// Driver Class
Import.java.*;
public class <LastName>_HugeIntegerTest
{
public static void Main(string[] args)
{
HugeInteger integer1 = new
HugeInteger();
HugeInteger integer2 = new
HugeInteger();
System.out.println("Enter
first HugeInteger: ");
integer1.Input(Console.ReadLine());
System.out.println ("Enter
second HugeInteger: ");
integer2.Input(Console.ReadLine());
System.out.println
("HugeInteger 1”+ integer1);
System.out.println ("HugeInteger 2”
+integer2);
HugeInteger result;
// add two
HugeIntegers
result =
integer1.Add(integer2);
System.out.println ("Add result” +
result);
// subtract two
HugeIntegers
result =
integer1.Subtract(integer2);
System.out.println ("Subtract
result” + result);
// compare two
HugeIntegers
System.out.println ( "HugeInteger 1
is zero: “ + integer1.IsZero());
System.out.println ( "HugeInteger 2
is zero: “ + integer2.IsZero());
System.out.println (
"HugeInteger 1 is
equal to HugeInteger 2: “ + integer1.IsEqualTo(integer2));
System.out.println (
"HugeInteger 1 is
not equal to HugeInteger 2:” +
integer1.IsNotEqualTo(integer2));
System.out.println (
"HugeInteger 1 is
greater than HugeInteger 2: “ +
integer1.IsGreaterThan(integer2));
System.out.println (
"HugeInteger 1 is
less than HugeInteger 2:” + integer1.IsLessThan(integer2));
System.out.println (
"HugeInteger 1 is
greater than or equal to HugeInteger 2: “ +
integer1.IsGreaterThanOrEqualTo(integer2));
System.out.println ( "HugeInteger 1
is less than or equal to HugeInteger 2: “ +
integer1.IsLessThanOrEqualTo(integer2));
}
}
Sample Run 1:
Enter first HugeInteger:
1234567890123456789012345678901234567890
Enter second HugeInteger:
0987654321098765432109876543210987654321
HugeInteger1 +1234567890123456789012345678901234567890
HugeInteger2 +987654321098765432109876543210987654321
Add result+2222222211222222221122222222112222222211
Subtract result+246913569024691356902469135690246913569
HugeInteger 1 is zero: false
HugeInteger 2 is zero: false
HugeInteger 1 is equal to HugeInteger 2: false
HugeInteger 1 is not equal to HugeInteger 2:true
HugeInteger 1 is greater than HugeInteger 2: true
HugeInteger 1 is less than HugeInteger 2:false
HugeInteger 1 is greater than or equal to HugeInteger 2: true
HugeInteger 1 is less than or equal to HugeInteger 2: false
Sample Run 2:
Enter first HugeInteger:
65479876253763637782636782636
Enter second HugeInteger:
989
HugeInteger1 +65479876253763637782636782636
HugeInteger2 +989
Add result+65479876253763637782636783625
Subtract result+65479876253763637782636781647
HugeInteger 1 is zero: false
HugeInteger 2 is zero: false
HugeInteger 1 is equal to HugeInteger 2: false
HugeInteger 1 is not equal to HugeInteger 2:true
HugeInteger 1 is greater than HugeInteger 2: true
HugeInteger 1 is less than HugeInteger 2:false
HugeInteger 1 is greater than or equal to HugeInteger 2: true
HugeInteger 1 is less than or equal to HugeInteger 2: false
public class HugeInteger
{
private final int DIGITS = 40;
private int[] integer;// array containing the integer
private boolean positive; // whether the integer is positive
// parameterless constructor
public HugeInteger()
{
//
}
// Convert a string to HugeInteger
public void Input(String inputstring)
{
//
}
// add two HugeIntegers
public HugeInteger Add(HugeInteger addValue)
{
//
}
// add two positive HugeIntegers
private HugeInteger AddPositives(HugeInteger addValue)
{
//
}
// subtract two HugeIntegers
public HugeInteger Subtract(HugeInteger subtractValue)
{
//
}
// subtract two positive HugeIntegers
private HugeInteger SubtractPositives(HugeInteger subtractValue)
{
//
}
// find first non-zero position of HugeInteger
private int FindFirstNonZeroPosition()
{
//
}
// get string representation of HugeInteger
public String toString()
{
//
}
// test if two HugeIntegers are equal
public boolean IsEqualTo(HugeInteger compareValue)
{
//
}
// test if two HugeIntegers are not equal
public boolean IsNotEqualTo(HugeInteger compareValue)
{
//
}
// test if one HugeInteger is greater than another
public boolean IsGreaterThan(HugeInteger compareValue)
{
//
}
// test if one HugeInteger is less than another
public boolean IsLessThan(HugeInteger compareValue)
{
//
}
// test if one HugeInteger is greater than or equal to another
public boolean IsGreaterThanOrEqualTo(HugeInteger compareValue)
{
//
}
// test if one HugeInteger is less than or equal to another
public boolean IsLessThanOrEqualTo(HugeInteger compareValue)
{
//
}
// test if one HugeInteger is zero
public boolean IsZero()
{
//
}
//Optional
public HugeInteger multiply(HugeInteger multiplyValue)
{
//
}
}
Code:
import java.*;
public class Main
{
public static void main(String[] arg) {
HugeInteger x = new
HugeInteger("123456789012345678901234567890123456799");
HugeInteger y = new
HugeInteger("123456789012345678901234567890123456780");
HugeInteger z = new HugeInteger("0000");
if(x.isEqualTo(y))
System.out.println(x + " Is Equal To " + y);
if(x.isLessThan(y))
System.out.println(x + " Is less Than " + y);
else
System.out.println(x + " Is Not less Than " + y);
if(x.isNotEqualTo(y))
System.out.println(x + " Is Not Equal To " + y);
if(x.isGreaterThan(y))
System.out.println(x + " Is Greater Than " + y);
else
System.out.println(x + " Is Not Greater Than " + y);
if(x.isGreaterThanOrEqualTo(y))
System.out.println(x + " Is Greater Than Or Equal To " + y);
if(x.isLessThanOrEqualTo(y))
System.out.println(x + " Is Less Than Or Equal To " + y);
if(z.isZero())
System.out.println("z:" + z + " Is zero.");
if(x.isZero())
System.out.println("x:" + x + " Is zero.");
else
System.out.println("x:" + x + " Is Not zero.");
HugeInteger s = x.subtract(y);
HugeInteger a = x.add(y);
System.out.println("subtract: " + s);
System.out.println("add: " + a);
}
}
class HugeInteger {
int[] digits;
boolean error;
HugeInteger(String str) {
parse(str);
}
HugeInteger(char[] array) {
this(new String(array));
}
HugeInteger(int length) {
digits = new int[length]; // initialize to 0
}
int size() {
return digits.length;
}
void parse(String str) {
digits = new int[str.length()];
for(int i = 0; i < str.length(); i++)
digits[i] = str.charAt(i) - '0';
}
public String toString() {
String str = "";
int i;
for(i = size() -1; i >= 0; i--) {
if(digits[i] != 0)
break;
}
System.out.println();
for(int j = i; j >= 0; j--) {
str = digits[j] + str;
}
return str;
}
public boolean isEqualTo(HugeInteger X) {
if (this.error || X.error){
return false;
}
for (int i = 0; i < this.size(); i++)
if (this.digits[i] != X.digits[i])
return false;
return true;
}
public boolean isNotEqualTo(HugeInteger X) {
return !error && !X.error &&
!this.isEqualTo(X);
}
public boolean isGreaterThan(HugeInteger X) {
return !error && !X.error && !this.isEqualTo(X)
&& !this.isLessThan(X);
}
public boolean isLessThan(HugeInteger X) {
for (int i = size() - 1; i >= 0; i--) {
if (digits[i] < X.digits[i])
return true;
if (digits[i] > X.digits[i])
return false;
}
return false;
}
public boolean isGreaterThanOrEqualTo(HugeInteger X) {
return !error && !X.error &&
!this.isLessThan(X);
}
public boolean isLessThanOrEqualTo(HugeInteger X) {
return !error && !X.error &&
!this.isGreaterThan(X);
}
public boolean isZero() {
HugeInteger z = new HugeInteger("0");
if ((z.toString()).equals(this.toString()))
return true;
else
return false;
}
void checkLength(int length) {
length += 1;
// test size
if(size() >= length)
return;
// make copy
int[] x = new int[length];
int k = length;
for(int i = size() -1; i >= 0; i--)
x[--k] = digits[i];
// new array
digits = x;
}
public HugeInteger add(HugeInteger X) {
int length = size();
if(X.size() > length)
length = X.size();
checkLength(length);
X.checkLength(length);
HugeInteger result = new HugeInteger(length + 1);
int carry = 0;
for(int i = length; i >= 0; i--) {
result.digits[i] = digits[i] + X.digits[i] + carry;
if(result.digits[i] < 10) {
carry = 0;
}
else {
carry = result.digits[i] % 10;
result.digits[i] %= 10;
}
}
return result;
}
public HugeInteger subtract(HugeInteger X) {
int borrow = 0;
HugeInteger result = new HugeInteger(this.size());
if (error || X.error || this.isLessThan(X)) {
System.out.println("Can't handle negative Huge Integers.");
error = true;
return this;
}
for (int i = 0; i < X.size(); i++) {
result.digits[i] = this.digits[i] - X.digits[i] + borrow;
if (result.digits[i] >= 0)
borrow = 0;
else {
result.digits[i] += 10;
borrow = 1;
}
}
return result;
}
}
Program:
Output: