Java Reference
In-Depth Information
4
5
public class
AccountTest
6
{
7
public static void
main(String[] args)
8
{
9
Account account1 =
new
Account(
"Jane Green"
,
50.00
);
Account account2 =
new
Account(
"John Blue"
,
-7.53
);
10
11
12
// display initial balance of each object
13
System.out.printf(
"%s balance: $
%.2f
account1.getBalance()
%.2f
account2.getBalance()
%n"
,
14
account1.getName(),
);
15
System.out.printf(
"%s balance: $
%n%n"
,
16
account2.getName(),
);
17
18
// create a Scanner to obtain input from the command window
19
Scanner input =
new
Scanner(System.in);
20
21
System.out.print(
"Enter deposit amount for account1: "
);
// prompt
22
double
depositAmount = input.nextDouble();
// obtain user input
%.2f
23
System.out.printf(
"%nadding
to account1 balance%n%n"
,
24
depositAmount);
25
account1.deposit(depositAmount);
// add to account1's balance
26
27
// display balances
28
System.out.printf(
"%s balance: $
%.2f
account1.getBalance()
%.2f
account2.getBalance()
%n"
,
29
account1.getName(),
);
30
System.out.printf(
"%s balance: $
%n%n"
,
31
account2.getName(),
);
32
33
System.out.print(
"Enter deposit amount for account2: "
);
// prompt
34
depositAmount = input.nextDouble();
// obtain user input
%.2f
35
System.out.printf(
"%nadding
to account2 balance%n%n"
,
36
depositAmount);
37
account2.deposit(depositAmount);
// add to account2 balance
38
39
// display balances
40
System.out.printf(
"%s balance: $
%.2f
account1.getBalance()
%.2f
account2.getBalance()
%n"
,
41
account1.getName(),
);
42
System.out.printf(
"%s balance: $
%n%n"
,
43
account2.getName(),
);
44
}
// end main
45
}
// end class AccountTest
Jane Green balance: $50.00
John Blue balance: $0.00
Enter deposit amount for account1:
25.53
adding 25.53 to account1 balance
Jane Green balance: $75.53
John Blue balance: $0.00
Fig. 3.9
|
Inputting and outputting floating-point numbers with
Account
objects. (Part 2 of 3.)