Gracias. he logrado resolverlo como uds indicaban. Ahora el programa está terminado y se ve así:
Código Java:
Ver originalimport java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.JTextField;
private Button year, ingresar
; private TextField interestField, amountField, outcome
; private Investment myMoney;
public void init() {
add(amountLabel);
add(amountField);
amountField.addActionListener(this);
Label rateLabel
= new Label("Enter interest rate"); add(rateLabel);
add(interestField);
interestField.addActionListener(this);
year
= new Button ("Calcular"); add(year);
year.addActionListener(this);
Label outcomeLabel
= new Label("Your money at the end of the year is"); add(outcomeLabel);
add(outcome);
myMoney = new Investment();
}
if(event.getSource().equals(amountField)){
float amount
= Integer.
parseInt(amountField.
getText()); myMoney.setInitialAmount(amount);
}
else{
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;
}
}