(Huge Integer Class) Create a class HugeInteger which uses a 40-element array of
ID: 3557356 • Letter: #
Question
(Huge Integer Class) 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. If you feel ambitious, also provide methods multiply, divide and remainder. [Note: Primitive boolean values can be output as the word âtrueâ or the word âfalseâ with format specifier %b.]
Explanation / Answer
/************************************
program works with the constraint that both number are positive
************************************/
import java.io.*;
import java.lang.*;
import java.util.Arrays;
class Test{
public boolean isLess;
public boolean isGreater;
public boolean isEqual;
public int ArrayDiff[]=new int[40];
public int AddRes[]=new int[40];
public int SubRes[]=new int[40];
public boolean isEqualTo(int Num1[],int Num2[]){
isEqual=Arrays.equals(Num1,Num2);
return isEqual;
}
public boolean isNotEqualTo(int Num1[],int Num2[]){
return (!Arrays.equals(Num1,Num2));
}
public boolean isGreaterThan(int Num1[],int Num2[]){
for(int i=0;i<40;i++){
ArrayDiff[i]=Num1[i]-Num2[i];
}
for(int i=0;i<40;i++){
if(ArrayDiff[i]>0){
isGreater=true;
return true;
}else if(ArrayDiff[i]==0){
continue;
}
else{
isLess=true;
return false;
}
}
return false;
}
public boolean isLessThan(int Num1[],int Num2[]){
for(int i=0;i<40;i++){
ArrayDiff[i]=Num1[i]-Num2[i];
}
for(int i=0;i<40;i++){
if(ArrayDiff[i]<0){
isLess=true;
return true;
}else if(ArrayDiff[i]==0){
continue;
}
else{
isGreater=true;
return false;
}
}
return false;
}
public boolean isGreaterThanOrEqualTo(int Num1[],int Num2[]){
if(isGreater || isEqual)
return true;
else
return false;
}
public boolean isLessThanOrEqualTo(int Num1[],int Num2[]){
if(isLess || isEqual)
return true;
else
return false;
}
public String Add(int Num1[],int Num2[]){
int carry=0;
String str="";
for(int i=39;i>=0;i--){
AddRes[i]=(Num1[i]+Num2[i]+carry)%10;
carry=(Num1[i]+Num2[i])/10;
}
for(int i=0;i<40;i++)
str+=AddRes[i]+"";
return str;
}
public String Sub(int Num1[],int Num2[]){
String str="";
if(isGreater){
int borrow=0;
str="";
for(int i=39;i>=0;i--){
if(Num1[i]<Num2[i]){
borrow=10;
Num1[i-1]--;
}
else {
borrow=0;
}
SubRes[i]=Num1[i]-Num2[i]+borrow;
}
}else{
int borrow=0;
str="-";
for(int i=39;i>=0;i--){
if(Num2[i]<Num1[i]){
borrow=10;
Num2[i-1]--;
}
else {
borrow=0;
}
SubRes[i]=Num2[i]-Num1[i]+borrow;
}
}
for(int i=0;i<40;i++)
str+=SubRes[i]+"";
return str;
}
}
class HugeInteger
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Eneter first huge Integer");
String Integer1=br.readLine();
System.out.println("Eneter second huge Integer");
String Integer2=br.readLine();
int HugeInteger1[]=new int[40];
int HugeInteger2[]=new int[40];
for(int i=0;i<Integer1.length();i++){
HugeInteger1[39-i]=Integer.parseInt((Integer1.charAt(Integer1.length()-1-i))+"");
}
for(int i=0;i<Integer2.length();i++){
HugeInteger2[39-i]=Integer.parseInt((Integer2.charAt(Integer2.length()-1-i))+"");
}
System.out.println();
for(int i=0;i<40;i++)
System.out.print(HugeInteger1[i]);
System.out.println();
for(int i=0;i<40;i++)
System.out.print(HugeInteger2[i]);
Test test=new Test();
System.out.println();
System.out.print("isEqualTo : "+test.isEqualTo(HugeInteger1,HugeInteger2));
System.out.println();
System.out.print("isNotEqualTo : "+test.isNotEqualTo(HugeInteger1,HugeInteger2));
System.out.println();
System.out.print("isGreaterThan : "+test.isGreaterThan(HugeInteger1,HugeInteger2));
System.out.println();
System.out.print("isLessThan : "+test.isLessThan(HugeInteger1,HugeInteger2));
System.out.println();
System.out.print("isGreaterThanOrEqualTo : "+test.isGreaterThanOrEqualTo(HugeInteger1,HugeInteger2));
System.out.println();
System.out.print("isLessThanOrEqualTo : "+test.isLessThanOrEqualTo(HugeInteger1,HugeInteger2));
System.out.println();
System.out.println("Addition");
System.out.print(test.Add(HugeInteger1,HugeInteger2));
System.out.println();
System.out.println("Subtraction");
System.out.print(test.Sub(HugeInteger1,HugeInteger2));
System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.