Help with Button
Hello
I am working on a simple mortgage program. I can not get the program to compile correctly. The program is supposed to allow the user to input the amout of the mortgage and then select from a menu of loans: 7 year at 5.35%, 15 year at 5.5%, and 30 year at 5.75%. After a loan is selected the mortgage payment amount will display.
Thank you in advance to anyone that is willing to help me out.
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DecimalFormat;
publicclass Mortgage2implements ActionListener{interface
{
JFrame frame;
JPanel panel;
JLabel prinLbl, aprLbl, termLbl, pymtLbl;
JTextField prinTf, aprTf, termTf, pymtTf;
JButton calcBtn;
JButton mortBtn1;
JButton mortBtn2;
JButton mortBtn3;
public Mortgage2()
{
frame =new JFrame("Mortgage Calculator");
frame =new JFrame("By: Kalei");
panel =new JPanel();
panel.setLayout(new GridLayout(5,2));
prinLbl =new JLabel ("Amount of the Mortgage");
prinTf =new JTextField (0);
aprTf =new JTextField (0);
termTf =new JTextField (0);
pymtTf =new JTextField(0);
prinLbl =new JLabel ("Select a Mortgage Plan");
mortBtn1 =new JButton ("7 Year @ 5.35%");
mortBtn2 =new JButton ("15 Year @ 5.5%");
mortBtn3 =new JButton ("30 Year @ 5.75%");
pymtLbl =new JLabel ("The monthly Mortgage payment");
calcBtn =new JButton ("Calculate");
panel.add(prinLbl);
panel.add(prinTf);
panel.add(aprLbl);
panel.add(aprTf);
panel.add(termLbl);
panel.add(termTf);
panel.add(pymtLbl);
panel.add(pymtTf);
panel.add(mortBtn1);
panel.add(mortBtn2);
panel.add(mortBtn3);
panel.add(calcBtn);
frame.getContentPane().add(panel, BorderLayout.CENTER);
mortBtn1.addActionListener(this);
mortBtn2.addActionListener(this);
mortBtn3.addActionListener(this);
calcBtn.addActionListener(new calcBtnlistener ());
frame.setSize(400,300);
frame.setLocation(200,100);
frame.setVisible(true);
}// end of Mortgage3
publicstaticvoid main (String[] args){
new Mortgage();
}
// Handler for the "Calculate" button
class calcBtnlistenerimplements ActionListener{
publicvoid actionPerformed(ActionEvent e){
double apr = 5.75, years = 30.0, pv = 200000;
// The calculations are done as follows.
double r = apr / 100.0 / 12.0;
double n = years * 12.0;
double pymt = pv * r / (1.0 - Math.pow((1.0 + r), -n));
pymtTf.setText("" + pymt);
}
}
}}
[4890 byte] By [
Kaleia] at [2007-10-2 4:32:27]

> I am working on a simple mortgage program. I can not get the program to compile correctly.
so, you copied the code from this post:
http://forum.java.sun.com/thread.jspa?threadID=653355&messageID=3882604
it doesn't work, and you want someone to fix it so you can hand it in as all your own work.
That about cover it?
You are right I copied parts of the code. I am still new to Java and I thought that using pieces of people's code was an acceptable practice. I would like to apologize if that was wrong. I did however post the wrong code. I posted the code word for word and I meant to post my own code that I used information from your code in. If this is wrong I will take the information I used out because I seriously don't want to cheat. The code below is my own that I have been modifying for the past 8 weeks in a Java programming class. The class is on line so it is hard to get help from classmates.
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat; //this class displays decimal format
public class Mortgage2 extends JPanel implements ActionListener
{
JFrame frame = new JFrame ("Mortgage Calculator");
JPanel panel = new JPanel ();
JFrame frame = new JFrame ("Please Select a Loan Term");
JButton mort1 = new JButton ("7 Year at 5.35%");
JButton mort2 = new JButton ("15 Year at 5.5%");
JButton mort3 = new JButton ("30 Year at 5.75%");
JButton exit = new JButton ("Exit the Calculator");
JLabel pay = new JLabel ("The Monthly Payment");
JScrollPane scroll = new JScrollPane(pane);
public Mortgage2
{
super ("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(new GridLayout(5,2));
panel.add(pay);
panel.add(exit);
panel.add(mort1);
panel.add(mort2);
panel.add(mort3);
mort1.addActionListener(this);
mort2.addActionListener(this);
mort3.addActionListener(this);
}//end Mortgage2
public static void main(String[] args)
{
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setSize(400,300);
frame.setLocation(200,100);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//declare class variables values
mortgage = 200000.00;
principal = 200000.00; //the current mortgage amount of the loan
interestRateYears = .0575;
interestRateMonths = (interestRateYears / 12) / 100;
termYears = 30;
termMonths = (termYears * 12);
monthlyInterestPayment = 0;
monthlyPrincipalPayment = 0;
balance = principal;
linecount = 15;
//this text formats the numbers to display two decimal places
java.text.DecimalFormat dec = new java.text.DecimalFormat ("###.00");
//calculations
mortgage = ((200000*.0575)+200000)/360; //monthly payment from week 2 program
monthlyPayment = (principal * interestRateMonths) /
(1 - Math.pow(1 + interestRateMonths, - termMonths)); //calculation retrieved from Dr math
// formula to calculate monthly interest and principal payments
monthlyInterestPayment = (balance * interestRateMonths);
monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment); //retrieved from Dr math
// start while loop
while (termMonths > 0)
{
// information to display
System.out.println(termMonths + "\t\t$" + dec.format(monthlyPrincipalPayment) +
"\t\t$" + dec.format(monthlyInterestPayment) +
"\t\t$" + dec.format(balance));
// decrement months
termMonths--;
// calculate interest and principal payments
monthlyInterestPayment = (balance * interestRateMonths);
monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);
balance = (balance - monthlyInterestPayment);
// these conditional statements cause the results to pause
if(linecount == 20)
{
linecount = 0;
try
{
Thread.sleep(3000);// pause to last three seconds
}
catch (InterruptedException e)
{
}
}// end if
else
{
linecount++;
}// end else
}// end while
} // end main
} //end program
> You are right I copied parts of the code. I am still
> new to Java and I thought that using pieces of
> people's code was an acceptable practice.
As a general principle, code reuse is a good thing. But if this is an assignment, meant for you to learn something, then it's not so good a thing. Assuming, of course, that you are in fact interested in learning.
What if your teacher asks you to explain certain details in the code, why you chose to do it this way or that? What will you answer?
Technically there is nothing wrong in using code that is in the 'public domain',
and, when you are researching a problem, it is working code you seek.
However, when you are doing this as a learning exercise, you will not gain any
knowledge form copy / get someone to fix / submit. The whole point of this, and
any exercise, is for you to learn something that you can carry forward into your
next assignment. If you do not want to do that, then what is the point of doing
the course?
Now, with your own code - for the moment forget everything to do with the
calculation and output.
Just concentrate on the getting the gui right.
Start by drawing it on paper, with these in mind
1)
What data do you require from the user?
years&rate
amount
other data?
2)
How are you going to get that data?
a JComboBox might be better than JButtons for year@rate
a JTextField for amount
others?
3)
how are you going to display the output?
perhaps just a JTextArea will do for this
or a JTabel, if adventurous
4) other incidentals?
a Jbutton to trigger the calculation/display?
a Jbutton to exit?
a JLabel for messages - perhaps the textArea will do for this?
you will probably need to nest some layout managers to get it all looking right
here are a couple of links
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html
when you have the look right, add listener code for the buttons,
but just put some println() statements at first - just to make sure the listeners are firing.
when OK, add the calculation code (with the output code).
Don't worry about the format of the output, just get the result correct.
When the result is correct you can then look at the formatting the output
Have a go at it, and post questions relating to specific problems.
