Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class \"Money\" that contains attributes \"dollars\", \"quarters\", \"d

ID: 3853690 • Letter: C

Question

Create a class "Money" that contains attributes "dollars", "quarters", "dimes", "nickels", and "pennies" (all of type "int"), a 5

argument constructor (i.e., to initialize all the members), mutator and accessor methods, instance method "display" (displays

the value associated with each attribute) and an instance method "add" that receives 1 "Money" argument and returns the

updated "Money" object. In addition, create a second class "MoneyTester" that contains the "main" method that performs the

following

2. Create two "Money" instances (prompting for values is not required).

3. Adds the first "Money" instance to the second instance

4. Displays the new values of the second "money" instance attributes

Note:

1. The "add" method must use the smallest number of coins and dollars. For example, 276 pennies is represented as

2 dollars

3 quarters

0 dimes

0 nickels

1 penny

2. One hint for the "add" algorithm would be,

a. convert both "Money" object attributes to pennies

b. add the pennies for both "Money" object

c. Convert the pennies to dollars

d. Convert the remaining pennies from (c) to quarters

e. Convert remaining pennies from (d) to dimes

f. Convert remaining pennies from (e) to nickels

Explanation / Answer


Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.

____________________________

Money.java

public class Money {

// Declaring instance variables

private int dollars;

private int quarters;

private int dimes;

private int nickels;

private int pennies;

// Parameterized constructor

public Money(int dollars, int quarters, int dimes, int nickels, int pennies) {

super();

this.dollars = dollars;

this.quarters = quarters;

this.dimes = dimes;

this.nickels = nickels;

this.pennies = pennies;

}

// getters and setters

public int getDollars() {

return dollars;

}

public void setDollars(int dollars) {

this.dollars = dollars;

}

public int getQuarters() {

return quarters;

}

public void setQuarters(int quarters) {

this.quarters = quarters;

}

public int getDimes() {

return dimes;

}

public void setDimes(int dimes) {

this.dimes = dimes;

}

public int getNickels() {

return nickels;

}

public void setNickels(int nickels) {

this.nickels = nickels;

}

public int getPennies() {

return pennies;

}

public void setPennies(int pennies) {

this.pennies = pennies;

}

// Method which displays Denominations

public void displays() {

System.out.println("Dollars =" + dollars + " Quarters =" + quarters

+ " Dimes =" + dimes + " Nickels = " + nickels + " Pennies ="

+ pennies);

}

// Method which add two Money Objects

public Money add(Money money) {

// Declaring variables

int totPennies = 0, dollr, rem, quartr, dim, nickl, penni;

// Converting all denominations into pennies

int curr = dollars * 100 + quarters * 25 + dimes * 10 + nickels * 5

+ pennies * 1;

int other = money.getDollars() * 100 + money.getQuarters() * 25

+ money.getDimes() * 10 + money.getNickels() * 5

+ money.getPennies() * 1;

// Calculating total pennies

totPennies = curr + other;

// Calculating denominations from total pennies

dollr = totPennies / 100;

rem = totPennies - (dollr * 100);

quartr = rem / 25;

rem = rem - (quartr * 25);

dim = rem / 10;

rem = rem - (dim * 10);

nickl = rem / 5;

rem = rem - (nickl * 5);

penni = rem;

return new Money(dollr, quartr, dim, nickl, penni);

}

}

______________________________

MoneyTester.java

public class MoneyTester {

public static void main(String[] args) {

//Creating two Money class objects

Money money1=new Money(2,5,5,4,4);

Money money2=new Money(3,4,5,6,3);

//Displaying Money class object denominations

System.out.println("Displaying Money#1 Object :");

money1.displays();

System.out.println("Displaying Money#2 Object :");

money2.displays();

//Adding the two Money class objects

Money money3=money1.add(money2);

//Displaying the Money3 class object denominations

System.out.println("Displaying Money#3 Object :");

money3.displays();

}

}

_________________________

Output:

Displaying Money#1 Object :
Dollars =2 Quarters =5 Dimes =5 Nickels = 4 Pennies =4
Displaying Money#2 Object :
Dollars =3 Quarters =4 Dimes =5 Nickels = 6 Pennies =3
Displaying Money#3 Object :
Dollars =8 Quarters =3 Dimes =0 Nickels = 1 Pennies =2


_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote