Math Error, no display

Mortgage Calculator CR#4

Page 1 of 1

13 Replies - 4536 Views - Last Post: 09 March 2009 - 01:32 PM Rate Topic: -----

#1 grabowpd   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Math Error, no display

Post icon  Posted 06 March 2009 - 09:15 AM

I can enter the amounts in my text fields, but I can not get them to calculate. Every time I try to put in the equation I get a compile error. I even tried to reduce the equation to just add the two numbers because I have found this same process to work on another program.

result = (num1 * num2);

//(1 - Math.pow(1/ (1 + num2), num3 * 12));

I used the second part of the equation as documentation statement but the original format should be:

result = (num1 * num2)/(1 - Math.pow(1/ (1 + num2), num3 * 12));

I get a compile error on the division sign and I just do not understand what I'm doing wrong.

Loan amount times Interest rate, number of years for amortizing. Display monthly payment in Monthly payment field.

Any help would be greatly appreciated.

Thank you,
Phil



/**

 * Mortgage Calculator- Complete Change Request #4 in Service Request SR-mf-003.

 * Insert comments in the program to document the program. Attach a design flow chart

 * to the source code of the program.

 *

 * Change Request #4: 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.

 * 

 * Author Phil Grabowski

 * University of Phoenix

 * PRG421 Java Programming II

 * Course Date 2/24/09  End Date 3/30/09

 * Instructor Yining Li

 */



import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;



public class MortgageCR41 extends JFrame implements ActionListener {



public MortgageCR41()

{

 Container content = this.getContentPane();

	content.setBackground(Color.white);

	content.setLayout(new FlowLayout()); 

	content.add(new JButton("Loan Amount"));

  content.add(new JTextField("					   "));

	content.add(new JButton("Interest Rate"));

  content.add(new JTextField("					   "));

	content.add(new JButton("Term in Years"));

  content.add(new JTextField("					   "));

  content.add(new JButton("Montly Payment"));

  content.add(new JTextField("					   "));

}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {											

		

	}  

									 



	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {										 

		System.exit(0);

	}				   

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {										 

		jTextField1.setText("");

		jTextField2.setText("");

		jTextField3.setText("");

	}									   



	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {										 

	 

	float num1, num2, num3, result;

  

  // num1 = Amount of loan

  // num2 = Interest of Loan

  // num3 = Term in years

  

	num1 = Float.parseFloat(jTextField1.getText());

	num2 = Float.parseFloat(jTextField2.getText());

	num3 = Float.parseFloat(jTextField3.getText());

	

  

	 

  result = (num1 * num2);

  //(1 - Math.pow(1/ (1 + num2), num3 * 12));

  

	jTextField3.setText(String.valueOf(result));



	}									   



   

	public static void main(String args[]) {

	 

// JFrame f = new JFrame("Mortgage Calculator CR#4");

		

			 MortgageCR41 f = new MortgageCR41();

			 

				f.setSize(300, 250);

				f.setVisible(true);

			}

	   

   

	// Variables declaration - do not modify					 

	private javax.swing.JButton jButton1;

	private javax.swing.JButton jButton2;

	private javax.swing.JButton jButton3;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel2;

	private javax.swing.JLabel jLabel3;

	private javax.swing.JPanel jPanel1;

	private javax.swing.JTextField jTextField1;

	private javax.swing.JTextField jTextField2;

	private javax.swing.JTextField jTextField3;

	// End of variables declaration				  



  JButton myButton = new JButton("Calculate");

   

   JCheckBox myCheckBox = new JCheckBox("Reset Fields");

	

	// Clear all fields in Jbuttons

 

   JTextArea myText = new JTextArea("Please enter the fields and click calculate");

 

   JPanel Panel = new JPanel();

 

   JPanel holdAll = new JPanel();

  {

	Panel.setLayout(new FlowLayout());

	Panel.add(myCheckBox);

	Panel.add(myButton);

 

	holdAll.setLayout(new BorderLayout());

	holdAll.add(Panel, BorderLayout.NORTH);

	holdAll.add(myText, BorderLayout.CENTER);

 

	getContentPane().add(holdAll, BorderLayout.CENTER);

 

	myButton.addActionListener(this);

	myCheckBox.addActionListener(this);

 

	setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  }

 

  public void actionPerformed(ActionEvent e)

  {

	if (e.getSource() == myButton)

	  myText.setText("Thank you");

 

	else if (e.getSource() == myCheckBox)

	  myText.setText("Fields Cleared" + 

					 myCheckBox.isSelected());

	else

	  myText.setText("E ...?");

  }

}




Is This A Good Question/Topic? 0
  • +

Replies To: Math Error, no display

#2 OrganizedChaos   User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: Math Error, no display

Posted 06 March 2009 - 09:22 AM

Have you tried declaring num1, num2, num3, and result as doubles instead of floats?

This post has been edited by OrganizedChaos: 06 March 2009 - 09:26 AM

Was This Post Helpful? 1
  • +
  • -

#3 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Math Error, no display

Posted 06 March 2009 - 10:25 AM

View PostOrganizedChaos, on 6 Mar, 2009 - 03:22 PM, said:

Have you tried declaring num1, num2, num3, and result as doubles instead of floats?

an alternative would be to cast the result of the expression using pow() which returns a double to a float, e.g.
 result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));


Was This Post Helpful? 1
  • +
  • -

#4 grabowpd   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Math Error, no display

Posted 06 March 2009 - 10:50 AM

View Posthorace, on 6 Mar, 2009 - 09:25 AM, said:

View PostOrganizedChaos, on 6 Mar, 2009 - 03:22 PM, said:

Have you tried declaring num1, num2, num3, and result as doubles instead of floats?

an alternative would be to cast the result of the expression using pow() which returns a double to a float, e.g.
 result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));




Ok great that compiles. When I run the program I am able to enter numbers but it does not display the result into the monthly payment when I click on calculate.

Thanks in advance for you help.
Was This Post Helpful? 0
  • +
  • -

#5 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Math Error, no display

Posted 06 March 2009 - 11:32 AM

View Postgrabowpd, on 6 Mar, 2009 - 04:50 PM, said:

View Posthorace, on 6 Mar, 2009 - 09:25 AM, said:

View PostOrganizedChaos, on 6 Mar, 2009 - 03:22 PM, said:

Have you tried declaring num1, num2, num3, and result as doubles instead of floats?

an alternative would be to cast the result of the expression using pow() which returns a double to a float, e.g.
 result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));




Ok great that compiles. When I run the program I am able to enter numbers but it does not display the result into the monthly payment when I click on calculate.

Thanks in advance for you help.

you had not setup jTextFiled1,jTextField2, etc and when you hit Calculate ActionPerformed is called
this is starting to work (something appears when you hit calculate) you can probably sort it out now
/**

 * Mortgage Calculator- Complete Change Request #4 in Service Request SR-mf-003.

 * Insert comments in the program to document the program. Attach a design flow chart

 * to the source code of the program.

 *

 * Change Request #4: 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.

 *

 * Author Phil Grabowski

 * University of Phoenix

 * PRG421 Java Programming II

 * Course Date 2/24/09  End Date 3/30/09

 * Instructor Yining Li

 */



import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;



public class MortgageCR41 extends JFrame implements ActionListener {



public MortgageCR41()

{

 Container content = this.getContentPane();

	content.setBackground(Color.white);

	content.setLayout(new FlowLayout());

	content.add(new JButton("Loan Amount"));

  content.add(jTextField1=new JTextField("					   "));  /// ** added jTextField1

	content.add(new JButton("Interest Rate"));

  content.add(jTextField2=new JTextField("					   "));

	content.add(new JButton("Term in Years"));

  content.add(jTextField3=new JTextField("					   "));

  content.add(new JButton("Montly Payment"));

  content.add(jTextField4=new JTextField("					   "));

}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {											

	   

	}  

									 



	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {										

		System.exit(0);

	}				  

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {										

		jTextField1.setText("");

		jTextField2.setText("");

		jTextField3.setText("");

	}									  



	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {										

	 

	float num1, num2, num3, result;

 

  // num1 = Amount of loan

  // num2 = Interest of Loan

  // num3 = Term in years

 

	num1 = Float.parseFloat(jTextField1.getText());

	num2 = Float.parseFloat(jTextField2.getText());

	num3 = Float.parseFloat(jTextField3.getText());

   

 

	 

  result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));

   System.out.println("ccc " + String.valueOf(result));

	jTextField3.setText(String.valueOf(result));



	}									  



   

	public static void main(String args[]) {

	 

// JFrame f = new JFrame("Mortgage Calculator CR#4");

	   

			 MortgageCR41 f = new MortgageCR41();

			 

				f.setSize(300, 250);

				f.setVisible(true);

			}

	   

   

	// Variables declaration - do not modify					

	private javax.swing.JButton jButton1;

	private javax.swing.JButton jButton2;

	private javax.swing.JButton jButton3;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel2;

	private javax.swing.JLabel jLabel3;

	private javax.swing.JPanel jPanel1;

	private javax.swing.JTextField jTextField1;

	private javax.swing.JTextField jTextField2;

	private javax.swing.JTextField jTextField3;

	// End of variables declaration				  
	private javax.swing.JTextField jTextField4;



  JButton myButton = new JButton("Calculate");

   

   JCheckBox myCheckBox = new JCheckBox("Reset Fields");

   

	// Clear all fields in Jbuttons

 

   JTextArea myText = new JTextArea("Please enter the fields and click calculate");

 

   JPanel Panel = new JPanel();

 

   JPanel holdAll = new JPanel();

  {

	Panel.setLayout(new FlowLayout());

	Panel.add(myCheckBox);

	Panel.add(myButton);

 

	holdAll.setLayout(new BorderLayout());

	holdAll.add(Panel, BorderLayout.NORTH);

	holdAll.add(myText, BorderLayout.CENTER);

 

	getContentPane().add(holdAll, BorderLayout.CENTER);

 

	myButton.addActionListener(this);

	myCheckBox.addActionListener(this);

 

	setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  }

 

  public void actionPerformed(ActionEvent e)

  {
	System.out.println("actionPerformed");
	 float num1, num2, num3, result;

 

  // num1 = Amount of loan

  // num2 = Interest of Loan

  // num3 = Term in years

 

	num1 = Float.parseFloat(jTextField1.getText());

	num2 = Float.parseFloat(jTextField2.getText());

	num3 = Float.parseFloat(jTextField3.getText());

   

 

	 

  result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));

   System.out.println("ccc " + String.valueOf(result));

	jTextField4.setText(String.valueOf(result));

  if (e.getSource() == myButton)

	  myText.setText("Thank you");

 

	else if (e.getSource() == myCheckBox)

	  myText.setText("Fields Cleared" +

					 myCheckBox.isSelected());

	else

	  myText.setText("E ...?");

  }

}


Was This Post Helpful? 2
  • +
  • -

#6 grabowpd   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Math Error, no display

Posted 06 March 2009 - 12:12 PM

Horace,

Thank you so much.

I'm adding this to my code as documentation. /* Horace </dream.in.code> Programming & web developement community. suggested
an alternative would be to cast the result of the expression using pow() which
returns a double to a float, e.g. */

Do you have a more proper name to use for citation?

I see the calculation is off so I need to work on the proper equation. Odd it is the same equation I used in previous classes I just changed the variables.

intrest=.0575;
ammount=200000;
term=30;
payment=(ammount*((intrest/12)/(1-Math.pow((1+(intrest/12)),-(term*12)))));

I quess my other question is reseting the fields because that does not seem to work properly.

Why is it when I hit calculate that all the formatting is lost?


I know this next part is minor, but inn the past I seen where a title bar would work with a jFrame. However when I use the constructor and try to add the Title into the title bar I receive compile errors.

// JFrame f = new JFrame("Mortgage Calculator CR#4");



MortgageCR41 f = new MortgageCR41();



f.setSize(300, 250);

f.setVisible(true);

This post has been edited by grabowpd: 06 March 2009 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#7 grabowpd   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Math Error, no display

Posted 06 March 2009 - 01:17 PM

View Postgrabowpd, on 6 Mar, 2009 - 11:12 AM, said:

Horace,

Thank you so much.

I'm adding this to my code as documentation. /* Horace </dream.in.code> Programming & web developement community. suggested
an alternative would be to cast the result of the expression using pow() which
returns a double to a float, e.g. */

Do you have a more proper name to use for citation?

I see the calculation is off so I need to work on the proper equation. Odd it is the same equation I used in previous classes I just changed the variables.

intrest=.0575;
ammount=200000;
term=30;
payment=(ammount*((intrest/12)/(1-Math.pow((1+(intrest/12)),-(term*12)))));

I quess my other question is reseting the fields because that does not seem to work properly.

Why is it when I hit calculate that all the formatting is lost?


I know this next part is minor, but inn the past I seen where a title bar would work with a jFrame. However when I use the constructor and try to add the Title into the title bar I receive compile errors.

// JFrame f = new JFrame("Mortgage Calculator CR#4");



MortgageCR41 f = new MortgageCR41();



f.setSize(300, 250);

f.setVisible(true);



It appears that I'm getting things in order. I am to display the gui enter data, but I get a wrong calculation. I think I need help with the formula.

Here are my notes... I have also attached the code.


//This is formula from a previous program that works properly for monthly payment.

intrest=.0575;
ammount=200000;
term=30;

payment=(ammount*((intrest/12)/(1-Math.pow((1+(intrest/12)),-(term*12)))));


//This formula below compiles but does not provide proper answer
// num1 = Amount of loan
// num2 = Interest of Loan
// num3 = Term in years


result=(num1*((num2/12)/1-(float)Math.pow((1+(num1/12)),-(num3*12))));




//Original formula that does not display proper calculation
result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));

This post has been edited by grabowpd: 06 March 2009 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Math Error, no display

Posted 06 March 2009 - 05:38 PM

Hmm... im not a professional nor have I been using JCreator for a long time, but I thought that JFrame had a setTitle method. Try this for adding in a title for the window

 this.setTitle("Mortgage Calculator CR#4");

This post has been edited by Fuzzyness: 06 March 2009 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

#9 grabowpd   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Math Error, no display

Posted 08 March 2009 - 11:07 AM

View PostFuzzyness, on 6 Mar, 2009 - 04:38 PM, said:

Hmm... im not a professional nor have I been using JCreator for a long time, but I thought that JFrame had a setTitle method. Try this for adding in a title for the window

 this.setTitle("Mortgage Calculator CR#4");



Nope that doesn't work, I just get compile errors on it.

MortgageCR41.java:81: illegal start of type
this.setTitle("Mortgage Calculator CR#4");
^
MortgageCR41.java:81: ';' expected
Was This Post Helpful? 0
  • +
  • -

#10 grabowpd   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Math Error, no display

Posted 08 March 2009 - 11:47 AM

Still having a ton of problems trying to get this problem to calculate properly. Is the float causing an error in the math?

/**

* Mortgage Calculator- Complete Change Request #4 in Service Request SR-mf-003.

* Insert comments in the program to document the program. Attach a design flow chart

* to the source code of the program.

*

* Change Request #4: 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.

*

* Author Phil Grabowski

* University of Phoenix

* PRG421 Java Programming II

* Course Date 2/24/09  End Date 3/30/09

* Instructor Yining Li

*/



import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;



public class MortgageCR41 extends JFrame implements ActionListener {



public MortgageCR41()

{

Container content = this.getContentPane();

	content.setBackground(Color.white);

	content.setLayout(new FlowLayout());

	content.add(new JButton("Loan Amount"));

  content.add(jTextField1=new JTextField("					   "));  /// ** added jTextField1

	content.add(new JButton("Interest Rate"));

  content.add(jTextField2=new JTextField("					   "));

	content.add(new JButton("Term in Years"));

  content.add(jTextField3=new JTextField("					   "));

  content.add(new JButton("Montly Payment"));

  content.add(jTextField4=new JTextField("					   "));

}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {											

	   

	}  

									 



	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {										

		System.exit(0);

	}				  

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {										

		jTextField1.setText("");

		jTextField2.setText("");

		jTextField3.setText("");

	}									  



	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {										

	 

	float num1, num2, num3, result;



  // num1 = Amount of loan

  // num2 = Interest of Loan

  // num3 = Term in years



	num1 = Float.parseFloat(jTextField1.getText());

	num2 = Float.parseFloat(jTextField2.getText());

	num3 = Float.parseFloat(jTextField3.getText());

   

/* Horace </dream.in.code> Programming & web developement community. suggested
an alternative would be to cast the result of the expression using pow() which
returns a double to a float, e.g. */


// This is the formula from the instructor
// PMT calculation
//  PMT=((PV*(IR/12))/(1 - Math.pow(1 + (IR/12), -NP)));
// PMT = Result

// int PV = 200000; //Principle Loan Amount
//  final double IR = 0.0575; //Interest Rate, by Month
//  int NP = 360; //Length of loan in months
//  final double PMT;

// Converted variables from the instructors formula, still get the same result.
 
result=((num1*num2/12))/(1-(float)Math.pow(1 + (num2/12),-(num3*12)));

// working formula result=(num1*((num2/12)/1-(float)Math.pow((1+(num1/12)),-(num3*12))));	 

	 
//	 result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));


   System.out.println("ccc " + String.valueOf(result));

	jTextField3.setText(String.valueOf(result));



	}									  



   

	public static void main(String args[]) {

	 

// JFrame f = new JFrame("Mortgage Calculator CR#4");

/*Instructor Yining Li added the constructor to the program. */	   

			 MortgageCR41 f = new MortgageCR41();

			 

				f.setSize(300, 250);

				f.setVisible(true);

			}

	   

   

	// Variables declaration - do not modify					

	private javax.swing.JButton jButton1;

	private javax.swing.JButton jButton2;

	private javax.swing.JButton jButton3;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel2;

	private javax.swing.JLabel jLabel3;

	private javax.swing.JPanel jPanel1;

	private javax.swing.JTextField jTextField1;

	private javax.swing.JTextField jTextField2;

	private javax.swing.JTextField jTextField3;

	// End of variables declaration				  
	private javax.swing.JTextField jTextField4;



  JButton myButton = new JButton("Calculate");

   

   JCheckBox myCheckBox = new JCheckBox("Reset Fields");

   

	// Clear all fields in Jbuttons



   JTextArea myText = new JTextArea("Please enter the fields and click calculate");



   JPanel Panel = new JPanel();



   JPanel holdAll = new JPanel();

  {

	Panel.setLayout(new FlowLayout());

	Panel.add(myCheckBox);

	Panel.add(myButton);



	holdAll.setLayout(new BorderLayout());

	holdAll.add(Panel, BorderLayout.NORTH);

	holdAll.add(myText, BorderLayout.CENTER);



	getContentPane().add(holdAll, BorderLayout.CENTER);



	myButton.addActionListener(this);

	myCheckBox.addActionListener(this);



	setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  }



  public void actionPerformed(ActionEvent e)

  {
	System.out.println("actionPerformed");
	 float num1, num2, num3, result;



  // num1 = Amount of loan

  // num2 = Interest of Loan

  // num3 = Term in years



	num1 = Float.parseFloat(jTextField1.getText());

	num2 = Float.parseFloat(jTextField2.getText());

	num3 = Float.parseFloat(jTextField3.getText());

   



	 

  result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));

   System.out.println("ccc " + String.valueOf(result));

	jTextField4.setText(String.valueOf(result));

  if (e.getSource() == myButton)

	  myText.setText("Thank you");



	else if (e.getSource() == myCheckBox)

	  myText.setText("Fields Cleared" +

					 myCheckBox.isSelected());

	else

	  myText.setText("E ...?");

  }

}



Was This Post Helpful? 0
  • +
  • -

#11 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Math Error, no display

Posted 08 March 2009 - 11:55 AM

Quote

Nope that doesn't work, I just get compile errors on it.

MortgageCR41.java:81: illegal start of type
this.setTitle("Mortgage Calculator CR#4");
^
MortgageCR41.java:81: ';' expected

You can set a title for your JFrame once you declare it and instaniate the JFrame class here is an example:
JFrame w = new JFrame("your title");


and to set the title for the same JFrame later, for example you want to change it later in the program you can add:
w.setTitle("your title2");


Hope this helps, good luck :)
Was This Post Helpful? 1
  • +
  • -

#12 grabowpd   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Math Error, no display

Posted 09 March 2009 - 10:57 AM

View Postayman_mastermind, on 8 Mar, 2009 - 10:55 AM, said:

Quote

Nope that doesn't work, I just get compile errors on it.

MortgageCR41.java:81: illegal start of type
this.setTitle("Mortgage Calculator CR#4");
^
MortgageCR41.java:81: ';' expected

You can set a title for your JFrame once you declare it and instaniate the JFrame class here is an example:
JFrame w = new JFrame("your title");


and to set the title for the same JFrame later, for example you want to change it later in the program you can add:
w.setTitle("your title2");


Hope this helps, good luck :)


This worked, so I thanked for the post being helpful.

MortgageCR41 f = new MortgageCR41();

f.setTitle("Mortgage Calculator CR#4");

f.setSize(300,250);

f.setVisible(true);




Now if I could only get the math function to work properly.
Was This Post Helpful? 0
  • +
  • -

#13 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Math Error, no display

Posted 09 March 2009 - 11:05 AM

Quote

Now if I could only get the math function to work properly.

Which one in specific? Thanks
Was This Post Helpful? 0
  • +
  • -

#14 grabowpd   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Math Error, no display

Posted 09 March 2009 - 01:32 PM

View Postayman_mastermind, on 9 Mar, 2009 - 10:05 AM, said:

Quote

Now if I could only get the math function to work properly.

Which one in specific? Thanks


Well I'm not sure how I did it. Too much coding in too little time but I doubled up on the math. I quess what makes this kind of difficult is when you work on different versions and you attempt to create a new version, you have to change a bunch of code to do it. So that just helps the confusion.

So everytime I try and take out the duplicate code I start getting compile errors and forget what I did. My mind is not all there either. Wife left 10 months ago.

It appears that the calculation is closer than it was in my new code. Got the title bar working. Thank you.

The only other thing I do not have working properly is clearing the jFields. However I think it is pretty minor. Probally should have used a jButton rather than a check box.

I still have to document this out and submit by tonight.

Thank you everyone for your help.




/**

* Mortgage Calculator- Complete Change Request #4 in Service Request SR-mf-003.

* Insert comments in the program to document the program. Attach a design flow chart

* to the source code of the program.

*

* Change Request #4: 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.

*

* Author Phil Grabowski

* University of Phoenix

* PRG421 Java Programming II

* Course Date 2/24/09  End Date 3/30/09

* Instructor Yining Li

*/



import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;



public class MortgageCR41 extends JFrame implements ActionListener {



public MortgageCR41()

{

Container content = this.getContentPane();

	content.setBackground(Color.white);

	content.setLayout(new FlowLayout());

	content.add(new JButton("Loan Amount"));

  content.add(jTextField1=new JTextField("					"));  /// ** added jTextField1

	content.add(new JButton("Interest Rate"));

  content.add(jTextField2=new JTextField("					"));

	content.add(new JButton("Term in Years"));

  content.add(jTextField3=new JTextField("					"));

  content.add(new JButton("Montly Payment"));

  content.add(jTextField4=new JTextField("					"));

}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {											

	   

	}  

									 



	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {										

		System.exit(0);

	}				  

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {										

		jTextField1.setText("");

		jTextField2.setText("");

		jTextField3.setText("");

	}									  



	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {										

	 

	float num1, num2, num3, result;

/* This is used to convert variables in formulas.

  // num1 = Amount of loan

  // num2 = Interest of Loan

  // num3 = Term in years  */



	num1 = Float.parseFloat(jTextField1.getText());

	num2 = Float.parseFloat(jTextField2.getText());

	num3 = Float.parseFloat(jTextField3.getText());

   
/* This is the formula from the instructor
	PMT calculation
	PMT=((PV*(IR/12))/(1 - Math.pow(1 + (IR/12), -NP)));
 	MT = Result 

 	nt PV = 200000; //Principle Loan Amount
 	inal double IR = 0.0575; //Interest Rate, by Month
 	nt NP = 360; //Length of loan in months
 	inal double PMT; */

/*Converted variables from the instructors formula, still get the same result.
 result=((num1*num2/12))/(1-(float)Math.pow(1 + (num2/12),-(num3*12)));
 working formula result=(num1*((num2/12)/1-(float)Math.pow((1+(num1/12)),-(num3*12)))); */	

//	 Original Formula   result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));


/*   System.out.println("ccc " + String.valueOf(result));

	jTextField4.setText(String.valueOf(result));*/



	}									  

 

	public static void main(String args[]) {

	 

// JFrame f = new JFrame("Mortgage Calculator CR#4"); Failed line to add title.

/*Instructor Yining Li added the constructor to the program. */	   

			 MortgageCR41 f = new MortgageCR41();

			 f.setTitle("Mortgage Calculator CR#4"); 
				  /*puts the title in the frame title. ayman mastermind (2009). Dream in code.
				  */
				  
				  
				f.setSize(300, 250);

				f.setVisible(true);

			}

	   
	// Variables declaration - do not modify					

	private javax.swing.JButton jButton1;

	private javax.swing.JButton jButton2;

	private javax.swing.JButton jButton3;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel2;

	private javax.swing.JLabel jLabel3;

	private javax.swing.JPanel jPanel1;

	private javax.swing.JTextField jTextField1;

	private javax.swing.JTextField jTextField2;

	private javax.swing.JTextField jTextField3;

					  
	private javax.swing.JTextField jTextField4;

// End of variables declaration

  JButton myButton = new JButton("Calculate");

   

   JCheckBox myCheckBox = new JCheckBox("Reset Fields");

   

	// Clear all fields in Jbuttons



   JTextArea myText = new JTextArea("Please enter the fields and click calculate");



   JPanel Panel = new JPanel();



   JPanel holdAll = new JPanel();

  {

	Panel.setLayout(new FlowLayout());

	Panel.add(myCheckBox);

	Panel.add(myButton);



	holdAll.setLayout(new BorderLayout());

	holdAll.add(Panel, BorderLayout.NORTH);

	holdAll.add(myText, BorderLayout.CENTER);



	getContentPane().add(holdAll, BorderLayout.CENTER);



	myButton.addActionListener(this);

	myCheckBox.addActionListener(this);



	setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  }



  public void actionPerformed(ActionEvent e)

  {
	System.out.println("actionPerformed");
	 float num1, num2, num3, result;



  // num1 = Amount of loan

  // num2 = Interest of Loan

  // num3 = Term in years



	num1 = Float.parseFloat(jTextField1.getText());

	num2 = Float.parseFloat(jTextField2.getText());

	num3 = Float.parseFloat(jTextField3.getText());

   

/* Horace </dream.in.code> Programming & web developement community. suggested
an alternative would be to cast the result of the expression using pow() which
returns a double to a float, e.g. */


	 
 result=(num1*((num2/12)/1-(float)Math.pow((1+(num1/12)),-(num3*12))));
//  result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));

   System.out.println("ccc " + String.valueOf(result));

	jTextField4.setText(String.valueOf(result));

  if (e.getSource() == myButton)

	  myText.setText("Thank you");



	else if (e.getSource() == myCheckBox)

	  myText.setText("Fields Cleared" +

					 myCheckBox.isSelected());

	else

	  myText.setText("E ...?");

  }

}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1