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

Task #1 Writing a Copy Constructor Copy the files Address.java (Code Listing 8.1

ID: 3695609 • Letter: T

Question

Task #1 Writing a Copy Constructor

Copy the files Address.java (Code Listing 8.1), Person.java (Code Listing 8.2), Money.java (Code Listing 8.3), MoneyDemo.java (Code Listing 8.4), and CreditCardDemo.java (Code Listing 8.5) from the Student CD or as directed by your instructor. Address.java, Person.java, MoneyDemo.java, and CreditCardDemo.java are complete and will not need to be modified. We will start by modifying Money.java.

Overload the constructor. The constructor that you will write will be a copy constructor. 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.

Task #2 Writing the equals and toString methods

Write and document 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.

Write and document 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 0 before printing the cents so that it appears correctly with 2 decimal places.

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

Task #3 Passing and Returning Objects

Create the CreditCard class according to the UML diagram. It should have data fields that include an owner of type Person, a balance of type Money, and a creditLimit of type Money.

It should have a constructor that has two parameters, a reference to a Person object to initialize the owner and a reference to a Money object to initialize the creditLimit. The balance can be initialized to a Money object with a value of zero. Remember you are passing in objects (passed by reference), so you are passing the memory address of an object. If you want your CreditCard to have its own creditLimit and balance, you should create a new object of each using the copy constructor in the Money class.

It should have accessor methods to get the balance and the creditLimit. Since these are Money objects (passed by reference), we don’t want to create a security issue by passing out addresses to components in our CreditCard class, so we must return a new object with the same values. Again, use the copy constructor to create a new object of type Money that can be returned.

It should have an accessor method to get the information about the owner, but in the form of a String that can be printed out. This can be done by calling the toString method for the owner (an instance of the Person class).

It should have a method that will charge to the CreditCard by adding the amount passed in the parameter to the balance, but only if it will not exceed the creditLimit. If the creditLimit will be exceeded, the amount should not be added, and an error message can be printed to the console.

It should have a method that will make a payment on the CreditCard by subtracting the amount passed in the parameter from the balance.

Compile, debug, and test it out completely by running the CreditCardDemo program.

You should get the output:




Code Listing 8.1 (Address.java)

*/

public Address(String road, String town, String st,
String zipCode)

The toString method
@return Information about the address.
*/
public String toString()
{
return (street + ", " + city +
", " + state + " " + zip);
}
}

Code Listing 8.2 (Person.java)

*/

*/

}

*/

} }


Code Listing 8.3 (Money.java)

*/

else {

} }

*/

return sum; }

*/

*/

int value;

}

Code Listing 8.4 (MoneyDemo.java)

*/

} else {

}

}
else
{

Code Listing 8.5 (CreditCardDemo.java)

*/

Explanation / Answer

class Address { // The street number and name private String street; // The city in which the address is located private String city; // The state in which the address is located private String state; // The zip code associated with the city and street private String zip; /** Constructor @param road Describes the street number and name. @param town Describes the city. @param st Describes the state. @param zipCode Describes the zip code. */ public Address(String road, String town, String st, String zipCode) { street = road; city = town; state = st; zip = zipCode; } /** The toString method @return Information about the address. */ public String toString() { return (street + ", " + city + ", " + state + " " + zip); } } /** This class defines a person by name and address. */ class Person { // The person's last name private String lastName; // The person's first name private String firstName; // The person's address private Address home; /** Constructor @param last The person's last name. @param first The person's first name. @param residence The person's address. */ public Person(String last, String first, Address residence) { lastName = last; firstName = first; home = residence; } /** The toString method @return Information about the person. */ public String toString() { return(firstName + " " + lastName + ", " + home.toString()); } } /** This class represents nonnegative amounts of money. */ 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 < 0) { System.out.println("Error: Negative amounts " + "of money are not allowed."); System.exit(0); } else { long allCents = Math.round(amount * 100); dollars = allCents / 100; cents = allCents % 100; } } // ADD LINES FOR TASK #1 HERE // Document and write a copy constructor public Money(Money money){ this.dollars = money.dollars; this.cents = money.cents; } /** 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
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