Quote
My program compiles and executes. However, when I press the calculate button, it does not give the result and it erases the principal value, leaving a square symbol of some type. Please help, it is due today, and I haven't been able to find the problem. I have attached the .htm code for the applet. Thanks
* Purpose: Write the program in Java (with a graphical user interface)
* and have it calculate and display the mortgage payment amount
* from user input of the amount of the mortgage, the term of the
* mortgage, and the interest rate of the mortgage. Allow the user
* to loop back and enter new data or quit. Please insert comments
* in the program to document the program.
*/
//packages imported for class and method use
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
public class JennProject extends JApplet implements ActionListener{
TextField principalText, paymentText, termText, interestText;
Button calculate; //calculation button
double beginamount; // original principle
double intRate; // interest rate
double years; // length of loan in years
final int numpayments = 12; //payments per year
NumberFormat format;
public void init() {
// Use a grid bag layout.
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(layout);
Label heading = new Label("Mortgage Calculator");
Label principalLabel = new Label("Principal");
Label termLabel = new Label("Years");
Label interestLabel = new Label("Interest Rate");
Label paymentLabel = new Label("Monthly Payments");
//Set field lengths
principalText = new TextField(10);
termText = new TextField(10);
paymentText = new TextField(10);
interestText = new TextField(10);
//Display payment field/not editable
paymentText.setEditable(false);
calculate = new Button("Calculate");
// Define the grid bag
constraints.weighty = 1.0;//Distribute space of 1.0
constraints.gridwidth = GridBagConstraints.REMAINDER;//last component
constraints.anchor = GridBagConstraints.NORTH;//top of display/centered horizontally
layout.setConstraints(heading, constraints);
// Anchor most components to the right
constraints.anchor = GridBagConstraints.EAST;
//setting constraints
constraints.gridwidth = GridBagConstraints.RELATIVE;//place next to last component
layout.setConstraints(principalLabel, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(principalText, constraints);
constraints.gridwidth = GridBagConstraints.RELATIVE;//place next to last component
layout.setConstraints(termLabel, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(termText, constraints);
constraints.gridwidth = GridBagConstraints.RELATIVE;//place next to last component
layout.setConstraints(interestLabel, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(interestText, constraints);
constraints.gridwidth = GridBagConstraints.RELATIVE;//place next to last component
layout.setConstraints(paymentLabel, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(paymentText, constraints);
constraints.anchor = GridBagConstraints.CENTER;
layout.setConstraints(calculate, constraints);
// Add components
add(heading);
add(principalLabel);
add(principalText);
add(termLabel);
add(termText);
add(interestLabel);
add(interestText);
add(paymentLabel);
add(paymentText);
add(calculate);
// Receive action events
principalText.addActionListener(this);
termText.addActionListener(this);
interestText.addActionListener(this);
calculate.addActionListener(this);
// Number formatting
format = NumberFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
}
//User presses calculate
public void actionPerformed(ActionEvent ae) {
repaint();
}
// Displays result of calculations
public void paint(Graphics g) {
double result = 0.0;
String principalString = principalText.getText();
String termString = termText.getText();
String interestString = interestText.getText();
try {
if (principalString.length() != 0 && termString.length() != 0
&& interestString.length() != 0) {
beginamount = Double.parseDouble(principalString);
years = Double.parseDouble(termString);
intRate = Double.parseDouble(interestString) / 100;
result = calculate();
principalText.setText(format.format(result));
}
showStatus(""); // erase any error messages displayed
} catch (NumberFormatException exc) {
showStatus("Invalid Input");
paymentText.setText("");
}
}
// Calculate the loan payment
double calculate() {
double first;
double second;
double x, z;
first = intRate * beginamount / numpayments;
x = -(numpayments * years);
z = (intRate / numpayments) + 1.0;
second = 1.0 - Math.pow(x, z);
return first / second;
}
}
Attached File(s)
-
JennProject.htm (248bytes)
Number of downloads: 56

New Topic/Question
Reply




MultiQuote




|