Presentation is loading. Please wait.

Presentation is loading. Please wait.

Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.

Similar presentations


Presentation on theme: "Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that."— Presentation transcript:

1 Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that the variable harrysChecking contains a reference to an object of type BankAccount  Therefore we should support method calls such as the following: harrysChecking.deposit(2000); harrysChecking.withdraw(500); System.out.println(harrysChecking.getBalance());  Deposit and withdraw are mutators while getBalance is an accessor method Specifying the Public Interface of a Class: Methods 1

2 Commenting the Public Interface 2 When implementing classes and methods, you should get used to comment their behaviors. Java has a useful standard form for documentation comments. ‒ If you use it in your classes, a program called javadoc can automatically generate a set of HTML pages that describe them  Comment is placed before the class or method.  It starts with a /** which is a special delimiter and ends with */  Then provide method/class description or purpose  For each method parameter you supply a line starts with @param followed by the parameter name and description ‒ You omit it for methods that have no parameters  For each method you supply a line starts with @return followed by return value description ‒ You omit it for methods whose return type is void Provide documentation comments for every class, every method, every parameter every return value. /** Withdraws money from the bank account. @param the amount to withdraw */ public void withdraw(double amount) { //implementation filled in later }

3 /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount {... /** Withdraws money from the bank account. @param the amount to withdraw */ public void withdraw(double amount) { //implementation filled in later } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { //implementation filled in later } } Comment Example 3

4 Javadoc Method Summary 4

5 Javadoc Method Detail 5

6 /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount{ private int accountNumber; private double balance; // private static int startingAccount=10000; /** Constructs a bank account with a zero balance @param anAccountNumber the account number for this account */ public BankAccount(int anAccountNumber) { accountNumber = anAccountNumber; balance = 0; } // public BankAccount() { // accountNumber = startingAccount++; // balance = 0; // } BankAccount.java 6

7 /** Constructs a bank account with a given balance @param anAccountNumber the account number for this account @param initialBalance the initial balance */ public BankAccount(int anAccountNumber, double initialBalance) { accountNumber = anAccountNumber; balance = initialBalance; } /** Constructs a bank account with a given balance @param initialBalance the initial balance */ // public BankAccount(double initialBalance) { // accountNumber = startingAccount++; // balance = initialBalance; // } BankAccount.java 7

8 /** Gets the account number of this bank account. @return the account number */ public int getAccountNumber() { return accountNumber; } /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; } /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount){ double newBalance = balance - amount; balance = newBalance; } BankAccount.java (cont.) 8

9 /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } } BankAccount.java (cont.) 9

10 /** This bank contains a collection of bank accounts. */ public class Bank{ public static final int SIZE = 1000; public int accountCounter = 0; //why not static? public int bankCounter = 0; private BankAccount[] accounts; /** Constructs a bank with no bank accounts. */ public Bank(){ accounts = new BankAccount[SIZE]; bankCounter++; } /** Constructs a bank with no bank accounts. @param size the number of possible accounts */ public Bank(int size){ accounts = new BankAccount[size]; bankCounter++; } Bank.java 10

11 /** Adds an account to this bank. @param a the account to add */ public void addAccount(BankAccount a) { accounts[accountCounter++]=a; } /** Gets the sum of the balances of all accounts in this bank. @return the sum of the balances */ public double getTotalBalance() { double total = 0; for (int i=0; i< accountCounter;i++) { total = total + accounts[i].getBalance(); } return total; } Bank.java (cont.) 11

12 /** Counts the number of bank accounts whose balance is at least a given value. @param atLeast the balance required to count an account @return the number of accounts having least the given balance */ public int count(double atLeast) { int matches = 0; for (int i=0; i<accountCounter;i++) { if (accounts[i].getBalance() >= atLeast) matches++; // Found a match } return matches; } Bank.java (cont.) 12 /** Finds a bank account with a given number. @param accountNumber the number to find @return the account with the given number, or null if there is no such account */ public BankAccount find(int accountNumber){ for (int i=0; i< accountCounter;i++) { if (accounts[i].getAccountNumber() == accountNumber) // Found a match return accounts[i]; } return null; // No match in the entire array list }

13 /** Gets the bank account with the largest balance. @return the account with the largest balance, or null if the bank has no accounts */ public BankAccount getMaximum() { if (accountCounter == 0) return null; BankAccount largestYet = accounts[0]; for (int i = 1; i < accountCounter; i++){ BankAccount a = accounts[i]; if (a.getBalance() > largestYet.getBalance()) largestYet = a; } return largestYet; } Bank.java (cont.) 13

14 /** This program tests the Bank class. */ public class BankTester { public static void main(String[] args) { Bank firstBankOfJava = new Bank(10); firstBankOfJava.addAccount(new BankAccount(1001, 20000)); firstBankOfJava.addAccount(new BankAccount(1015, 10000)); firstBankOfJava.addAccount(new BankAccount(1729, 15000)); double threshold = 15000; int c = firstBankOfJava.count(threshold); System.out.println("Count: " + c); System.out.println("Expected: 2"); int accountNumber = 1015; BankAccount a = firstBankOfJava.find(accountNumber); if (a == null) System.out.println("No matching account"); else System.out.println("Balance of matching account: " + a.getBalance()); System.out.println("Expected: 10000"); BankAccount max = firstBankOfJava.getMaximum(); System.out.println("Account with largest balance: " + max.getAccountNumber()); System.out.println("Expected: 1001"); } BankTester.java 14


Download ppt "Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that."

Similar presentations


Ads by Google