Hi, so I\'m trying to add two really large numbers that are stored in an ArrayLi
ID: 3542165 • Letter: H
Question
Hi, so I'm trying to add two really large numbers that are stored in an ArrayLists of type BigInt (the name of my class which has a private ArrayList<Integer> BigNumber = new ArrayList<Integer>()). The explicit constructor of my class brings in an the number as a String and I do the conversion from the string to the ArrayList<Integer> by placing every number in a different index of the array. So this is what i have from the adding method, lets pretend that the ArrayList<BigInt> a -from the parameters is the largest one:
public ArrayList<BigInt> add(ArrayList<BigInt> a, ArrayList<BigInt> b){
ArrayList<BigInt> sum = new ArrayList<BigInt>();
int i;
int j;
int added = 0;
for(i = 0; i<a.size(); i++){
for(j=0; j<b.size(); i++){
int indexOfFirstNumber = a.get(i); // I get error here, it says to change indexOfFirstNumber from int to BigInt
int indexOfSecondNumber = b.get(j); // also here, it says to change indexOfSecondNumber from int to BigInt
added = indexOfFirstNumber + indexOfSecondNumber;
sum.add(added); // and here with the add method, suggesting to change added to BigInt
}
}
return sum;
}
I'm all confused with this variables of the type BigInt since the method is suppose to return a BigInt. Help! thanks
Explanation / Answer
import java.util.ArrayList;
public class BigInt {
private String number;
public BigInt(String number) {
super();
this.number = number;
}
public BigInt() {
}
//be able to output back the number
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
/*
*perform addition and subtraction with the numbers that will be entered from the main class*/
// check weather the input is valid (no characters, appropiate minus signs)
public void checkForValidityOfNumber(){
String numberString = number;
boolean minus = false;
if (numberString.charAt(0)== '-'){
System.out.println("Executed");
minus = true;
}
String finalNumber = "";
numberString = numberString.replaceAll("\D+","");
if (minus == true){
finalNumber = "-"+numberString;
}
else
finalNumber = numberString;
setNumber(finalNumber);
}
//*store the string from the main class into an ArrayList
// You asked for adding the number into arrayList. Actually I too need it. But not the whole length
// but every single digit is stored for addition purpose. This method is just for nothing.
// Doing it because it was mentioned in the question.
public ArrayList<String> storeIntoArrayListInWhole (BigInt big){
ArrayList<String> arrayListNumber = new ArrayList<String>();
arrayListNumber.add(big.getNumber());
return arrayListNumber;
}
// Adding each digit in arrayList
// Used for addition and subtraction
/*public ArrayList<String> storeIntoArrayList (BigInt big){
ArrayList<String> arrayListNumber = new ArrayList<String>();
if (big.getNumber().charAt(0)=='-'){
}
else {
int numberLength= big.getNumber().length();
for (int i = 0; i < numberLength; i++){
String character = ""+big.getNumber().charAt(i);
arrayListNumber.add(character);
}
}
return arrayListNumber;
}*/
//Add two large numbers
public BigInt add(BigInt b2){
BigInt b3 = null;
ArrayList<String> list1 = new ArrayList<String>();
if (getNumber().charAt(0)=='-'){
}
else {
int numberLength= getNumber().length();
for (int i = 0; i < numberLength; i++){
String character = ""+getNumber().charAt(i);
list1.add(character);
}
}
ArrayList<String> list2 = new ArrayList<String>();
if (b2.getNumber().charAt(0)=='-'){
}
else {
int numberLength= b2.getNumber().length();
for (int i = 0; i < numberLength; i++){
String character = ""+b2.getNumber().charAt(i);
list2.add(character);
}
}
int lengthOfArrayList1 = list1.size();
int lengthOfArrayList2 = list2.size();
int difference = lengthOfArrayList1 - lengthOfArrayList2;
int countBig = 0;
int countSmall = 0;
String first = "", second= "";
if (difference > 0){
while((countBig < difference))
{
list2.add(countBig,"0");
countBig++;
}
}
else if (difference < 0){
difference = difference *(-1);
while((countSmall < difference))
{
list1.add(countSmall,"0");
countSmall++;
}
}
else {
System.out.println("");
}
ArrayList<Integer> arrayAdded = new ArrayList<Integer>();
int toBeForwarded = 0;
for (int i = list1.size()-1; i >=0; i--){
String number1String = list1.get(i);
int number1 = Integer.parseInt(number1String);
String number2String = list2.get(i);
int number2 = Integer.parseInt(number2String);
int sum = number1 + number2+ toBeForwarded;
toBeForwarded = 0;
if(sum >9){
int remaining = sum % 10;
arrayAdded.add(remaining);
toBeForwarded = sum/10;
}
else{
arrayAdded.add(sum);
}
}
if(toBeForwarded != 0 ){
arrayAdded.add(arrayAdded.size(),toBeForwarded);
}
int countFirst =0;
for (int i = 0; i <first.length(); i++){
if (first.charAt(i)== '0' ){
countFirst++;
}
if (first.charAt(i)!= '0' ){
break;
}
}
first = first.substring(countFirst);
int countSecond = 0;
for (int i = 0; i <second.length(); i++){
if (second.charAt(i)== '0' ){
countSecond++;
}
if (second.charAt(i)!= '0' ){
break;
}
}
second = second.substring(countSecond);
System.out.print(" If we add "+first+" and "+second+" , we get: ");
String numberAdded ="";
for (int i = arrayAdded.size()-1; i >=0; i--){
numberAdded += arrayAdded.get(i).toString();
b3 = new BigInt(numberAdded);
}
return b3;
}
public void SubtractTwoLargeNumbers(ArrayList<String> list1, ArrayList<String> list2){
String finalString= "";
int lengthOfArrayList1 = list1.size();
int lengthOfArrayList2 = list2.size();
int difference = lengthOfArrayList1 - lengthOfArrayList2;
int countBig = 0;
int countSmall = 0;
if (difference > 0){
System.out.println("in first");
while((countBig < difference))
{
System.out.println(countBig);
list2.add(countBig,"0");
countBig++;
}
}
else if (difference < 0){
System.out.println("In second");
difference = difference *(-1);
while((countSmall < difference))
{
System.out.println(countSmall);
list1.add(countSmall,"0");
countSmall++;
}
}
else {
System.out.println("");
int list1FirstIndex = 0;
int list2FirstIndex = 0;
int differenceOfIndex = 0;
for (int i = 0;i<list1.size(); i++){
list1FirstIndex = Integer.parseInt(list1.get(i));
list2FirstIndex = Integer.parseInt(list2.get(i));
differenceOfIndex= list1FirstIndex - list2FirstIndex;
if(differenceOfIndex < 0){
finalString = subtractionLogic(list2, list1);
int count = 0;
for (int j = 0; j <finalString.length(); j++){
if (finalString.charAt(j)== '0' ){
count++;
}
if (finalString.charAt(j)!= '0' ){
break;
}
}
finalString = finalString.substring(count);
System.out.println("After subtracting, we get: -"+finalString);
break;
}
else if(differenceOfIndex > 0){
finalString = subtractionLogic(list1, list2);
int count = 0;
for (int j = 0; j <finalString.length(); j++){
if (finalString.charAt(j)== '0' ){
count++;
}
if (finalString.charAt(j)!= '0' ){
break;
}
}
finalString = finalString.substring(count);
System.out.println("After subtracting, we get: "+finalString);
break;
}
}
}
}
public static String subtractionLogic (ArrayList<String> list1, ArrayList<String> list2){
ArrayList<Integer> arraySubtracted = new ArrayList<Integer>();
int toBeForwarded = 0;
for (int i = list1.size()-1; i >=0; i--){
String number1String = list1.get(i);
int number1 = Integer.parseInt(number1String);
String number2String = list2.get(i);
int number2 = Integer.parseInt(number2String);
int differenceBetweenTwoNumbers = number1 - number2- toBeForwarded;
if(differenceBetweenTwoNumbers < 0 && i == 0){
}
else if(differenceBetweenTwoNumbers < 0){
int remaining = number1 + 10 - toBeForwarded;
differenceBetweenTwoNumbers= remaining -number2;
toBeForwarded =1;
arraySubtracted.add(differenceBetweenTwoNumbers);
}
else{
arraySubtracted.add(differenceBetweenTwoNumbers);
toBeForwarded = 0;
}
}
if(toBeForwarded != 0 ){
arraySubtracted.add(arraySubtracted.size(),toBeForwarded);
}
String finalString = "";
for (int i = arraySubtracted.size()-1; i >=0; i--){
finalString += arraySubtracted.get(i);
}
return finalString;
}
@Override
public String toString() {
return number ;
}
}
______________________
public class BigIntTest {
public static void main(String[] args) {
BigInt b1 = new BigInt("2");
System.out.println(b1.getNumber());
System.out.println("After validating the number, the number is :");
b1.checkForValidityOfNumber();
System.out.println(b1.getNumber());
System.out.println(" ");
BigInt b2 = new BigInt("4");
BigInt b3 = new BigInt();
System.out.println(b2.getNumber());
System.out.println("After validating the number, the number is :");
b2.checkForValidityOfNumber();
System.out.println(b2.getNumber());
b3 = b1.add(b2);
System.out.println("Sum is :");
System.out.println(b3.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.