B
ButtonNovice
Hello,
I am having an issue getting the "Compute", "Reset", and "Close Window"
buttons to work in this calulator. The code compiles without issue in
TextPad. I had it working (calculating and displaying in the text
area), but I was getting a "uses or overrides a deprecated API" error
when compiling. So I changed, public boolean action(Event evt, Object
arg) to public boolean AWTEvent(Event evt, Object arg) in order to fix
it. Once I changed from action to AWT Event it stopped calulating/
displaying in the text box. I am using SDK 5.0.
Any ideas?
Thanks in advance!
import java.applet.Applet;
import java.awt.*;
public class test extends Applet
{ TextField balField;
TextField intField;
TextField nyrField;
Button Calc;
Button Reset;
Button Close;
TextArea msgArea;
// Convert double to dollars and cents
static String format(double dollars)
{ String numString = Double.toString(dollars);
int dotpos = numString.indexOf('.');
if (dotpos < 0) // Check if whole number
return numString;
// Check for excess fraction digits
else if (dotpos < numString.length() - 2)
return numString.substring(0, dotpos + 3); // `.'+ 2
digits
else return numString + "0"; // Assume only 1 fraction
digit
}
public void init()
{ balField = new TextField("", 15);
intField = new TextField("", 5);
nyrField = new TextField("", 5);
Calc = new Button("Compute");
Reset = new Button("Reset");
Close = new Button("Close Window");
msgArea = new TextArea("", 25, 100);
msgArea.setEditable(false);
add(new Label("Enter principal"));
add(balField);
add(new Label("Enter annual interest rate"));
add(intField);
add(new Label("Enter number of years"));
add(nyrField);
add(Calc);
add(Reset);
add(Close);
add(msgArea);
}
public boolean AWTEvent(Event evt, Object arg)
{ if (evt.target == Calc)
{
this.update();
return true;
}
else if (evt.target == Reset)
{
balField.setText("");
intField.setText("");
nyrField.setText("");
msgArea.setText("");
return true;
}
else if (evt.target == Close)
{
System.exit(0);
return true;
}
else return false;
}
void update()
{ String balString = balField.getText();
String intString = intField.getText();
String nyrString = nyrField.getText();
if (balString.trim().length() == 0)
msgArea.setText("Principal amount missing");
else if (intString.trim().length() == 0)
msgArea.setText("Interest rate missing");
else if (nyrString.trim().length() == 0)
msgArea.setText("Number of years missing");
else {
double bal = new Double(balString).doubleValue();
double intyr = new Double(intString).doubleValue() /
100.;
short nyears = (short) new Integer(nyrString).intValue();
StringBuffer msg = new StringBuffer();
msg.append("\n\nprincipal=" + bal + " interest=" +
intyr + " years=" + nyears + "\n");
double intmo = intyr / 12.;
int npmts = nyears * 12;
double pmt = bal * (intmo / (1.- Math.pow(1.+
intmo,-npmts)));
msg.append("payment\ttotal\tinterest\tprincipal\tbalance\n");
msg.append("number\tpayment\tpayment\tpayment\n");
msg.append("\t\t\t\t" + bal + "\n");
for (short mo = 1; mo <= npmts; ++mo) {
double prinpmt, intpmt = bal * intmo;
if (mo < npmts)
prinpmt = pmt - intpmt;
else prinpmt = bal;
bal -= prinpmt;
msg.append(mo + "\t" + format(intpmt + prinpmt)
+ "\t" + format(intpmt)
+ "\t" + format(prinpmt)
+ "\t" + format(bal) + "\n\n\n");
}
msgArea.setText(msg.toString());
}
}
}
I am having an issue getting the "Compute", "Reset", and "Close Window"
buttons to work in this calulator. The code compiles without issue in
TextPad. I had it working (calculating and displaying in the text
area), but I was getting a "uses or overrides a deprecated API" error
when compiling. So I changed, public boolean action(Event evt, Object
arg) to public boolean AWTEvent(Event evt, Object arg) in order to fix
it. Once I changed from action to AWT Event it stopped calulating/
displaying in the text box. I am using SDK 5.0.
Any ideas?
Thanks in advance!
import java.applet.Applet;
import java.awt.*;
public class test extends Applet
{ TextField balField;
TextField intField;
TextField nyrField;
Button Calc;
Button Reset;
Button Close;
TextArea msgArea;
// Convert double to dollars and cents
static String format(double dollars)
{ String numString = Double.toString(dollars);
int dotpos = numString.indexOf('.');
if (dotpos < 0) // Check if whole number
return numString;
// Check for excess fraction digits
else if (dotpos < numString.length() - 2)
return numString.substring(0, dotpos + 3); // `.'+ 2
digits
else return numString + "0"; // Assume only 1 fraction
digit
}
public void init()
{ balField = new TextField("", 15);
intField = new TextField("", 5);
nyrField = new TextField("", 5);
Calc = new Button("Compute");
Reset = new Button("Reset");
Close = new Button("Close Window");
msgArea = new TextArea("", 25, 100);
msgArea.setEditable(false);
add(new Label("Enter principal"));
add(balField);
add(new Label("Enter annual interest rate"));
add(intField);
add(new Label("Enter number of years"));
add(nyrField);
add(Calc);
add(Reset);
add(Close);
add(msgArea);
}
public boolean AWTEvent(Event evt, Object arg)
{ if (evt.target == Calc)
{
this.update();
return true;
}
else if (evt.target == Reset)
{
balField.setText("");
intField.setText("");
nyrField.setText("");
msgArea.setText("");
return true;
}
else if (evt.target == Close)
{
System.exit(0);
return true;
}
else return false;
}
void update()
{ String balString = balField.getText();
String intString = intField.getText();
String nyrString = nyrField.getText();
if (balString.trim().length() == 0)
msgArea.setText("Principal amount missing");
else if (intString.trim().length() == 0)
msgArea.setText("Interest rate missing");
else if (nyrString.trim().length() == 0)
msgArea.setText("Number of years missing");
else {
double bal = new Double(balString).doubleValue();
double intyr = new Double(intString).doubleValue() /
100.;
short nyears = (short) new Integer(nyrString).intValue();
StringBuffer msg = new StringBuffer();
msg.append("\n\nprincipal=" + bal + " interest=" +
intyr + " years=" + nyears + "\n");
double intmo = intyr / 12.;
int npmts = nyears * 12;
double pmt = bal * (intmo / (1.- Math.pow(1.+
intmo,-npmts)));
msg.append("payment\ttotal\tinterest\tprincipal\tbalance\n");
msg.append("number\tpayment\tpayment\tpayment\n");
msg.append("\t\t\t\t" + bal + "\n");
for (short mo = 1; mo <= npmts; ++mo) {
double prinpmt, intpmt = bal * intmo;
if (mo < npmts)
prinpmt = pmt - intpmt;
else prinpmt = bal;
bal -= prinpmt;
msg.append(mo + "\t" + format(intpmt + prinpmt)
+ "\t" + format(intpmt)
+ "\t" + format(prinpmt)
+ "\t" + format(bal) + "\n\n\n");
}
msgArea.setText(msg.toString());
}
}
}