0
    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].getMoney() + ", ");
                else
                    System.out.print(m6[i].getMoney() + ")");
            }
            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].getMoney() + ", ");
                else
                    System.out.print(m7[i].getMoney() + ")");
            }
            System.out.println();
        }
    }

.

public class Money{
    private int dollars;
    private int cents;
    public Money(){
        this.dollars = 0;
        this.cents = 0;
    }
    public Money(int dollars, int cents){
        this.dollars = dollars;
        this.cents = cents;
        normalize(); 
    }
    public double getMoney() {
        double amount = this.dollars;
        double amountCent = this.cents;
        amountCent = amountCent/100;
        amount = amount + amountCent;
        return amount;
    }
    public Money add(Money m){
        Money n = new Money();
        n.dollars = this.dollars + m.dollars;
        n.cents = this.cents + m.cents;
        return n;
    }
    public Money subtract(Money m){
        Money n = new Money();
        n.dollars = this.dollars - m.dollars;
        n.cents = this.cents - m.cents;
        return n;
    }
    public Money multiply(int m){
        Money n = new Money();
        n.dollars = this.dollars * m;
        n.cents = this.cents * m;
        return n;    
    }
    public boolean equals(Money money){
        if(this.dollars == money.dollars && this.cents == money.cents){
            return true;
        }
        else{ 
            return false;
        }
    }
    public static Money[] multiply(Money[] moneys, int amt){
        for(int i=0; i < moneys.length; i++){
            moneys[i].dollars = moneys[i].dollars * amt;
            moneys[i].cents = moneys[i].cents * amt;
        }
        return moneys;
    }
    private void normalize() { 
        this.dollars = this.dollars + this.cents/100;
        this.cents = this.cents%100;
        // normalize dollars and cents field 
    }
}

There are 2 different classes MoneyTester() and Money(). There are different arrays - m6 and m7 - in MoneyTester().

m7 is an array of m6, where m6's elements are multiplied by 2.

I have to print m6 and m7 both, but the problem I am facing is that m6 is also being multiplied by 2. I haven't written anything for m6 then why is it happening like that?

PS - Ignore the first comment. I have to edit one of my previous questions, as I cannot post anymore questions(I am banned because question weren't good enough to ask).

  • Don't mix `nextLine` and `nextAnythingElse`, it doesn't do what you think it does (it does what the spec says that does, which is mostly useless and unexpected). Instead, always set your delimiter: Call `.useDelimiter("\\R")` on your scanner immediately after making it. Then, don't call nextLine. `next()` reads a line and returns it as a string. `nextInt()` reads a line and returns it as an int, etcetera. – rzwitserloot Jan 26 '22 at 19:43

0 Answers0