multiplicationadditionbcd

Self-Made BCD Class - Multiplying BCDs Error


I am making a BCD class as an exercise for school, and am encountering some issues. Below is my BCD class.

My problem is with the multiplyBCDs method. It works fine with smaller numbers such as 4,329 * 4, however, with larger products, such as the product of 4,329 and 29,385, I receive a NullPointerException error at the first line of my addBCDs method:

int[] added = new int[other.numberOfDigits()];

I have tried retracing the problem and could not find the issue. Why am I receiving this error and how could I fix it?

public class BCD {
    
private int[] digits;

//Constructor: takes in an array of integers as an argument
public BCD(int bcdDigits[]){
    digits = bcdDigits;
}

//Constructor: takes in an integer argument
public BCD(int bcdDigits){
    int b = bcdDigits;
    int count = 0;
    int c = 0;
    
    int length = String.valueOf(bcdDigits).length();
    
    digits = new int[length];
    while(b >= 1){
        c = b % 10;
        b = b / 10;

        digits[count] = c;
        count++;

    }
    
}

//Returns the number of digits in the BCD
public int numberOfDigits(){
    return digits.length;
}



//Returns the nth digit in a BCD
public int nthDigit(int n){
    if(n >= digits.length)
        return 0;
    else
        return digits[n];
}


//Prints digits in reverse order. After every index divisible by three, it outputs a comma.
public void print(){
            
    int count = 1; 
    
    for (int x=1; x<=digits.length; x++){
        if (digits.length<=3){
            System.out.print(digits[digits.length-x]); }
        else {
            if (digits.length % 3==0){
                if (count % 3==0  && count != digits.length)
                    System.out.print(digits[digits.length-x]+"," );
                else
                    System.out.print(digits[digits.length-x]);
            }
            if (digits.length % 3==1){
                if (count % 3==1  && count != digits.length)
                    System.out.print(digits[digits.length-x]+"," );
                else
                    System.out.print(digits[digits.length-x]);
            }
            if (digits.length % 3==2){
                if (count % 3==2  && count != digits.length)
                    System.out.print(digits[digits.length-x]+"," );
                else
                    System.out.print(digits[digits.length-x]);
            }
        }
        count++;
    }
    System.out.println();
}

//Adds one more digit to the BCD 
public void addADigit(int newDigit){
    
    
    int[] tempArray = new int[digits.length + 1];
    
    for(int x = 0; x < tempArray.length - 1 ; x++){
            tempArray[x] = digits[x];
    }
    
    tempArray[digits.length] = newDigit;        
    digits = tempArray;
        
}

//Adds two BCDs together and creates a new BCD
public BCD addBCDs(BCD other){
    
                //converts the BCD other to an array
    int[] added = new int[other.numberOfDigits()];
        for(int x = 0; x < other.numberOfDigits(); x++){
            added[x] = other.nthDigit(x);
        }
    
    
    int greater[];
    int smaller[];
    int diff;
            
                //finding greater addends
    
    if(added.length > digits.length){
        greater = added.clone();
        smaller = new int[greater.length];
        
        for(int x = 0; x < greater.length; x++){
            if(x < digits.length)
                smaller[x] = digits[x];
            else
                smaller[x] = 0;
        }
    }
    else{

        greater = digits.clone();
        smaller = new int[greater.length];

        for(int x = 0; x < greater.length; x++){
            if(x < added.length)
                smaller[x] = added[x];
            else 
                smaller[x] = 0;
        }   
    
    }
            
    int carry = 0;
    
    int[] sum = new int[greater.length + 1];

    for(int x = 0; x < sum.length - 1; x++){
        sum[x] = ((greater[x] + smaller[x]) % 10 + carry);
        carry = (greater[x] + smaller[x]) / 10;
    }
    
    sum[sum.length - 1] = carry;
    
    
    int[] newSum;
    BCD ans;
    
    if(sum[sum.length - 1] == 0){
        newSum = new int[sum.length - 1];
        for(int x = 0; x < sum.length - 1; x++){
            newSum[x] = sum[x];
        }
        
        ans = new BCD(newSum);
    }
    else{
        newSum = sum.clone();
        ans = new BCD(newSum);
    }
    
    return ans;
    
}
    
public BCD multiplyByTen(){
    BCD ans;
    int[] newDigits;
    
    if(digits[0] == 0 && digits.length == 1){
        ans = new BCD(0); 
        return ans;
    }
    else{
        newDigits = new int[digits.length + 1];
        newDigits[0] = 0;
        
        for(int x = 1; x <= digits.length ; x++){
            newDigits[x] = digits[x - 1];   
        }
        
        ans = new BCD(newDigits);
        return ans;
    }   
}

public BCD multiplyBy(int num){
    BCD ans = null;
    
    if(digits.length == 1 && digits[0] == 0){
        ans = new BCD(0);
        return ans;
    }
    
    else if(num == 0){
        ans = new BCD(0);
        return ans;
    }
    
    else if(num == 1){
        ans = new BCD(digits);
        return ans;
    }
    else if(num == 10){
        BCD ans1 = new BCD(digits);
        ans = ans1.multiplyByTen();
        return ans;
    }
    else{
        int carry = 0;
        int remainder = 0;
        int prod = 0;
        int[] answer= new int[(digits.length)];
        int[] newDigits;
        
        for(int x = 0; x < digits.length; x++){
            prod = (num * digits[x]) + carry;
            carry = prod / 10;
            remainder = prod % 10;
            answer[x] = remainder;
                            
            
            if(x == digits.length - 1 && carry != 0){
                ans = new BCD(answer);
                int length = String.valueOf(carry).length();
                newDigits = new int[length + 1];
                
                int b = carry;
                int c = 0;
                
                while(b > 0){
                    c = b % 10;
                    b = b / 10;

                    ans.addADigit(c);   

                }
                
                }   
            }   
        }
    return ans;
    }
    

public BCD multiplyBCDs(BCD other){
    BCD newBCD = new BCD(0);
    int digit = 0;
    BCD dig = new BCD(0);
    do{
        dig = other.multiplyBy(digits[digit]);
        newBCD = newBCD.addBCDs(dig);
        
        other = other.multiplyByTen();
        digit++;
    }while (digit < digits.length);
    
    return newBCD;
}

Thanks for the help!


Solution

  • In the method:

    public BCD multiplyBy(int num)
    

    In the last else statement, the following condition is never met:

    if (x == digits.length - 1 && carry != 0)
    

    and so "ans" is never set and remains null.