Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Huge Integer Class) Create a class HugeInteger which uses a 40-element array of

ID: 667262 • 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

I havent tested due to lack of time and 40 integers if any thing goes wrong please comment

import java.util.Scanner;

public class HugeInteger{
   int[] t=new int[40];
public static void main(String[] args){

Scanner s=new Scanner(System.in);
System.out.println("Enter the first number");
String first=s.next();
HugeInteger h1=new HugeInteger();
h1.parse(first);
HugeInteger h2=new HugeInteger();
System.out.println("Enter the Second number");
String second=s.next();
h2.parse(second);
add(h1,h2);
sub(h1,h2);
   }
private static boolean isEqualTo(HugeInteger h1, HugeInteger h2){
   int flag=0;
   for(int i=0;i<40;i++){
       if(h1.t[i]==h2.t[i]){
          
           continue;
       }else{
           flag=1;
           break;
       }
   }
   if(flag==0){
   return true;
   }else{
       return false;
   }
}
private static boolean isNotEqualTo(HugeInteger h1, HugeInteger h2){
   if(isEqualTo(h1, h2)){
       return false;
   }else{
       return true;
   }
}
private static boolean isGreaterThan(HugeInteger h1, HugeInteger h2){
   int flag=0;
   for(int i=0;i<40;i++){
   if(h1.t[i]>=h2.t[i]){
   if(h1.t[i]>h2.t[i]){
       break;
   }
   else{
       continue;
   }
   }else{
       flag=1;
       break;
   }  
   }
   if(isEqualTo(h1, h2)){
       flag=1;
   }
   if(flag==0){
       return true;
       }else{
           return false;
       }
}
private static boolean isGreaterThanOrEqualTo(HugeInteger h1, HugeInteger h2){
   if(isEqualTo(h1, h2)|| isGreaterThanOrEqualTo(h1, h2)){
       return true;
   }else{
       return false;
   }
}
private static boolean isLessThanOrEqualTo(HugeInteger h1, HugeInteger h2)
{
   if(!isGreaterThan(h1, h2)){
       return true;
   }else if(isEqualTo(h1, h2)){
       return true;
   }
   else{
       return false;
   }
   }
private static void sub(HugeInteger h1, HugeInteger h2) {
   int[] sub=new int[40];
   int remainder=-1;
if(isEqualTo(h1, h2)){
   System.out.println("Subtract="+0);
}else if(isGreaterThan(h1, h2)) {
   {
       for(int i=39;i>=0;i--){
           if(h1.t[i]<h2.t[i]){
               if(remainder==-1){
               remainder=h1.t[i-1]-1;
               sub[i]=h1.t[i]+10-h2.t[i];
               }else{
                   if(remainder<h2.t[i]){
                       sub[i]=remainder+10-h2.t[i];
                       remainder=h1.t[i-1]-1;
                   }
               }
           }else{
               if(remainder>-1){
               sub[i]=remainder-h2.t[i];
               remainder=-1;
               }
               else{
                   sub[i]=h1.t[i]-h2.t[i];
               }
           }
       }
   }
}else{
   sub(h2,h1);
}
System.out.print("Sub:");
for(int j=0;j<40;j++){
   System.out.print(sub[j]);
}  
}
private static void add(HugeInteger h1, HugeInteger h2) {
   int carry=0;
   int[] sum=new int[41];
   for(int i=0;i<40;i++){
       sum[40-i]=h1.t[39-i]+h2.t[39-i]+carry;
       if(sum[40-i]>=10){
           carry=1;
           sum[40-i]=sum[40-i]%10;
       }else{
           carry=0;
       }
   }
   if(carry==1){
       sum[0]=1;
   }
   System.out.print("Sum:");
   for(int j=0;j<41;j++){
       System.out.print(sum[j]);
   }
  
}
private void parse(String digits) {
   for(int i=0;i<digits.length();i++){
       t[i]=Character.getNumericValue(digits.charAt(i));
   }
  
}

}