Sample BankAccount class
// Classes are factories for making virtual objects // // Classes have the following components: // * Constructor(s), for making objects // * Methods, for doing stuff // * Fields, for storing and using information public class BankAccount { // Four integer data types: // byte: 8 bits (each bit is a 0 or 1) // short: 16 bits // int: 32 bits (default) // long: 64 bits (often useful) // Two floating point data types: // float: 32 bits (don't think anyone uses this much) // double: 64 bits (default; "double" is short for "double-precision") // There are two identical terms used to describe the variable // balance: field and instance variable. The purpose of a field // is to store information in an object that persists after the // object is created. In this case, balance is declared to be a // double, meaning a 64-bit piece of data that handles fractions. // This makes sense since a bank account balance has dollars and // cents (and can have fractional cents as well). private double balance; // This is a constructor for the BankAccount class. Notice that // the data type being returned is BankAccount. Note also that // we have two variables called balance in the constructor. The // keyword this is used to distinguish between the field balance // and the formal parameter balance. Eclipse is nice in that if // you click on one of the balance variables, it will highlight all // other instances of the same variable. This is particularly useful // in seeing the effects of the keyword "this". // // Note that parameters are temporary and go away after a constructor // or method is completed. Fields persist. Thus, the input to the // constructor, if it is to persist, must be stored in a field. public BankAccount(double balance) { this.balance = balance; } // For deposit, withdraw, and printBalance, the code should mirror // real-world behavior. If you access your bank account at an ATM, // what does that mean? // // For deposit, you put money into your account, you get a receipt, // but you do not take money out of the account. Nothing is being // returned; hence, deposit is made to be type void. public void deposit(double amount) { balance += amount; // Same as commented line, below // balance = balance + amount; System.out.println("Your new balance is: " + balance); } // Withdraw actually does put genuine money into your hands, so it // will return that amount of money. Hence, withdraw is of type double. // If you try to withdraw more money than you have, you will get an // error message and no money is returned. If you do have enough // in your balance, you get that money and a receipt is printed. public double withdraw(double amount) { if (amount > balance) { System.out.println("Request denied!"); return 0; } else { balance -= amount; System.out.println("Your new balance is: " + balance); return amount; } } // What should printBalance return? No money, that's for sure. public void printBalance() { System.out.println("Your account contains: " + balance); } }