Question: Java 3 . 1 5 ( Removing Duplicated Code in Method main ) In the AccountTest class of Fig . 3 . 9 , method
Java Removing Duplicated Code in Method main In the AccountTest class of Fig method main contains six statements lines and that each display an Account objects name and balance. Study these statements and youll notice that they differ only in the Account object being manipulatedaccount or account 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 to declare method displayAccount Fig 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 account or account object, as appropriate. Then, test the updated AccountTest class to ensure that it produces the same output as shown in Fig
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
