Question: Java 3 . 1 5 ( Removing Duplicated Code in Method main ) In the AccountTest class of Fig . 3 . 9 , method

Java 3.15(Removing Duplicated Code in Method main) In the AccountTest class of Fig.3.9, method main contains six statements (lines 1112,1314,2627,2829,3839 and 4041) that each display an Account objects name and balance. Study these statements and youll notice that they differ only in the Account object being manipulatedaccount1 or account2. In this exercise, youll define a new displayAccount method that contains one copy of that output statement. The methods parameter will be an Account object and the method will output the objects name and balance. Youll 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.3.9 to declare method displayAccount (Fig.3.20) after the closing right brace of main and before the closing right brace of class AccountTest. Replace the comment in the methods body with a statement that displays accountToDisplays name and balance. 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 youve completed displayAccounts declaration, modify main to replace the statements that display each Accounts name and balance with calls to displayAccounteach 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.3.9.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!