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

Class: The name of the class is Money. This means that the code must be stored i

ID: 653133 • Letter: C

Question

Class: The name of the class is Money. This means that the code must be stored in the file Money.java Instance data: There are two integer values for the instance data representing the dollars and cents. These must be declared as private. Instance methods: There are six methods in the class. All methods are public. 1. setMoney(int dollarsln, int centsln) ?Set the dollars and cents to the values of the parameters. If either input is negative, set the dollars and cents to O. 2. add(Money moneyln) ? add the dollars and cents of the parameter to the current object. If the cents exceeds 100, adjust the dollars and cents accordingly. 3. subtract(Money moneyln) ? subtract the parameter from the current object If the cents fall below O, then the dollars and cents must be adjusted. If the number of dollars falls below O, then set both the dollars and cents to O 4. boolean equals(Money moneyln): return true if both the dollars and cents of the parameter match the dollars and cents of the current object, otherwise return false 5. boolean IessThan(Money moneyln): return true if the current object represents less money than the parameter. otherwise return false 6. String toString: return the values of the object as a String formatted as $dd.cc

Explanation / Answer


class Money {

private int dollars;
private int cents;
private int hundred;
private int aBuckFifty, aDollarFifty, aBuckHalf;

public Money()
{
}
public Money(int dollars)
{
}
public Money(int dollar, int cents)
{
}


  

public void setDollars(int dollars)

{

this.dollars = dollars;

}


  
  

public void setCents(int cents)

{

this.cents=cents;

}

  

public boolean equals(Money obj)

{

return ((aBuckHalf == aDollarFifty) && (hundred == cents));

}

  

public int Money add (Money m1, Money m2)

{

return m1 + m2;

}

  

public int Money minus (Money m1, Money m2)

{

return m1 - m2;

}

public String toString() {

String result;

if (dollars < 0)

result = "-$";

else

result = "$";

if (cents < 10)

result = result + Math.abs(dollars) + ".0" + cents;

else

result = result + Math.abs(dollars) + "." + cents;

return result;

}

}

public class MoneyDemo {

public static void main(String[] args) {

// Test the no-arg constructor, setter methods, and toString

Money nickel = new Money();

nickel.setCents(5);

System.out.println("A nickel: " + nickel);

Money aBuckFifty = new Money();

aBuckFifty.setDollars(1);

aBuckFifty.setCents(50);

System.out.println("A buck fifty: " + aBuckFifty);

Money cNote = new Money(100);

System.out.println("A C-Note: " + cNote);

Money aDollarFifty = new Money(1, 50);

System.out.println("One dollar and fifty cents: " + aDollarFifty);

  

System.out.println("C-Note equals a nickel? " + cNote.equals(nickel));

System.out.println("A buck fifty equals one dollar and fifty cents? " +

aBuckFifty.equals(aDollarFifty));

System.out.println();

System.out.println(

cNote + " + " +aBuckFifty + " = " + Money.add(cNote, aBuckFifty));

System.out.println(

cNote + " - " +aBuckFifty + " = " + Money.minus(cNote, aBuckFifty));

Money aDebt = Money.minus(nickel, cNote);

System.out.println(nickel + " - " +cNote + " = " + aDebt);

System.out.println(

aDollarFifty + " - " + aBuckFifty + " = " +

Money.minus(aDollarFifty, aBuckFifty));

System.out.println(

aDollarFifty + " + " + aBuckFifty + " = " +

Money.add(aDollarFifty, aBuckFifty));

System.out.println(

aDebt + " + " + aBuckFifty + " = " +

Money.add(aDebt, aBuckFifty));

}

}