2 Replies - 8115 Views - Last Post: 07 September 2010 - 05:58 AM Rate Topic: -----

#1 MrsE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 06-September 10

Java Mortgage Calculator GUI Help

Posted 06 September 2010 - 05:41 PM

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 and the user's
selection from a menu of available mortgage loans:
- 7 years at 5.35%
- 15 years at 5.5%
- 30 years at 5.75%
Use an array for the mortgage data for the different loans. Display the mortgage payment amountfollowed by the loan balance and interest paid for each payment over the term of the loan. Allow
the user to loop back and enter a new amount and make a new selection or quit. Please insert
comments in the program to document the program.

Here are my errors:

double cannot be dereferenced Line 112
double cannot be dereferenced Line 113


Here is my code:
/** Mortgage Calculator Program
 * Week 2 PRG421
 *Change Request #4
 *Requestor: Ninfa Pendleton - Rapid City, SD
*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: Stacey Estey
*Due August 30, 2010*/


//import needed packages
import java.awt.*;
import javax.swing.*;
import javax.swing.JComboBox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

//declare calculator class
public class Calculator extends JFrame implements ActionListener
 {

private JFrame frame;
private JPanel panelAdder;
private JLabel labelAmount;
private JLabel labelTerm;
private JLabel labelRate;
private JTextField textFieldAmount;
private JTextField textFieldMonthlyPayment;
private JComboBox termList;
private JComboBox rateList;
private JButton buttonCalc;



public Calculator()
 {

  initComponents();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
  setTitle("Mortgage Calculator");
pack();

  // Add Listeners
  buttonCalc.addActionListener(this);
}

public void initComponents()
 {

 	//strings for combo boxes
 String [] termString = {"7", "15", "30"};
 String [] rateString = {"0.535", "0.055", "0.0575"};



 //Initialize Components
 frame = new JFrame();
 panelAdder = new JPanel();
 labelAmount = new JLabel("Amount");
 textFieldAmount = new JTextField();
 labelTerm = new JLabel("Term");
 termList = new JComboBox(termString);
 labelRate = new JLabel("Rate");
 rateList = new JComboBox(rateString);
 textFieldMonthlyPayment = new JTextField();
 buttonCalc = new JButton("Calculate");


 //Set Object Attributes
 textFieldMonthlyPayment.setEditable(false);
 textFieldMonthlyPayment.setColumns(12);
 textFieldAmount.setColumns(8);
 termList.setSelectedIndex(2);
 termList.addActionListener(this);
 rateList.setSelectedIndex(2);
 rateList.addActionListener(this);


 Container contentPane = getContentPane();
 contentPane.setLayout(new FlowLayout());

 // add the components to the panel
 panelAdder.add(labelAmount);
 panelAdder.add(textFieldAmount);
 panelAdder.add(labelTerm);
 panelAdder.add(termList);
 panelAdder.add(labelRate);

 panelAdder.add(rateList);
 panelAdder.add(buttonCalc);
 panelAdder.add(textFieldMonthlyPayment);

 contentPane.add(panelAdder);
}

public static void main(String[] args)
 {
  Calculator frame = new Calculator();
}

private void setMonthlyPaymentValue()
 {
 	DecimalFormat formatter = new DecimalFormat("$#,##0.00");


   double amount = Double.parseDouble(textFieldAmount.getText()); //get user input--mortgage amount
   double termList = Double.toString(termString(termList.getSelectedItem()))* 12 ; //get user input--term of mortgage in years
   double rateList = Double.toString(rateString(rateList.getSelectedItem()))/ 12 ; // get user input--interest rate
   double monthlyPayment = amount * Math.pow(1+rateList, termList) * rateList /(Math.pow(1+rateList , termList)-1); //calculate input display monthly payment amount.

   textFieldMonthlyPayment.setText(" " + formatter.format(monthlyPayment));


 }
 public void actionPerformed(ActionEvent event)
 {

  String command = event.getActionCommand();
  if (command == "Calculate") {
      setMonthlyPaymentValue();
  }

}
}



This is as far as I have got. I am stuck trying to getting these lines of code right so I can move on to trying to get the rest done. Any advice would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Mortgage Calculator GUI Help

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,153
  • Joined: 27-December 08

Re: Java Mortgage Calculator GUI Help

Posted 06 September 2010 - 06:10 PM

The Double.toString() method converts the Double to a String. Then you are multiplying a String by an int, and assigning it to a Double. No need to use Double.toString() at all. Do you mean to use Double.parseDouble() instead?
double termList = Double.toString(termString(termList.getSelectedItem()))* 12 ;


Was This Post Helpful? 0
  • +
  • -

#3 MrsE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 06-September 10

Re: Java Mortgage Calculator GUI Help

Posted 07 September 2010 - 05:58 AM

View Postmacosxnerd101, on 06 September 2010 - 05:10 PM, said:

The Double.toString() method converts the Double to a String. Then you are multiplying a String by an int, and assigning it to a Double. No need to use Double.toString() at all. Do you mean to use Double.parseDouble() instead?
double termList = Double.toString(termString(termList.getSelectedItem()))* 12 ;


That is how i had it to start with, I have changed it back, but I still get the same error when I compile.
/** Mortgage Calculator Program
 * Week 2 PRG421
 *Change Request #4
 *Requestor: Ninfa Pendleton - Rapid City, SD
*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: Stacey Estey
*Due August 30, 2010*/


//import needed packages
import java.awt.*;
import javax.swing.*;
import javax.swing.JComboBox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

//declare calculator class
public class Calculator extends JFrame implements ActionListener
 {

private JFrame frame;
private JPanel panelAdder;
private JLabel labelAmount;
private JLabel labelTerm;
private JLabel labelRate;
private JTextField textFieldAmount;
private JTextField textFieldMonthlyPayment;
private JComboBox termList;
private JComboBox rateList;
private JButton buttonCalc;



public Calculator()
 {

  initComponents();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
  setTitle("Mortgage Calculator");
pack();

  // Add Listeners
  buttonCalc.addActionListener(this);
}

public void initComponents()
 {

 	//strings for combo boxes
 String [] termString = {"7", "15", "30"};
 String [] rateString = {"0.535", "0.055", "0.0575"};



 //Initialize Components
 frame = new JFrame();
 panelAdder = new JPanel();
 labelAmount = new JLabel("Amount");
 textFieldAmount = new JTextField();
 labelTerm = new JLabel("Term");
 termList = new JComboBox(termString);
 labelRate = new JLabel("Rate");
 rateList = new JComboBox(rateString);
 textFieldMonthlyPayment = new JTextField();
 buttonCalc = new JButton("Calculate");


 //Set Object Attributes
 textFieldMonthlyPayment.setEditable(false);
 textFieldMonthlyPayment.setColumns(12);
 textFieldAmount.setColumns(8);
 termList.setSelectedIndex(2);
 termList.addActionListener(this);
 rateList.setSelectedIndex(2);
 rateList.addActionListener(this);


 Container contentPane = getContentPane();
 contentPane.setLayout(new FlowLayout());

 // add the components to the panel
 panelAdder.add(labelAmount);
 panelAdder.add(textFieldAmount);
 panelAdder.add(labelTerm);
 panelAdder.add(termList);
 panelAdder.add(labelRate);

 panelAdder.add(rateList);
 panelAdder.add(buttonCalc);
 panelAdder.add(textFieldMonthlyPayment);

 contentPane.add(panelAdder);
}

public static void main(String[] args)
 {
  Calculator frame = new Calculator();
}

private void setMonthlyPaymentValue()
 {
 	DecimalFormat formatter = new DecimalFormat("$#,##0.00");


   double amount = Double.parseDouble(textFieldAmount.getText()); //get user input--mortgage amount
   double termList = Double.parseDouble(termString(termList.getSelectedItem()))* 12 ; //get user input--term of mortgage in years
   double rateList = Double.parseDouble(rateString(rateList.getSelectedItem()))/ 12 ; // get user input--interest rate
   double monthlyPayment = amount * Math.pow(1+rateList, termList) * rateList /(Math.pow(1+rateList , termList)-1); //calculate input display monthly payment amount.

   textFieldMonthlyPayment.setText(" " + formatter.format(monthlyPayment));


 }
 public void actionPerformed(ActionEvent event)
 {

  String command = event.getActionCommand();
  if (command == "Calculate") {
      setMonthlyPaymentValue();
  }

}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1