(Solved) : Write Class Encapsulating Concept Money Assuming Money Following Attributes Dollars Cents Q42764784 . . .

Write a class encapsulating the concept of money, assuming thatmoney has the following attributes: dollars, cents

In addition to the constructors, the accessors and mutators,write the following methods:

public Money()

public Money(int dollars, int cents)

public Money add(Money m)

public Money substract(Money m)

public Money multiply(int m)

public static Money[] multiply(Money[] moneys, int amt)

public boolean equals(Money money)

public String toString()

private void normalize() // normalize dollars and centsfield

Add additional helper methods if necessary.

Tester file:

public class MoneyTester

{

public static void main(String[] args)

{

Money m1 = new Money(8, 75); // set dollars to 8 and cents to75

Money m2 = new Money(5, 80); // set dollars to 5 and cents to 80Money

Money m3 = new Money(); // initialize dollars to 0 and cents to0

System.out.println(“tBin Nary ” + “CIS36A Fall 2019 Lab1”);

System.out.println(“m1 = ” + m1);

System.out.println(“m2 = ” + m2);

System.out.println(“m3 = ” + m3);

System.out.println(“m1 equals m2? ” + m1.equals(m2));

System.out.println(“m1 equals m3? ” + m1.equals(m3));

Money m4 = m1.add(m2);

System.out.println(“m4 = m1 + m2 = ” + m1.add(m2));

Money m5 = m4.multiply(3);

System.out.println(“m5 = m4 * 3 = ” + m5);

System.out.println(“m1 + m2 + m3 + m4 = ” +

m1.add(m2).add(m3).add(m4));

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();

}

}

Expert Answer


Answer to Write a class encapsulating the concept of money, assuming that money has the following attributes: dollars, cents In ad…

Leave a Comment

About

We are the best freelance writing portal. Looking for online writing, editing or proofreading jobs? We have plenty of writing assignments to handle.

Quick Links

Browse Solutions

Place Order

About Us

× How can I help you?