Calculation Problem

Mortgage Calculator - GUI Based

Page 1 of 1

2 Replies - 1174 Views - Last Post: 04 June 2007 - 10:28 AM Rate Topic: -----

#1 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Calculation Problem

Post icon  Posted 04 June 2007 - 09:03 AM

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)



Is This A Good Question/Topic? 0
  • +

Replies To: Calculation Problem

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Calculation Problem

Posted 04 June 2007 - 09:30 AM

I expect the problem is that your call to Math.pow() is returning something undefined. Have you checked the return value of that call? What are you expecting to see there (if you were to use an example for the the entire calculate function...run through the numbers)
Was This Post Helpful? 0
  • +
  • -

#3 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Re: Calculation Problem

Posted 04 June 2007 - 10:28 AM

View PostAmadeus, on 4 Jun, 2007 - 09:30 AM, said:

I expect the problem is that your call to Math.pow() is returning something undefined. Have you checked the return value of that call? What are you expecting to see there (if you were to use an example for the the entire calculate function...run through the numbers)

I wasn't exactly sure, but your comments made me take a look at the calculations again, and I realized I had variables in the wrong order, and one variable was incorrectly named. Thanks again for the help you provided.[quote]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1