Find Us On:

# More Classes and Objects

phillyluvr

Lab Assignment This lab assignment will ask you to write a program using a copy constructor and equals and toString methods. Task #1 Writing a Copy Constructor 1. Create a New Project named MoneyDemo . 2. Create a new class called Money and cut and paste the Money.java file from Blackboard into the class. Cut and paste the MoneyDemo.java file from Blackboard into the MoneyDemo class. 3. 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 1. 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. 2. 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. 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 Attachments Tags Clarifications

## Solution of MoneyDemo

mariobeka

Below you'll find the solution for Lab8. Attachments
Money.java (3 K) Preview
otherwise. */ public boolean equals(Money money) { return this.dollars == money.dollars && this.cents == money.cents; } /** * Prints out the string representation of the Money object. * If the cents are less then 10 then a 0 is added before the printing the cents * to ensure 2 decimal

MoneyDemo.java (2 K) Preview
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 {

Screen Shot 2015-10-28 at 19.07.19.png (40 K) Preview

Screenshots