Search code examples
javaarrayscurrency

Trying to multiply an array Object by an int. Assigning new array, however original one gets changed


public class Money {
    private int moneyDollars;
    private int moneyCents;
    private int newDollars;
    private int newCents;

    public Money() {
        moneyDollars = 0;
        moneyCents = 0;
    }
    public Money(int dollars, int cents) {
        moneyDollars = dollars;
        moneyCents = cents;
    }
    public static Money[] multiply(Money[] moneys, int amt) {
        Money[] m = new Money[moneys.length];
        for(int i = 0; i < moneys.length; i++) {
            m[i] = moneys[i];
        }
        for(int i = 0; i < moneys.length; i++) {
            moneys[i].newDollars = moneys[i].getDollars() * amt;
            moneys[i].newCents = moneys[i].getCents() * amt;
            m[i].normalize();
            m[i].moneyDollars = moneys[i].newDollars;
            m[i].moneyCents = moneys[i].newCents;
        }
        return m;
    }
    @Override
    public String toString() {
        return getDollars() + "." + getCents();
    }
    private void normalize() {
        if (newCents > 99) {
            newDollars += newCents / 100;
            newCents %= 100;
        }
        if (getCents() > 99) {
            moneyDollars += getCents() / 100;
            moneyCents %= 100;
        }
    }
}

****I am not allowed to change the MoneyTester Class at all.****

m6 should output original values m7 should output multiplied values. I have tried every combination of moneyDollars/Cents and newDollars/Cents, and combinations of Money[] m, and Money[] moneys, that I can think of. Every time m6 is changed along with m7.

public class MoneyTester{
    public static void main(String[] args)
    {
        Money[] m6 = new Money[]{new Money(10, 50), new Money(20, 50), new Money(30, 50), new Money(40, 50)};
        Money[] m7 = Money.multiply(m6, 2);

        System.out.print("m6 = (");
        for(int i = 0; i < m6.length; i++)
        {
            if(i < m6.length -1)
                System.out.print(m6[i] + ", ");
            else
                System.out.print(m6[i] + ")");
        }
        System.out.println();

        System.out.print("m7 = m6 * 2 = (");
        for(int i = 0; i < m7.length; i++)
        {
            if (i < m7.length -1)
                System.out.print(m7[i] + ", ");
            else
                System.out.print(m7[i] + ")");
        }
        System.out.println();

    }
}

Output:

m6 = (21.0, 41.0, 61.0, 81.0)
m7 = m6 * 2 = (21.0, 41.0, 61.0, 81.0)

Solution

  • When you do this line:

     for(int i = 0; i < moneys.length; i++) {
       m[i] = moneys[i];
     }
    

    You are copying the object from one array to another, but its by reference so the objects are the same. What you need to do is create a new object:

     for(int i = 0; i < moneys.length; i++) {
       m[i] = new Money(moneys[i].getDollars(), moneys[i].getCents());
     }
    

    Also the second for loop you are still modifying the orginal moneys with the assignment operators when multiplying so you want to change that to reference m not moneys.

    The for loop can also be simplified to be a single for loop:

        for(int i = 0; i < moneys.length; i++) {
            m[i] = new Money(moneys[i].getDollars(), moneys[i].getCents());
    
            m[i].newDollars = m[i].getDollars() * amt;
            m[i].newCents = m[i].getCents() * amt;
            m[i].normalize();
            m[i].moneyDollars = m[i].newDollars;
            m[i].moneyCents = m[i].newCents;
        }