Lecture 10 Private Variables Let us start with some code for a class: String name; double balance; // end Account // end class Account The class we are building here will be a template for an account at a bank. There are two "class" variables, name and balance. The "constructor" for the class, which has the same name Account as the class is // end Account It has a parameter list Account(String n, double b) in the creation of an instance of this class. which will be used The code for the class is kept in a separate file Account.java. The next program, Bank.java, will eventually be a collection of accounts. Right now it has only one. The file Bank.java is kept in the same directory as "Account": Account a = new Account("Ronald", 3000); System.out.println("\n\nbalance = "+ a.balance); //end main() // end class Bank In this application we are constructing an account for "Ronald", with an initial deposit of $3000. When the application is run its' output is: 1
In reviewing this program one sees that the variable balance can be accessed by the program Bank. As a matter of fact it can be accesed by any number of programs. The access can be restricted by declaring the two class variables in Account to be private: private String name; private double balance; // end Account // end class Account When Bank.java is compiled (after the above change has been made and saved to the Account.java file) one gets a "variable balance.. not accessible" error message: It is not accessible because it has been declared to be private. The variable balance can only be manipulated within the class Account". So, how does the bank find out what is in each account? This is done by adding a toprint() method to the class Account: private String name; private double balance; 2
public String toprint() String output; output = name +", balance = " + balance; return output; Then, Bank is modified by a call to the a.toprint() method, which prints out the name and the balance : Account a = new Account("Ronald", 3000); //end main() // end class Bank The output is: Deposits, Withdrawals Next, we add two methods to to permit the bank to make deposits and withdrawls; private String name; private double balance; public String toprint() String output; 3
output = name +", balance = " + balance; return output; public double deposit(double d) if ( d >= 0) balance = balance + d; if (d < 0) System.out.println("Your deposit has to be >= 0"); return balance; public double withdrawl(double w) if (w >= 0 && w <= balance) balance = balance -w; if (w > balance) System.out.println("You don't have sufficient funds!"); if (w < 0) System.out.println("Withdrawl must be >= 0"); return balance; // end Account We modify Bank to test deposit() and withdrawl() : Account a = new Account("Ronald", 3000); System.out.println("Started with $3000"); System.out.println("\nThen deposited $450"); a.deposit(450); System.out.println("\nThen tried to deposit -200"); a.deposit(-200); System.out.println("\nWithdrew $650"); a.withdrawl(650); System.out.println("\nTried to get $4000"); a.withdrawl(4000); //end main() // end class Bank The output is: 4
5
Overloading a Constructor Suppose the bank wishes to include the social security number of all new accounts. This can be done by "overloading" the constructor for the class, as folows. private String name; private String socialsecurity = ""; private double balance; public Account(String n, String s, double b) socialsecurity = s; public String toprint() public double deposit(double d) public double withdrawl(double w) public String printss() String ssnumber ; if (socialsecurity.equals("")) ssnumber = "no social security number for this account"; else ssnumber = socialsecurity; return ssnumber; // end account This class has a new class variable private String socialsecurity = "";, 6
initialized as the null string, and two different constructors for Account, the previously defined one Account(String n, double b), and a newly defined one Account(String n, String s, double b). A class can have any number of constructors. Each constructor has to have the name of the class, Account in this case. Constructors with different parameter lists are considered to be distinct constructors. The text of the methods that were defined earlier toprint(), deposit(), and withdrawl() was deleted above for space and readability. It has to be replaced when the program is run. The next version of the class Bank shows the two different ways an account can now be constructed. The first account a is constructed without a social security number; the second, b, with one. The method printss() is used to print out this number, provided it has been entered. Account a = new Account("Ronald", 3000); Account b = new Account("David","123-45-678", 4000); System.out.println("Ronald Started with $3000"); System.out.println(a.printSS()); System.out.println("\nDavid Started with $4000"); System.out.println(b.toPrint()); System.out.print("His social security number is "); System.out.println(b.printSS()); //end main() // end class Bank The output is: 7
An Array of Accounts We shall show the syntax for building a bank with 3 accounts. We will be assuming that all accounts do have a social security number so, we will add another print method, toprint2(), to the class Account to simplify the output. It is: public String toprint2() String output = "name: "+name + "\n"; output = output + "Social Security Number: "+socialsecurity; output = output +"\nbalance: " + balance; return output; Once this method has been added to Account we then define Bank as an array of Accounts: Account[] b ; b = new Account[3]; b[0] = new Account("Edward", "234-56-789", 300); System.out.println(b[0].toPrint2()+ " \n"); b[1] = new Account("Amy", "34-567-891", 50000); System.out.println(b[1].toPrint2()+ "\n"); b[2] = new Account("Charlie", "45-678-912",897.45); System.out.println(b[2].toPrint2()); The output is: 8