Java Reference
In-Depth Information
To compute the minimum of a data set, keep a candidate for the minimum and
replace it whenever you encounter a smaller value. At the end of the array list, you
have found the minimum.
The following sample program implements a Bank class that stores an array list of
bank accounts. The methods of the Bank class use the algorithms that we have
discussed in this section.
ch07/bank/Bank.java
1 import java.util.ArrayList;
2
3 /**
4 This bank contains a collection of bank
accounts.
5 */
6 public class Bank
7 {
8 /**
9 Constructs a bank with no bank accounts.
10 */
11 public Bank()
12 {
13 accounts = new ArrayList<BankAccount>();
14 }
15
16 /**
17 Adds an account to this bank.
18 @param a the account to add
19 */
20 public void addAccount(BankAccount a)
21 {
22 accounts.add(a);
23 }
24
25 /**
26 Gets the sum of the balances of all
accounts in this bank.
27 @return the sum of the balances
28 */
29 public double getTotalBalance()
30 {
31 double total = 0 ;
303
304
Search WWH ::




Custom Search