simple Interest
setup Form
btnCalculate
int principal = 1000; //invest $1000
double rate = 0.05; //at 5% per annum
int time = 2; //for 2 years
double final_amount;
final_amount = principal + simpleInterest(principal, rate, time);
lblResult.setText(String.valueOf(final_amount));
create Sub-procedure
- use sub-procedures / sub-methods to keep your programs modular. Put your sub-procedure for
simpleInterest
before your calculate event procedure btnCalculateActionPerformed
(or whatever you called it) as shown:
public double simpleInterest(int P, double R, int T){
double interest = P * R * T;
return interest;
}
challenge
- add a clear button, and add another sub-procedure, this time sub-procedure returns no value (nothing) which is what 'void' means:
public void clear(){
lblResult.setText("");
}
call the clear() sub-procedure from your new 'clear' button event handler