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. It is called a
// "default constructor" because it has no input. Not every class
// needs a default constructor, but if you are going to use one, it
// makes sense to consider what it should do. In this case, it
// probably makes sense for getting an account into a bank's database,
// but with no initial money. So...
public BankAccount() {
balance = 0;
}
// You can have more than one constructor in a class. Here is one
// where the initial balance is known and the account will have money.
// We have two variables called balance in this 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;
}
// 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 void withdraw(double amount) {
if (amount > balance) {
System.out.println("Insufficient funds!");
} else {
balance -= amount;
}
}
// It might be nice to see how much money you have left when you
// go to an ATM. getBalance() returns the amount.
public double getBalance() {
return balance;
}
}