Ask Question
16 May, 21:35

5.05) Define a class named Money whose objects represent amounts of U. S. money. The class should have two instance variables of type int for the dollars and cents in the amount of money. Include a constructor with two parameters of type int for the dollars and cents, one with one constructor of type int for an amount of dollars with zero cents and a no-argument constructor. Include the methods add and minus for addition and subtraction of amounts of money. These methods should be static methods, should each have two parameters of type Money, and return a value of type Money. Include a reasonable set of accessor and mutator methods as well as the methods equals and toString. Write a test program for your class.

+4
Answers (1)
  1. 16 May, 22:58
    0
    import java. util. Scanner;

    public class Money {

    private int dollars;

    private int cents;

    public Money ()

    {

    dollars = 0;

    cents = 0;

    }

    public Money (int dollars)

    {

    this. dollars = dollars;

    cents = 0;

    }

    public Money (int dollars, int cents)

    {

    this. dollars = dollars;

    this. cents = cents;

    if (cents > 99) {

    int totalDollars = cents / 100;

    cents - = totalDollars * 100;

    dollars + = totalDollars;

    }

    }

    public void setDollars (int newDollars)

    {

    dollars = newDollars;

    }

    public void setCents (int newCents)

    {

    cents = newCents;

    if (cents > 99)

    {

    int totalDollars = cents / 100;

    cents - = totalDollars * 100;

    dollars + = totalDollars;

    }

    }

    public int getDollars ()

    {

    return dollars;

    }

    public int getCents ()

    {

    return cents;

    }

    public static Money add (Money initial, Money toAdd)

    {

    / / We create a new Money object which will contain the result after adding the inital and toAdd money

    Money addMoney = new Money (initial. dollars + toAdd. dollars, initial. cents + toAdd. cents);

    / / Use this condition statement to make changes to addMoney and don't make any changes to initial and toAdd objects

    if (addMoney. cents > 99)

    {

    int totalDollars = addMoney. cents / 100;

    addMoney. cents - = totalDollars * 100;

    addMoney. dollars + = totalDollars;

    }

    return addMoney;

    }

    public static Money subtract (Money initial, Money toSubtract)

    {

    / / Create a new Money object that will contain the result after subtracting toSubtract from initial

    Money subMoney = new Money ();

    / / make changes to subMoney and don't make any changes to initial and toSubtract objects

    if (initial. dollars < toSubtract. dollars

    || (initial. dollars = = toSubtract. dollars && initial. cents < toSubtract. cents))

    {

    subMoney. dollars = 0;

    subMoney. cents = 0;

    return subMoney;

    }

    subMoney. dollars = initial. dollars - toSubtract. dollars;

    subMoney. cents = initial. cents - toSubtract. cents;

    if (subMoney. cents < 0)

    {

    subMoney. cents + = 100;

    subMoney. dollars--;

    }

    return subMoney;

    }

    public Money add (Money addAmount)

    {

    dollars + = addAmount. dollars;

    cents + = addAmount. cents;

    if (cents > 99) {

    int totalDollars = cents / 100;

    cents - = totalDollars * 100;

    dollars + = totalDollars;

    }

    return this;

    }

    public Money subtract (Money subAmount)

    {

    if (dollars < subAmount. dollars || (dollars = = subAmount. dollars && cents < subAmount. cents)) {

    dollars = 0;

    cents = 0;

    return this;

    }

    dollars - = subAmount. dollars;

    cents - = subAmount. cents;

    if (cents < 0) {

    cents + = 100;

    dollars--;

    }

    return this;

    }

    public boolean equals (Money other)

    {

    return (dollars = = other. dollars) && (cents = = other. cents);

    }

    public String toString ()

    {

    if (cents < 10) / / if cents < 10, then add a preceeding 0 before cents

    return "$" + dollars + ".0" + cents;

    return "$" + dollars + "." + cents;

    }

    public static class TestMoney

    {

    public static void main (String[] args) {

    / / create the scanner object

    Scanner scan = new Scanner (System. in);

    int dollar, cents;

    Money a1, a2; / / create 2 money references

    / / input the dollar and cents for first object

    System. out. print ("Enter dollar: ");

    dollar = scan. nextInt ();

    System. out. print ("Enter cents: ");

    cents = scan. nextInt ();

    / / create the first object

    a1 = new Money (dollar, cents);

    System. out. println (a1);

    / / input the dollar and cents for second object

    System. out. print ("Enter dollar: ");

    dollar = scan. nextInt ();

    System. out. print ("Enter cents: ");

    cents = scan. nextInt ();

    / / create the second object

    a2 = new Money (dollar, cents);

    System. out. println (a2);

    / / add a1 and a2 and store the result in aAdd

    Money aAdd = add (a1, a2);

    / / subtract a1 from a2 and store the result in aSub

    Money aSub = subtract (a2, a1);

    / / display the result of addition and subtraction

    System. out. println (aAdd);

    System. out. println (aSub);

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “5.05) Define a class named Money whose objects represent amounts of U. S. money. The class should have two instance variables of type int ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers