A sample interest calculator, very basic in functionality, coded in Java and ready to be compiled and executed as a Java Applet.
1.import java.awt.*;
2.import java.awt.event.*;
3.import java.applet.*;
4.import java.text.NumberFormat;
5.
6.public class InterestCalculator extends Applet implements ActionListener
7.{
8. TextField textPresentVal = new TextField("0", 10);
9. TextField textInterestRate = new TextField("0", 10);
10. TextField textPeriods = new TextField("0", 10);
11. Label lblPresentVal = new Label("Present Value");
12. Label lblInterestRate = new Label("Interest Rate");
13. Label lblPeriods = new Label("Number of Periods");
14. Label lblFutureVal = new Label("Future Value:");
15. Button btnOk = new Button("Calculate");
16.
17. public void init()
18. {
19.
20. add(lblPresentVal);
21. add(textPresentVal);
22. add(lblInterestRate);
23. add(textInterestRate);
24. add(lblPeriods);
25. add(textPeriods);
26. add(lblFutureVal);
27. add(btnOk);
28.
29. btnOk.addActionListener(this);
30. }
31.
32. public void actionPerformed(ActionEvent evt)
33. {
34. if (evt.getSource() == btnOk)
35. {
36. int PresentVal = Integer.parseInt(textPresentVal.getText());
37. int InterestRate = Integer.parseInt(textInterestRate.getText());
38. int Periods = Integer.parseInt(textPeriods.getText());
39. double FutureVal = PresentVal * (Math.pow((1 + InterestRate), Periods));
40. NumberFormat nf = NumberFormat.getCurrencyInstance();
41. lblTotal.setText("Future Value: " + nf.format(FutureVal));
42.
43. repaint();
44. }
45. }
46.}