Question: Fig. 3 . 9 / / Fig. 3 . 9 : AccountTest.java / / Inputting and outputting floating - point numbers with Account objects. 3

Fig. 3.9
// Fig. 3.9: AccountTest.java
// Inputting and outputting floating-point numbers with Account
objects.
3 import java.util.Scanner;
4
5 public class AccountTest {
public static void main(String[] args){
Account account1= new Account("Jane Green", 50.00);
Account account2= new Account("John Blue", -7.53);
// display initial balance of each object
System.out.printf("%s balance: $%.2f%n",
account1.getName(), account1.getBalance());
System.out.printf("%s balance: $%.2f%n%n",
account2.getName(), account2.getBalance());
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(
System.in);
System.out.print("Enter deposit amount for account1: "); //
prompt
20 double depositAmount = input.nextDouble(); // obtain user input
21 System.out.printf("%nadding %.2f to account1 balance%n%n",
22 depositAmount);
23 account1.deposit(depositAmount); // add to account1's balance // display balances
System.out.printf("%s balance: $%.2f%n",
account1.getName(), account1.getBalance());
System.out.printf("%s balance: $%.2f%n%n",
account2.getName(), account2.getBalance());
System.out.print("Enter deposit amount for account2: "); //
depositAmount = input.nextDouble(); // obtain user input
System.out.printf("%nadding %.2f to account2 balance%n%n",
depositAmount);
account2.deposit(depositAmount); // add to account2 balance
// display balances
System.out.printf("%s balance: $%.2f%n",
account1.getName(), account1.getBalance());
System.out.printf("%s balance: $%.2f%n%n",
account2.getName(), account2.getBalance());
}
}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.
Fig. 3 . 9 / / Fig. 3 . 9 : AccountTest.java / /

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!