0
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)
gil cohen
  • 3
  • 1
  • 3
    In your `Money.multipy` method, the line `m[i] = moneys[i];` does *not* create a new `Money` instance. Instead, it creates a new reference _to the same object_ in the `m` array. So when you modify the object at `m[i]`, you're also modifying the object at `money[i]`, because they're two difference references that point to the same object. – Jordan Nov 18 '19 at 20:22
  • Does this answer your question? [copying array by value in java](https://stackoverflow.com/questions/2371568/copying-array-by-value-in-java) – Jan Nov 18 '19 at 20:42

1 Answers1

0

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;
    }
locus2k
  • 2,649
  • 1
  • 13
  • 19
  • Thank you. This worked, thought after I initialized as new Money it would create a new reference. Also just needed to initialze the variable before the for loop with Money[] m = new Money[moneys.length]; – gil cohen Nov 19 '19 at 07:05