Question
public class MoneyDemo { /** * @param args the command line arguments */ public static void main(String[] args) { // Named constants final int BEGINNING
public class MoneyDemo {
/** * @param args the command line arguments */ public static void main(String[] args) { // Named constants final int BEGINNING = 500; // Beginning balance final Money FIRST_AMOUNT = new Money(10.02); final Money SECOND_AMOUNT = new Money(10.02); final Money THIRD_AMOUNT = new Money(10.88);
// Create an instance of the Money class with // the beginning balance. Money balance = new Money(BEGINNING);
// Display the current balance. System.out.println("The current amount is " + balance.toString());
// Add the second amount to the balance // and display the results. balance = balance.add(SECOND_AMOUNT); System.out.println("Adding " + SECOND_AMOUNT + " gives " + balance.toString());
// Subtract the third amount from the balance // and display the results. balance = balance.subtract(THIRD_AMOUNT); System.out.println("Subtracting " + THIRD_AMOUNT + " gives " + balance.toString());
// Determine if the second amount equals // the first amount and store the result. boolean equal = SECOND_AMOUNT.equals(FIRST_AMOUNT);
// Display the result. if(equal) { // The first and second amounts are equal. System.out.println(SECOND_AMOUNT + " equals " + FIRST_AMOUNT); } else { // The first and second amounts are not equal. System.out.println(SECOND_AMOUNT + " does not equal " + FIRST_AMOUNT); }
// Determine if the third amount equals // the first amount and store the result. equal = THIRD_AMOUNT.equals(FIRST_AMOUNT);
// Display the result. if(equal) { // The third and first amounts are equal. System.out.println(THIRD_AMOUNT + " equals " + FIRST_AMOUNT); } else { // The third and first amounts are not equal. System.out.println(THIRD_AMOUNT + " does not equal " + FIRST_AMOUNT); } } }
public class Money { // The number of dollars private long dollars;
// The number of cents private long cents;
/** Constructor @param amount The amount in decimal format. */
public Money(double amount) { if (amount
// ADD LINES FOR TASK #1 HERE // Document and write a copy constructor
/** The add method @param otherAmount The amount of money to add. @return The sum of the calling Money object and the parameter Money object. */
public Money add(Money otherAmount) { Money sum = new Money(0);
sum.cents = this.cents + otherAmount.cents;
long carryDollars = sum.cents / 100;
sum.cents = sum.cents % 100;
sum.dollars = this.dollars + otherAmount.dollars + carryDollars;
return sum; }
/** The subtract method @param amount The amount of money to subtract. @return The difference between the calling Money object and the parameter Money object. */
public Money subtract (Money amount) { Money difference = new Money(0);
if (this.cents
difference.dollars = this.dollars - amount.dollars; difference.cents = this.cents - amount.cents;
return difference; }
/** The compareTo method @param amount The amount of money to compare against. @return -1 if the dollars and the cents of the calling object are less than the dollars and the cents of the parameter object. 0 if the dollars and the cents of the calling object are equal to the dollars and cents of the parameter object. 1 if the dollars and the cents of the calling object are more than the dollars and the cents of the parameter object. */
public int compareTo(Money amount) { int value;
if(this.dollars amount.dollars) value = 1; else if (this.cents amount.cents) value = 1; else value = 0;
return value; }
// ADD LINES FOR TASK #2 HERE // Document and write an equals method // Document and write a toString method }
I am confused of what I am doing please help. Please in Java only. I don't know whot to do in C++
Task #2 writing the equals and toString methods 1. Write an equals method. The method compares the instance variables of the calling object with instance variables of the parameter object for equality and returns true if the dollars and the cents of the calling object are the same as the dollars and the cents of the parameter object. Otherwise, it returns false. 2. Write a toString method. This method will return a String that looks like currency, including the dollar sign. Remember that if you have less than 10 cents, you will need to put a O before printing the cents so that t appears correctly with 2 decimal places 3. Compile, debug, and test by running the MoneyDemo program. You should get the following output: The current amount is $500.00 Adding $10.02 gives $510.02 Subtracting $10.88 gives $499.14 $10.02 equals $10.02 $10.88 does not equal $10.02
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started