Question: I have created a HugeInteger class program in JAVA. In this program my add function is not working properly and my subtract method will not
I have created a HugeInteger class program in JAVA. In this program my add function is not working properly and my subtract method will not subtract negatives properly. Help me fix these please.
import java.io.*; import java.util.Scanner;
//This program deals with exceptionally long integer values (max length 40 digits) and performs basic operations //such as add, subtract, comparison, greater than, less than, equal to with the input two long integers //Author
class HugeInteger {//Begin class HugeInteger
//Declare variables private final int Digits = 40;// constant to decide number of maximum digits in a huge integer private int[] integer;// array to hold the big integer digits private boolean positive;// boolean variable to hold the sign of the huge integer number. // Default constructor to initialize the huge integer array with the size and to set the integer to a positive number. public HugeInteger() { integer = new int [Digits]; positive = true; } //Method to parse the input string to a huge integer type @param inputString public void input(String inputString) { // converting input string to character array char[] integerChar = inputString.toCharArray(); // checking whether the element at zeroth index is the sign minus (-) or not if (integerChar[0] == '-') positive = false;// if yes then set the positive flag to false. if(positive)// if the number is positive then set the integer digit at index (40 - input number length) = 0 integer[Digits - integerChar.length] = (int)integerChar[0] - (int) '0'; for (int i = 1; i < integerChar.length; i ++)// assign rest digits of the huge integer to the digits of input number integer[Digits - integerChar.length+i]= (int)integerChar[i] - (int) '0'; } //Method to add two huge integers @param HugeInteger @return HugeInteger public HugeInteger add(HugeInteger addValue) { HugeInteger temp = new HugeInteger();// creating a temporary HugeInteger object to store the added value // checking whether this HugeInteger is positive and the HugeInteger as parameter is positive // OR if both the HugeInteger is negative or not if ((positive && addValue.positive) ||!(positive || addValue.positive)) // if yes then add the both HugeInteger normally by calling the helper method addPositives() normally temp = addPositives(addValue); // else if this HugeInteger is positive and parametric hugeIntger is negative else if (positive && (!addValue.positive)) { // then make the parametric sign to positive addValue.positive = true; // checking whether parametric HugeIntger is greater or not after changing its sign
// if yes then subtract the two numbers temp = (number1 - number2) if (isGreatThan (addValue)) temp = subtractPositives(addValue); // else subtract the two numbers by exchanging them temp = (number2 - number1) else { temp = addValue.subtractPositives(this); temp.positive = false; } addValue.positive = false;//Reset sign for addValue
} // else if this HugeInteger is positive and the parametric HugeIntger is positive then else if (!positive && addValue.positive) { // assign the parametric HugeInteger sign flag to false addValue.positive = false; // if parametric HugeInteger is greater then perform => temp = number2 - (-number1) if(isGreatThan(addValue)) temp = addValue.subtractPositives(this); // else perform temp = number1 - (-number2) else { temp = subtractPositives(addValue); temp.positive = false; } addValue.positive = true;//Reset sign for addValue } return temp;//Return the sum } // Method to subtract 2 HugeInteger numbers @param HugeInteger @return HugeInteger public HugeInteger subtract(HugeInteger subtractValue) { HugeInteger temp = new HugeInteger(); // checking if this HugeInteger is positive and parametric HugeInteger is negative if (positive && (!subtractValue.positive)) temp = addPositives(subtractValue);// if yes then perform => temp = number1 + (-number2) // else if this HugeInteger is negative and parametric HugeIntger is positive then else if(!positive && subtractValue.positive) { temp = addPositives(subtractValue);// perform temp = number1 + (-number2) } if(positive && subtractValue.positive) temp = subtractValue.subtractPositives(this);// else call this method if both the numbers are positive return temp; }
//Method to add 2 positive HugeInteger numbers @param HugeIntger to be added @return HugeInteger public HugeInteger addPositives(HugeInteger addValue) { HugeInteger temp = new HugeInteger(); int carry = 0;// to store the carry number // looping from unit's place digit to highest place digit for ( int i = 39; i <= 0; i --) { // adding each digit of both numbers and adding the carry and saving at position i in temp array temp.integer[i] = integer[i] + addValue.integer[i] + carry; // checking whether the value of temp array at index i is greater then 9 if(temp.integer[i] > 9) { // if yes then divide by 10 and store the remainder into it temp.integer[i] %= 10; carry = 1;// assign carry as 1 } else carry = 0;// else assign carry as 0 } if(!positive) temp.positive = false; return temp; }
// Method to add subtract 2 HUgeIntegers @param subtractValue @return public HugeInteger subtractPositives(HugeInteger subtractValue) { int borrow = 0;// to store the borrow digit HugeInteger temp = new HugeInteger(); // looping from unit's place digit to highest place digit for(int i = 39; i >= 0; i --) { // checking whether this HugeInteger's digit at index i is less then parametric intger's digit at index i if(integer[i] < subtractValue.integer[i]) { // if yes then add 10 to this integer's digit at index i and then subtract the parametric digit
// at index i from this integer as well as subtract the borrow temp.integer[i] = (integer[i] + 10) - subtractValue.integer[i] - borrow; borrow = 1;// assign borrow to 1 } else { temp.integer[i]= integer[i] - subtractValue.integer[i] - borrow; borrow = 0;// else assign borrow as 0 } } return temp; }
//Method to find the position(index) of first non-zero number into a HugeIntger @return position public int findFirstNonZeroPosition() { int position = 39; for (int i = 0; i < Digits; i ++) { if(integer[i] > 0) { position = i; break; } } return position; }
//Method to filter the HugeIntger into the exact form by removing the redundant 0's @return String as HugeInteger public String toHugeIntegerString() { String sign = "", output = ""; if(!positive) sign = "-"; for (int i = findFirstNonZeroPosition(); i < Digits; i ++) output += integer[i]; return sign + output; }
//Method to check whether two HugeInteger are equal or not @param HugeInteger @return public boolean isEqualTo(HugeInteger compareValue) { for (int i = 0; i < Digits; i ++) { if(integer[i] != compareValue.integer[i]) return false; } if((positive && compareValue.positive) ||! (positive || compareValue.positive)) return true; else return false; }
//Method to check whether the two hugeInteger are unequal @param compareValue @return public boolean isNotEqualTo(HugeInteger compareValue) { return !isEqualTo(compareValue); }
//Method to check whether 1st hugeInteger is greater then 2nd @param HugeIntger @return boolean public boolean isGreatThan(HugeInteger compareValue) { // if 2nd HugeInteger is negative if(positive && (!compareValue.positive)) return true;// if yes then return true else if(!positive && compareValue.positive)// checking whether 1st HugeIntger is negative or not return false;// if yes the return false else { // else find first non zero position and then compare which one is greater between the two HugeIntger if(findFirstNonZeroPosition() > compareValue.findFirstNonZeroPosition()) { if(positive) return false; else return true; } else if(findFirstNonZeroPosition() < compareValue.findFirstNonZeroPosition()) { if (positive) return true; else return false; } // else compare each value by value else { for (int i = 0; i < Digits; i ++) { if(integer[i] > compareValue.integer[i]) if(positive) return true; else return false; } if(positive) return false; else return true; } } }
//Method to check 1st HugeInteger is greater then parametric HugeInteger @param HugeInteger @return boolean public boolean isLessThan(HugeInteger compareValue) { return !(isGreatThan(compareValue) || isEqualTo(compareValue)); }
//Method to find the 1st HugeIntgers is greater then or equal to parametric HugeIntger* @param compareValue @return public boolean isGreatThanOrEqualTo(HugeInteger compareValue) { return !isLessThan(compareValue); }
//Method to find 1st HugeIntger is less then or equal to parametric HugeIntger @param compareValue @return public boolean isLessThanOrEqualTo(HugeInteger compareValue) { return !isGreatThan(compareValue); }
//Method to check whether a HugeInteger is 0 or not @return public boolean isZero() { for (int i = 0; i < Digits; i ++)// looping through each digit { if(integer[i] != 0)// if any of the digits are non-zero then return false return false; } return true;// else return true } }//End class definition
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
