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)
*/