CMPU-181, Spring 2013 Interactions like those seen in class March 6, 2013 Welcome to DrJava. Working directory is /Users/hunsberg/Desktop > // The BankAcct class that we defined in class (!) contains four different kinds > // of information: (1) class-oriented data (i.e., "fields") > // (2) class-oriented functions (i.e., "methods") > // (3) object-oriented data (template for creating objects) > // (4) object-oriented methods (functions that an object can be asked to "run"). > // > // Example of class-oriented data: BankName. > BankAcct.BankName "CMPU-181 Bank of Vassar" > // Example of class-oriented function/method: cofunc. > BankAcct.cofunc() Hello from our bank! > // Note that class-oriented data and functions/methods can be used even if no BankAcct > // objects have been created. That is because they are independent of any objects. > // Below, we will create some BankAcct objects. Each BankAcct object has three fields, > // called: BALANCE, OWNER and PASSWORD. The NEW keyword is used to create new objects. > new BankAcct() Not doing any initialization! BankAcct@71257687 > // Notice that the above expression created a new BankAcct object and then automatically > // called the BankAcct "constructor" function that takes no inputs. That constructor > // function caused the "Not doing any initialization!" string to be displayed. > // Below, we cause a different "constructor" to be called, one that takes some input > // values that initialize the contents of the new BankAcct object. To enable us to > // view the contents of the object after we create it, we'll store the new object in > // a variable called myAcct. > BankAcct myAcct = new BankAcct("luke", "pword", 1000) > myAcct.owner "luke" > myAcct.balance Static Error: No field in BankAcct has name 'balance' > // The above error was caused by the fact that the BALANCE field is declared to > // be PRIVATE. That is, it is not available for direct inspection. Similar remarks > // apply to the PASSWORD field. > myAcct.password Static Error: No field in BankAcct has name 'password' > // Using PRIVATE fields enables the BankAcct class to control access to certain > // information. However, if a user knows the password, the getBalance method > // can be used to access the balance of an account. > myAcct.getBalance("pword") 1000.0 > // But if we don't give the right password: > myAcct.getBalance("wrongWord") Hey! Password incorrect! 0.0 > // Similar remarks apply to making a withdrawal: > myAcct.makeWithdrawal("pword", 25) Withdrawal authorized! New balance = 975.0 25.0 > myAcct.makeWithdrawal("wrongWord!", 25) Withdrawal not authorized! 0.0 > // Also, we can't withdraw a negative amount, or an amount that would cause our > // balance to go negative. > myAcct.makeWithdrawal("pword", 20303030) Withdrawal not authorized! 0.0 > myAcct.makeWithdrawal("pword", -35) Withdrawal not authorized! 0.0 >