Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Don't need to do number 4 -- just 1-3 (I'll provide the given Money class) MONEY CLASS: MONEY CLASS IN TEXT: /** Objects represent nonnegative

image text in transcribed

Don't need to do number 4 -- just 1-3 (I'll provide the given Money class)

MONEY CLASS:

image text in transcribed

MONEY CLASS IN TEXT:

/** Objects represent nonnegative amounts of money*/ public class Money { /** A number of dollars */ private long dollars; /** A number of cents */ private long cents; /** Constructor creates a Money object using the amount of money in dollars and cents represented with a decimal number @param amount amount to be represented */ public Money(double amount) { if (amount

/** Adds the calling Money object to the parameter Money object. @param otherAmount amount to be added @return amount of added values */ 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; } /** Subtracts the parameter Money object from the calling Money object and returns the difference. @param amount the amount to be subtracted @return the updated amount */ public Money subtract (Money amount) { Money difference = new Money(0); if (this.cents amount.dollars) { value = 1; } else if (this.cents amount.cents) { value = 1; } else { value = 0; } return value; }

}

Consider the following UML diagram. You are given a complete Person class and Address class and an incomplete Money class. You are also given two driver classes (MoneyDriver and CreditCardDriver). CreditCard balance :Money creditLimit:Money owner: Person + CreditCard(newCardHolderPerson, Iimit Money) + getBalancel):Money +getCreditLimit(): Money +getPersonals:String chargejamountMoney) :void + payment(amount:Moncy):void Money Person dollarsdong lastName:String firstName:String home: Address cents +Moneylamountdouble): +add(otherAmount Money) Money toStringl iString subtractfotherAmountMoney):Money +compareTo(otherObject:Monoy): int +equalsfotherObject Money):boolean +toString String Address street:String city String state:String ip:String toString)String Part I It is expected that all methods will be fully documented. Use @param, @return and @Override as appropriate 1. Add a copy constructor to the Money class. It should use the parameter Money object to make a duplicate Money object, by copying the value of each instance variable from the parameter object to the instance variable of the new object. What line can you add to MoneyDriver.java to test your new constructor? 2. Add a.equals method to the Money class that 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. HINT: write this "in terms of " the compareTo method you're provided 3. Adda .toString) method to the Money class that 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 0 before printing the cents so that it appears correctly with 2 decimal places Now run the MoneyDriver program. The output should be 4. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

Students also viewed these Databases questions