richmp95
asked on
i have two versions trying to get either one to work. both doing the same thing. getting a square in the answer box. the second version i need help to get the amort to work
First version
import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MortgageCalc extends JFrame implements ActionListener
{
//declare variables
double rateMo, rateApr, monthPmt, principal, iMonths, iYears;
//create JLabels for entry
JLabel Dprincipal = new JLabel("Please enter Principal: ");
JLabel RateApr = new JLabel("Please enter APR: ");
JLabel Years = new JLabel("Please enter Term: ");
JLabel MonthPmt = new JLabel("Your Monthly Payment will be: ");
JTextField loanPrincipal = new JTextField("", 10);
JTextField loanRate = new JTextField("", 10);
JTextField loanTerm = new JTextField("", 10);
JTextField loanPayment = new JTextField("", 10);
//create buttons
JButton CalcPayment = new JButton("Calculate Payment");
JButton exit = new JButton("Exit");
JButton reset = new JButton("Reset");
public MortgageCalc()
{
//Create Frame
Container container = getContentPane();
container.setLayout(new BorderLayout(10,10));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(6,4));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(8,2));
JPanel answerPanel = new JPanel();
answerPanel.setLayout(new FlowLayout());
//add components to input panel
inputPanel.add(Dprincipal) ;
inputPanel.add(loanPrincip al);
inputPanel.add(RateApr);
inputPanel.add(loanRate);
inputPanel.add(Years);
inputPanel.add(loanTerm);
//add components to button panel
buttonPanel.add(CalcPaymen t);
buttonPanel.add(reset);
buttonPanel.add(exit);
//add componets to answer
answerPanel.add(MonthPmt);
answerPanel.add(loanPaymen t);
container.add(inputPanel, BorderLayout.WEST);
container.add(buttonPanel, BorderLayout.EAST);
container.add(answerPanel, BorderLayout.SOUTH);
//add listeners
CalcPayment.addActionListe ner(this);
reset.addActionListener(th is);
exit.addActionListener(thi s);
loanPayment.setEditable(fa lse);
}//End MortgageCalc
public void actionPerformed(ActionEven t e)
{
Object source = e.getSource();
if(source == CalcPayment)
{
String tPrincipal = loanPrincipal.getText();
String rateAPR = loanRate.getText();
String years = loanTerm.getText();
try
{
double Dprincipal = Double.parseDouble(tPrinci pal);
if (Dprincipal <= 0.00) throw new NumberFormatException();
double rateApr = Double.parseDouble(rateAPR );
if (rateApr <= 0.00) throw new NumberFormatException();
double iYears = Double.parseDouble(years);
if (iYears <= 0.0) throw new NumberFormatException();
}
catch(NumberFormatExceptio n f)
{
JOptionPane.showMessageDia log(null, "Invalid Entry, Please Try Again.", "Error", JOptionPane.INFORMATION_ME SSAGE);
}
// calculations
rateMo = (rateApr /12)/100;
iMonths = (iYears * 12);
monthPmt = (principal * rateMo) / (1- Math.pow(1 + rateMo, - iMonths));
DecimalFormat twoDigits = new DecimalFormat("$#,###.00") ;
loanPayment.setText(twoDig its.format (monthPmt) );
}
else if (source == reset)
{
loanPrincipal.setText("");
loanRate.setText("");
loanTerm.setText("");
loanPayment.setText("");
loanPrincipal.requestFocus ();
}
else
{
System.exit(0);
}
}//end
public static void main(String[] args) throws IOException
{
MortgageCalc frame = new MortgageCalc();
frame.setTitle("Mortgage Payment Calculator V2.0");
frame.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.setSize(1200,800);
frame.setVisible(true);
}//end Main
public double getResult()
{
return monthPmt;
}
}//end program
second version
import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mortgage extends JFrame implements ActionListener
{
// declare some variables
double dRateMo, dRateAPR, dMonthPmt, dPrincipal, iMonths, iYears;
// create JLabels for entry fields
JLabel greeting = new JLabel("Good Day! Please enter your name.");
JLabel Principal = new JLabel("Please enter principal: ");
JLabel rateAPR = new JLabel("Please enter loan rate: ");
JLabel years = new JLabel("Please enter term: ");
JLabel payment = new JLabel("Your monthly payment will be: ");
// create textfields for user entries
JTextField nameField = new JTextField("", 10);
JTextField loanPrincipal = new JTextField("", 10);
JTextField loanRate = new JTextField("", 10);
JTextField loanTerm = new JTextField("", 10);
JTextField loanPayment = new JTextField("", 20);
//create buttons for use in panels
JButton calcPayment = new JButton("Calculate Payment");
JButton calcAmort = new JButton("Show Amortization Table");
JButton nameEntry = new JButton("Enter Name");
JButton exit = new JButton("Exit");
JButton reset = new JButton("Reset");
public Mortgage()
{
// create frame and panels
Container container = getContentPane();
container.setLayout(new BorderLayout(5,5));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(3,2));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4,1));
JPanel greetingPanel = new JPanel();
greetingPanel.setLayout(ne w FlowLayout());
JPanel answerPanel = new JPanel();
answerPanel.setLayout(new FlowLayout());
// add compenents to greeting panel
greetingPanel.add(greeting );
greetingPanel.add(nameFiel d);
greetingPanel.add(nameEntr y);
//add components to input panel
inputPanel.add(Principal);
inputPanel.add(loanPrincip al);
inputPanel.add(rateAPR);
inputPanel.add(loanRate);
inputPanel.add(years);
inputPanel.add(loanTerm);
//add components to button panel
buttonPanel.add(calcPaymen t);
buttonPanel.add(calcAmort) ;
buttonPanel.add(reset);
buttonPanel.add(exit);
//add components to answer panel
answerPanel.add(payment);
answerPanel.add(loanPaymen t);
//add panels to frame
container.add(greetingPane l, BorderLayout.NORTH);
container.add(inputPanel, BorderLayout.WEST);
container.add(buttonPanel, BorderLayout.EAST);
container.add(answerPanel, BorderLayout.SOUTH);
// add listeners to buttons
calcPayment.addActionListe ner(this);
calcAmort.addActionListene r(this);
reset.addActionListener(th is);
exit.addActionListener(thi s);
nameEntry.addActionListene r(this);
loanPayment.setEditable(fa lse);
}
public void actionPerformed(ActionEven t e)
{
Object source = e.getSource();
if (source == nameEntry)
{
String name = nameField.getText();
remove(nameField);
remove(nameEntry);
validate();
greeting.setText("Welcome, " + name + " !");
}
else if (source == calcPayment)
{
String totPrincipal = loanPrincipal.getText();
String rateAPR = loanRate.getText();
String years = loanTerm.getText();
try
{
double dPrincipal = Double.parseDouble(totPrin cipal);
if (dPrincipal <= 0.00) throw new NumberFormatException();
double dRateAPR = Double.parseDouble(rateAPR );
if (dRateAPR <= 0.00) throw new NumberFormatException();
double iYears = Double.parseDouble(years);
if (iYears <= 0.0) throw new NumberFormatException();
}
catch(NumberFormatExceptio n f)
{
JOptionPane.showMessageDia log(null, "Invalid Entry, please try again.", "Error", JOptionPane.INFORMATION_ME SSAGE);
}
// declare conversion for APR to monthly rate and years to months
iMonths = iYears * 12;
dRateMo = dRateAPR / (12*100);
dMonthPmt = getPayment(dRateMo, iMonths, dPrincipal);
DecimalFormat twoDigits = new DecimalFormat("$###,000.00 ");
loanPayment.setText(twoDig its.format (dMonthPmt ));
}
else if (source == calcAmort)
{
JOptionPane.showMessageDia log(null, "Under Construction", "Error", JOptionPane.INFORMATION_ME SSAGE);
/*String Principal = loanPrincipal.getText();
String rateAPR = loanRate.getText();
String years = loanTerm.getText();
try
{
double dPrincipal = Double.parseDouble(Princip al);
if (dPrincipal <= 0.00) throw new NumberFormatException();
double dRateAPR = Double.parseDouble(rateAPR );
if (dRateAPR <= 0.00) throw new NumberFormatException();
double iYears = Double.parseDouble(years);
if (iYears <= 0.0) throw new NumberFormatException();
// declare conversion for APR to monthly rate and years to months
iMonths = iYears * 12;
dRateMo = dRateAPR / (12*100);
dMonthPmt = getPayment(dRateMo, iMonths, dPrincipal);
PaymentCalc.Calculation(dP rincipal, dRateMo, dMonthPmt, iMonths);
if (dPrincipal <= 0.0 || dRateMo <= 0.0 || dMonthPmt <= 0.0 || iMonths <= 0)
throw new NumberFormatException();
}
catch(NumberFormatExceptio n f)
{
JOptionPane.showMessageDia log(null, "Invalid Entry, please try again.", "Error", JOptionPane.INFORMATION_ME SSAGE);
}
*/
}
else if (source == reset)
{
loanPrincipal.setText("");
loanRate.setText("");
loanTerm.setText("");
loanPayment.setText("");
loanPrincipal.requestFocus ();
}
else
{
// user clicks exit button
System.exit(0);
}
}
public static void main(String[] args) throws IOException
{
Mortgage frame = new Mortgage();
frame.setTitle("Mortgage Payment Calculator");
frame.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.setSize(600,400);
frame.setVisible(true);
}//end main
// The getPayment method accepts Monthly rate, months, and principal variables
// and returns the monthly payment.
public static double getPayment(double dRateMo, double iMonths, double dPrincipal)
{
return (dRateMo + (dRateMo / ((Math.pow((1+ dRateMo),iMonths)) - 1)))*dPrincipal;
}//end getpayment
}//end class
import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MortgageCalc extends JFrame implements ActionListener
{
//declare variables
double rateMo, rateApr, monthPmt, principal, iMonths, iYears;
//create JLabels for entry
JLabel Dprincipal = new JLabel("Please enter Principal: ");
JLabel RateApr = new JLabel("Please enter APR: ");
JLabel Years = new JLabel("Please enter Term: ");
JLabel MonthPmt = new JLabel("Your Monthly Payment will be: ");
JTextField loanPrincipal = new JTextField("", 10);
JTextField loanRate = new JTextField("", 10);
JTextField loanTerm = new JTextField("", 10);
JTextField loanPayment = new JTextField("", 10);
//create buttons
JButton CalcPayment = new JButton("Calculate Payment");
JButton exit = new JButton("Exit");
JButton reset = new JButton("Reset");
public MortgageCalc()
{
//Create Frame
Container container = getContentPane();
container.setLayout(new BorderLayout(10,10));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(6,4));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(8,2));
JPanel answerPanel = new JPanel();
answerPanel.setLayout(new FlowLayout());
//add components to input panel
inputPanel.add(Dprincipal)
inputPanel.add(loanPrincip
inputPanel.add(RateApr);
inputPanel.add(loanRate);
inputPanel.add(Years);
inputPanel.add(loanTerm);
//add components to button panel
buttonPanel.add(CalcPaymen
buttonPanel.add(reset);
buttonPanel.add(exit);
//add componets to answer
answerPanel.add(MonthPmt);
answerPanel.add(loanPaymen
container.add(inputPanel, BorderLayout.WEST);
container.add(buttonPanel,
container.add(answerPanel,
//add listeners
CalcPayment.addActionListe
reset.addActionListener(th
exit.addActionListener(thi
loanPayment.setEditable(fa
}//End MortgageCalc
public void actionPerformed(ActionEven
{
Object source = e.getSource();
if(source == CalcPayment)
{
String tPrincipal = loanPrincipal.getText();
String rateAPR = loanRate.getText();
String years = loanTerm.getText();
try
{
double Dprincipal = Double.parseDouble(tPrinci
if (Dprincipal <= 0.00) throw new NumberFormatException();
double rateApr = Double.parseDouble(rateAPR
if (rateApr <= 0.00) throw new NumberFormatException();
double iYears = Double.parseDouble(years);
if (iYears <= 0.0) throw new NumberFormatException();
}
catch(NumberFormatExceptio
{
JOptionPane.showMessageDia
}
// calculations
rateMo = (rateApr /12)/100;
iMonths = (iYears * 12);
monthPmt = (principal * rateMo) / (1- Math.pow(1 + rateMo, - iMonths));
DecimalFormat twoDigits = new DecimalFormat("$#,###.00")
loanPayment.setText(twoDig
}
else if (source == reset)
{
loanPrincipal.setText("");
loanRate.setText("");
loanTerm.setText("");
loanPayment.setText("");
loanPrincipal.requestFocus
}
else
{
System.exit(0);
}
}//end
public static void main(String[] args) throws IOException
{
MortgageCalc frame = new MortgageCalc();
frame.setTitle("Mortgage Payment Calculator V2.0");
frame.setDefaultCloseOpera
frame.setSize(1200,800);
frame.setVisible(true);
}//end Main
public double getResult()
{
return monthPmt;
}
}//end program
second version
import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mortgage extends JFrame implements ActionListener
{
// declare some variables
double dRateMo, dRateAPR, dMonthPmt, dPrincipal, iMonths, iYears;
// create JLabels for entry fields
JLabel greeting = new JLabel("Good Day! Please enter your name.");
JLabel Principal = new JLabel("Please enter principal: ");
JLabel rateAPR = new JLabel("Please enter loan rate: ");
JLabel years = new JLabel("Please enter term: ");
JLabel payment = new JLabel("Your monthly payment will be: ");
// create textfields for user entries
JTextField nameField = new JTextField("", 10);
JTextField loanPrincipal = new JTextField("", 10);
JTextField loanRate = new JTextField("", 10);
JTextField loanTerm = new JTextField("", 10);
JTextField loanPayment = new JTextField("", 20);
//create buttons for use in panels
JButton calcPayment = new JButton("Calculate Payment");
JButton calcAmort = new JButton("Show Amortization Table");
JButton nameEntry = new JButton("Enter Name");
JButton exit = new JButton("Exit");
JButton reset = new JButton("Reset");
public Mortgage()
{
// create frame and panels
Container container = getContentPane();
container.setLayout(new BorderLayout(5,5));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(3,2));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4,1));
JPanel greetingPanel = new JPanel();
greetingPanel.setLayout(ne
JPanel answerPanel = new JPanel();
answerPanel.setLayout(new FlowLayout());
// add compenents to greeting panel
greetingPanel.add(greeting
greetingPanel.add(nameFiel
greetingPanel.add(nameEntr
//add components to input panel
inputPanel.add(Principal);
inputPanel.add(loanPrincip
inputPanel.add(rateAPR);
inputPanel.add(loanRate);
inputPanel.add(years);
inputPanel.add(loanTerm);
//add components to button panel
buttonPanel.add(calcPaymen
buttonPanel.add(calcAmort)
buttonPanel.add(reset);
buttonPanel.add(exit);
//add components to answer panel
answerPanel.add(payment);
answerPanel.add(loanPaymen
//add panels to frame
container.add(greetingPane
container.add(inputPanel, BorderLayout.WEST);
container.add(buttonPanel,
container.add(answerPanel,
// add listeners to buttons
calcPayment.addActionListe
calcAmort.addActionListene
reset.addActionListener(th
exit.addActionListener(thi
nameEntry.addActionListene
loanPayment.setEditable(fa
}
public void actionPerformed(ActionEven
{
Object source = e.getSource();
if (source == nameEntry)
{
String name = nameField.getText();
remove(nameField);
remove(nameEntry);
validate();
greeting.setText("Welcome,
}
else if (source == calcPayment)
{
String totPrincipal = loanPrincipal.getText();
String rateAPR = loanRate.getText();
String years = loanTerm.getText();
try
{
double dPrincipal = Double.parseDouble(totPrin
if (dPrincipal <= 0.00) throw new NumberFormatException();
double dRateAPR = Double.parseDouble(rateAPR
if (dRateAPR <= 0.00) throw new NumberFormatException();
double iYears = Double.parseDouble(years);
if (iYears <= 0.0) throw new NumberFormatException();
}
catch(NumberFormatExceptio
{
JOptionPane.showMessageDia
}
// declare conversion for APR to monthly rate and years to months
iMonths = iYears * 12;
dRateMo = dRateAPR / (12*100);
dMonthPmt = getPayment(dRateMo, iMonths, dPrincipal);
DecimalFormat twoDigits = new DecimalFormat("$###,000.00
loanPayment.setText(twoDig
}
else if (source == calcAmort)
{
JOptionPane.showMessageDia
/*String Principal = loanPrincipal.getText();
String rateAPR = loanRate.getText();
String years = loanTerm.getText();
try
{
double dPrincipal = Double.parseDouble(Princip
if (dPrincipal <= 0.00) throw new NumberFormatException();
double dRateAPR = Double.parseDouble(rateAPR
if (dRateAPR <= 0.00) throw new NumberFormatException();
double iYears = Double.parseDouble(years);
if (iYears <= 0.0) throw new NumberFormatException();
// declare conversion for APR to monthly rate and years to months
iMonths = iYears * 12;
dRateMo = dRateAPR / (12*100);
dMonthPmt = getPayment(dRateMo, iMonths, dPrincipal);
PaymentCalc.Calculation(dP
if (dPrincipal <= 0.0 || dRateMo <= 0.0 || dMonthPmt <= 0.0 || iMonths <= 0)
throw new NumberFormatException();
}
catch(NumberFormatExceptio
{
JOptionPane.showMessageDia
}
*/
}
else if (source == reset)
{
loanPrincipal.setText("");
loanRate.setText("");
loanTerm.setText("");
loanPayment.setText("");
loanPrincipal.requestFocus
}
else
{
// user clicks exit button
System.exit(0);
}
}
public static void main(String[] args) throws IOException
{
Mortgage frame = new Mortgage();
frame.setTitle("Mortgage Payment Calculator");
frame.setDefaultCloseOpera
frame.setSize(600,400);
frame.setVisible(true);
}//end main
// The getPayment method accepts Monthly rate, months, and principal variables
// and returns the monthly payment.
public static double getPayment(double dRateMo, double iMonths, double dPrincipal)
{
return (dRateMo + (dRateMo / ((Math.pow((1+ dRateMo),iMonths)) - 1)))*dPrincipal;
}//end getpayment
}//end class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry that was for the second one (Mortgage)
and the first one (MortgageCalc) should be
double rateApr=0, iYears=0;
try
{
principal = Double.parseDouble(tPrinci pal);
if (principal <= 0.00) throw new NumberFormatException();
rateApr = Double.parseDouble(rateAPR );
if (rateApr <= 0.00) throw new NumberFormatException();
iYears = Double.parseDouble(years);
if (iYears <= 0.0) throw new NumberFormatException();
}
double rateApr=0, iYears=0;
try
{
principal = Double.parseDouble(tPrinci
if (principal <= 0.00) throw new NumberFormatException();
rateApr = Double.parseDouble(rateAPR
if (rateApr <= 0.00) throw new NumberFormatException();
iYears = Double.parseDouble(years);
if (iYears <= 0.0) throw new NumberFormatException();
}