1. Complete the BankAccount class: do setName,setBalance, withdraw(any amount).
Note that withdraw should not allow the balance to go negative.
Use pop-up dialogs to input any numbers and display results. Info via Tools|Code templates in Japa.
(Or, use the available textfields
and labels to test the parts you add. (Don't worry about the quality of the GUI !)
2. add an overloaded withdraw method, which always withdraws £50.
The balance should not go negative.
(Overloading:
we can have several methods with the same name in a class. They must have differing parameter types.
Java looks at the calling parameters, and choses the right method).
3. Add a constructor so we can create an account with a name, any balance we wish, as in
b= new BankAccount("john", 50);Solution: a constructor method has the same name as the class it is in. it has no return type. Normally, the passed values are assigned to instance variables. Here is a BankAccount constructor:
public BankAccount(String anyName, int anyBallance){ balance=anyBalance; name=anyName; }
4. This involves the topic of inheritance, which we will cover in a lecture. Create a new type of account, similar to a BankAccount, named GoldAccount. It should have an 'overdraft' variable, initially set to e.g 200. It is ok to withdraw from a GoldAccount if we do not go below the overdraft limit. The constructor of a GoldAccount should have the same parameters as a BankAccount, and the rule is that you can't 'build on sand'. When constructing a GoldAccount, you must call up the constructor of the class you are building on. This is always named 'super'. Thus, the constructor of the GoldAccount is:
public GoldAccount(...etc) { super (...etc);// pass details to BankAccount constructor }
How is the original BankAccount class affected?
Explain which method in which class gets invoked in:
GoldAccount gold; ... gold = new GoldAccount("June",40); ... goldAcc.withdraw(300); goldAcc.deposit(234);