import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class Accumulate extends Applet implements ActionListener { private Button year; private TextField interestField, amountField, outcome; private Investment myMoney; public void init() { Label amountLabel = new Label("Enter amount:"); add(amountLabel); amountField = new TextField(8); add(amountField); amountField.addActionListener(this); Label rateLabel = new Label("Enter interest rate"); add(rateLabel); interestField = new TextField(4); add(interestField); interestField.addActionListener(this); year = new Button ("Another Year"); add(year); year.addActionListener(this); Label outcomeLabel = new Label ("Your money at the end of the year is"); add(outcomeLabel); outcome = new TextField(20); add(outcome); myMoney = new Investment(); } public void actionPerformed(ActionEvent event) { if (event.getSource() == amountField) { float amount = Integer.parseInt(amountField.getText()); myMoney.setInitialAmount(amount); } if (event.getSource() == interestField) { float rate = Float.parseFloat(interestField.getText()); myMoney.setRate(rate); } if (event.getSource() == year) { myMoney.anotherYear(); float newAmount = myMoney.getNewAmount(); int dollars = (int) newAmount; int cents = Math.round(100.0f * (newAmount - dollars)); outcome.setText(dollars + " dollars " + cents + " cents"); } } } class Investment { private float interestRate; private float oldAmount, newAmount; public void setInitialAmount(float amount) { oldAmount = amount; } public void setRate(float rate) { interestRate = rate; } public void anotherYear() { newAmount = oldAmount + (oldAmount * interestRate / 100.0f); oldAmount = newAmount; } public float getNewAmount() { return newAmount; } }