In the AccountTest class of Fig.7.9, method main contains six statements (lines 1112, 1314, 2627, 2829, 3839

Question:

In the AccountTest class of Fig.7.9, method main contains six statements (lines 11–12, 13–14, 26–27, 28–29, 38–39 and 40–41) that each display an Account object’s name and balance. Study these statements and you’ll notice that they differ only in the Account object being manipulated—account1 or account2. In this exercise, you’ll define a new displayAccount method that contains one copy of that output statement. The method’s parameter will be an Account object and the method will output the object’s name and balance. You’ll then replace the six duplicated statements in main with calls to displayAccount, passing as an argument the specific Account object to output.

Modify class AccountTest of Fig.7.9 to declare method displayAccount (Fig.7.18) after the closing right brace of main and before the closing right brace of class AccountTest. Replace the comment in the method’s body with a statement that displays accountToDisplay’s name and balance.

Fig. 7.18

1234 public static void display Account (Account accountToDisplay) { // place the statement that displays //

Recall that main is a static method, so it can be called without first creating an object of the class in which main is declared. We also declared method displayAccount as a static method.
When main needs to call another method in the same class without first creating an object of that class, the other method also must be declared static.
Once you’ve completed displayAccount’s declaration, modify main to replace the statements that display each Account’s name and balance with calls to displayAccount—each receiving as its argument the account1 or account2 object, as appropriate. Then, test the updated AccountTest class to ensure that it produces the same output as shown in Fig.7.9.

Fig.7.9J // Fig. 7.9: AccountTest.java 2 // Inputting and outputting floating-point numbers with Account objects. 3

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question

Java How To Program Late Objects Version

ISBN: 9780136123712

8th Edition

Authors: Paul Deitel, Deitel & Associates

Question Posted: