[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-04-21
5
Medium Priority
?
171 Views
Last Modified: 2010-03-31
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(loanPrincipal);
            inputPanel.add(RateApr);
            inputPanel.add(loanRate);
            inputPanel.add(Years);
            inputPanel.add(loanTerm);

            //add components to button panel
            buttonPanel.add(CalcPayment);
            buttonPanel.add(reset);
            buttonPanel.add(exit);

            //add componets to answer
            answerPanel.add(MonthPmt);
            answerPanel.add(loanPayment);

            container.add(inputPanel, BorderLayout.WEST);
            container.add(buttonPanel, BorderLayout.EAST);
            container.add(answerPanel, BorderLayout.SOUTH);

            //add listeners
            CalcPayment.addActionListener(this);
            reset.addActionListener(this);
            exit.addActionListener(this);
            loanPayment.setEditable(false);

      }//End MortgageCalc



      public void actionPerformed(ActionEvent 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(tPrincipal);
                        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(NumberFormatException f)
                  {
                        JOptionPane.showMessageDialog(null, "Invalid Entry, Please Try Again.", "Error", JOptionPane.INFORMATION_MESSAGE);
                  }

                  // calculations
                  rateMo = (rateApr /12)/100;
                  iMonths = (iYears * 12);

                  monthPmt = (principal * rateMo) / (1- Math.pow(1 + rateMo, - iMonths));
                  DecimalFormat twoDigits = new DecimalFormat("$#,###.00");

                  loanPayment.setText(twoDigits.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.setDefaultCloseOperation(JFrame.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(new FlowLayout());

            JPanel answerPanel = new JPanel();
            answerPanel.setLayout(new FlowLayout());

            // add compenents to greeting panel
            greetingPanel.add(greeting);
            greetingPanel.add(nameField);
            greetingPanel.add(nameEntry);

            //add components to input panel
            inputPanel.add(Principal);
            inputPanel.add(loanPrincipal);
            inputPanel.add(rateAPR);
            inputPanel.add(loanRate);
            inputPanel.add(years);
            inputPanel.add(loanTerm);

            //add components to button panel
            buttonPanel.add(calcPayment);
            buttonPanel.add(calcAmort);
            buttonPanel.add(reset);
            buttonPanel.add(exit);

            //add components to answer panel
            answerPanel.add(payment);
            answerPanel.add(loanPayment);

            //add panels to frame
            container.add(greetingPanel, BorderLayout.NORTH);
            container.add(inputPanel, BorderLayout.WEST);
            container.add(buttonPanel, BorderLayout.EAST);
            container.add(answerPanel, BorderLayout.SOUTH);

            // add listeners to buttons
            calcPayment.addActionListener(this);
            calcAmort.addActionListener(this);
            reset.addActionListener(this);
            exit.addActionListener(this);
            nameEntry.addActionListener(this);
            loanPayment.setEditable(false);
      }

      public void actionPerformed(ActionEvent 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(totPrincipal);
                        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(NumberFormatException f)
                  {
                        JOptionPane.showMessageDialog(null, "Invalid Entry, please try again.", "Error", JOptionPane.INFORMATION_MESSAGE);
                  }

                  // 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(twoDigits.format(dMonthPmt));
            }

            else if (source == calcAmort)
            {

                  JOptionPane.showMessageDialog(null, "Under Construction", "Error", JOptionPane.INFORMATION_MESSAGE);
                  /*String Principal = loanPrincipal.getText();
                  String rateAPR = loanRate.getText();
                  String years = loanTerm.getText();

                  try
                  {
                        double dPrincipal = Double.parseDouble(Principal);
                        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(dPrincipal, dRateMo, dMonthPmt, iMonths);
                        if (dPrincipal <= 0.0 || dRateMo <= 0.0 || dMonthPmt <= 0.0 || iMonths <= 0)
                        throw new NumberFormatException();

                  }

                  catch(NumberFormatException f)
                  {
                        JOptionPane.showMessageDialog(null, "Invalid Entry, please try again.", "Error", JOptionPane.INFORMATION_MESSAGE);
                  }
                  */

            }

            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.setDefaultCloseOperation(JFrame.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
0
Comment
Question by:richmp95
  • 3
3 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 2000 total points
ID: 16513622
for the first
apply these changes
remove "double" keyword behind the three vars
and define dRateAPR and iYears out of try block

      double dRateAPR=0, iYears=0;
      try
      {
            dPrincipal = Double.parseDouble(totPrincipal);
            if (dPrincipal <= 0.00) throw new NumberFormatException();
            dRateAPR = Double.parseDouble(rateAPR);
            if (dRateAPR <= 0.00) throw new NumberFormatException();
            iYears = Double.parseDouble(years);
            if (iYears <= 0.0) throw new NumberFormatException();

      }

the square you got was in fact caused by a devide by zero in the method getPayment because all three parameters passed to it were zero because you did not assign these variables correctly
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16513628
sorry that was for the second one (Mortgage)
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16513632
and the first one (MortgageCalc) should be

   double rateApr=0, iYears=0;
   try
   {
            principal = Double.parseDouble(tPrincipal);
            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();
   }
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 4 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question