( In Java ) Create a class HugeInteger which uses a 40-element array of digits t
ID: 675246 • Letter: #
Question
( In Java ) Create a class HugeInteger which uses a 40-element array of digits to store integers as large as 40 digits each. Provide methods parse, toString, add and subtract. Method parse should receive a String, extract each digit using method charAt and place the integer equivalent of each digit into the integer array. For comparing HugeInteger objects, provide the following methods: isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo and isLessThanOrEqualTo. Each of these is a predicate method that returns true if the relationship holds between the two HugeInteger objects and returns false if the relationship does not hold. Provide a predicate method isZero. A second class HugeIntegerTest will be used to ensure class HugeInteger is working and print out the results.
remainder. [Note: Primitive boolean values can be output as the word true or the word false with
format specifier %b.]
Explanation / Answer
public class HugeInteger
{
private char array;
private static char c1[];
private static char c2[];
public HugeInteger(char[] a)
{
array = a[40];
for (int i = 0; i < 40; i++)
{
a[i] = intToChar(charToInt(c1[i]));
}
for (int i = 0; i < 40; i++)
{
a[i] = intToChar(charToInt(c2[i]));
}
}
public HugeInteger add(HugeInteger i)
{
return i;
}
public HugeInteger subtract(HugeInteger i)
{
return i;
}
public boolean isEqualTo (HugeInteger i)
{
return true;
}
public boolean isNotEqualTo (HugeInteger i)
{
return true;
}
public boolean isGreaterThan (HugeInteger i)
{
return true;
}
public boolean isLessThan (HugeInteger i)
{
return true;
}
public boolean isGreaterThanOrEqualTo (HugeInteger i)
{
return true;
}
public boolean isLessThanOrEqualTo (HugeInteger i)
{
return true;
}
public static void main(String[] args)
{
char c1[] = new char[45];
c1[0] = '0';
c1[1] = '1';
c1[2] = '2';
c1[3] = '3';
c1[4] = '4';
c1[5] = '5';
c1[6] = '6';
c1[7] = '7';
c1[8] = '8';
c1[9] = '9';
c1[10] = '1';
c1[11] = '2';
c1[12] = '2';
c1[13] = '3';
c1[14] = '4';
c1[15] = '5';
c1[16] = '6';
c1[17] = '7';
c1[18] = '8';
c1[19] = '9';
c1[20] = '1';
c1[21] = '2';
c1[22] = '2';
c1[23] = '3';
c1[24] = '4';
c1[25] = '5';
c1[26] = '6';
c1[27] = '7';
c1[28] = '8';
c1[29] = '9';
c1[30] = '1';
c1[31] = '2';
c1[32] = '2';
c1[33] = '3';
c1[34] = '4';
c1[35] = '5';
c1[36] = '6';
c1[37] = '7';
c1[38] = '8';
c1[39] = '9';
c1[40] = '9';
char c2[] = new char[45];
c2[0] = '0';
c2[1] = '1';
c2[2] = '2';
c2[3] = '3';
c2[4] = '4';
c2[5] = '5';
c2[6] = '6';
c2[7] = '7';
c2[8] = '8';
c2[9] = '9';
c2[10] = '1';
c2[11] = '2';
c2[12] = '2';
c2[13] = '3';
c2[14] = '4';
c2[15] = '5';
c2[16] = '6';
c2[17] = '7';
c2[18] = '8';
c2[19] = '9';
c2[20] = '1';
c2[21] = '2';
c2[22] = '2';
c2[23] = '3';
c2[24] = '4';
c2[25] = '5';
c2[26] = '6';
c2[27] = '7';
c2[28] = '8';
c2[29] = '9';
c2[30] = '1';
c2[31] = '2';
c2[32] = '2';
c2[33] = '3';
c2[34] = '4';
c2[35] = '5';
c2[36] = '6';
c2[37] = '7';
c2[38] = '8';
c2[39] = '9';
c2[40] = '9';
System.out.println("HugeInteger 1: ");
for (int i = 0; i < 40; i++){
System.out.print("" + c1[i]);
}
System.out.println();
System.out.println("HugeInteger 2: ");
for (int j = 0; j < 40; j++)
{
System.out.print("" + c2[j]);
}
}
public static char intToChar(int c)
{
if (c == 0)
{
return '0';
}
else if (c == 1)
{
return '1';
}
else if (c == 2)
{
return '2';
}
else if (c == 3)
{
return '3';
}
else if (c == 4)
{
return '4';
}
else if (c == 5)
{
return '5';
}
else if (c == 6)
{
return '6';
}
else if (c == 7)
{
return '7';
}
else if (c == 8)
{
return '8';
}
else
{
return '9';
}
}
public static int charToInt(char c)
{
if (c == '0')
{
return 0;
}
else if (c == '1')
{
return 1;
}
else if (c == '2')
{
return 2;
}
else if (c == '3')
{
return 3;
}
else if (c == '4')
{
return 4;
}
else if (c == '5')
{
return 5;
}
else if (c == '6')
{
return 6;
}
else if (c == '7')
{
return 7;
}
else if (c == '8')
{
return 8;
}
else
{
return '9';
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.