Question

In: Computer Science

Write the program in Java (with a graphical user interface) and have it calculate and display...

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. You need to include Calculate, Reset, and Exit buttons on your GUI. Please insert comments in the program to document the program. Allow the user to enter the values of the loan, term, and rate. Do not add any additional touches, keep it simple.

Solutions

Expert Solution

Hii, I have answered similar questions many times before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// MortgageCalculator.java

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class MortgageCalculator extends JFrame implements ActionListener {

      // declaring needed UI components

      private JTextField loanAmtTxt, intRateTxt, yearsTxt, resultTxt;

      private JButton calculate, reset, exit;

      // constructor

      public MortgageCalculator() {

            // using a grid layout with any number of rows and 2 columns

            setLayout(new GridLayout(0, 2));

            // adding labels and text fields in proper order

            add(new JLabel("Enter loan amount: "));

            loanAmtTxt = new JTextField();

            add(loanAmtTxt);

            add(new JLabel("Enter interest rate (%): "));

            intRateTxt = new JTextField();

            add(intRateTxt);

            add(new JLabel("Enter number of years: "));

            yearsTxt = new JTextField();

            add(yearsTxt);

            add(new JLabel("Monthly payment: "));

            resultTxt = new JTextField();

            resultTxt.setEditable(false);

            add(resultTxt);

           

            //creating buttons and adding action listeners

            calculate = new JButton("Calculate");

            calculate.addActionListener(this);

            reset = new JButton("Reset");

            reset.addActionListener(this);

            exit = new JButton("Exit");

            exit.addActionListener(this);

           

            //creating a Panel and adding buttons to the panel

            JPanel buttons = new JPanel();

            buttons.add(calculate);

            buttons.add(reset);

            buttons.add(exit);

           

            //adding panel to frame

            add(buttons);

            //compact size

            pack();

            //exit on close button click

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            //making window visible

            setVisible(true);

      }

      public static void main(String[] args) {

            //initializing GUI

            new MortgageCalculator();

      }

      @Override

      public void actionPerformed(ActionEvent e) {

            //finding source of action

            if (e.getSource().equals(calculate)) {

                  try {

                        //extracting values

                        double loan = Double.parseDouble(loanAmtTxt.getText());

                        double interestRate = Double.parseDouble(intRateTxt.getText());

                        int years = Integer.parseInt(yearsTxt.getText());

                       

                        //finding monthly interest rate and then monthly payment

                        double monthlyInterestRate = getMonthlyInterest(interestRate);

                        double monthlyPayment = getMonthlyPayment(loan, years,

                                    monthlyInterestRate);

                        //updating result text

                        resultTxt.setText(String.format("$%.2f", monthlyPayment));

                  } catch (Exception ex) {

                        //error occurred

                        resultTxt.setText("Invalid input");

                  }

            } else if (e.getSource().equals(reset)) {

                  //clearing all text fields

                  loanAmtTxt.setText("");

                  yearsTxt.setText("");

                  intRateTxt.setText("");

                  resultTxt.setText("");

            } else if (e.getSource().equals(exit)) {

                  //exit

                  System.exit(0);

            }

      }

      /**

      * method to calculate the monthly interest, by passing annual interest rate

      *

      * @param interestRate

      *            - rate of interest in percentage

      */

      private double getMonthlyInterest(double interestRate) {

            double monthlyInterest = ((interestRate / 100) / 12);

            return monthlyInterest;

      }

      /**

      * method to find the monthly payment rate, by passing loan amount, total

      * number of years and monthly interest rate

      */

      private double getMonthlyPayment(double loanAmt, int numYears,

                  double monthlyInterest) {

            /**

            * Applying the formula to find the monthly payment rate

            *

            * P = L[c(1 +c)^n]/[(1 + c)^n - 1]

            *

            * (fixed monthly payment (P) required to fully amortize a loan of L

            * dollars over a term of n months at a monthly interest rate of c)

            */

            int numMonths = numYears * 12;

            double monthlyPaymentRate = loanAmt * monthlyInterest

                        * (Math.pow(1 + monthlyInterest, numMonths))

                        / (Math.pow(1 + monthlyInterest, numMonths) - 1);

            return monthlyPaymentRate;

      }

}

/*OUTPUT*/



Related Solutions

write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
*****Using Java Write a program that finds the standard deviation while also using a graphical user...
*****Using Java Write a program that finds the standard deviation while also using a graphical user interface.
Write a program to use Math methods to calculate and display the followings: Prompts the user...
Write a program to use Math methods to calculate and display the followings: Prompts the user to enter an angle, in degrees, and then display the angle in radians and the Sine of the angle. Prompts the user to enter a positive integer and calculates the square root of the number. Computational thinking: Your program must check if it is a positive number. If a negative number is entered, it must ask for another number until the positive number is...
Write a program in java that uses methods to input data for calculation, calculate, and display...
Write a program in java that uses methods to input data for calculation, calculate, and display the results of the calculations. That is, there are at least three methods. The problem is to write a program that calculates the area of a rectangle. This action should be repeatable.
Java program Statement: Provide a user interface to the invoice program in Section 12.3 that allows...
Java program Statement: Provide a user interface to the invoice program in Section 12.3 that allows a user to enter and print an arbitrary invoice. Do not modify any of the existing classes. ..... ..... ..... /** Describes an invoice for a set of purchased products. */ public class Invoice { /** Adds a charge for a product to this invoice. @param aProduct the product that the customer ordered @param quantity the quantity of the product */ public void add(Product...
Java - Write a program to calculate a user’s BMI and display his/her weight status. The...
Java - Write a program to calculate a user’s BMI and display his/her weight status. The status is defined as Underweight, Normal, Overweight and obese if BMI is less than 18.5, 25, 30 or otherwise respectively. You need to read weight (in kilogram) and height (in metre) from the user as inputs. The BMI is defined as the ratio of the weight and the square of the height. [10 marks]
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
Use the below info to create a java program A GUI interface to ensure a user...
Use the below info to create a java program A GUI interface to ensure a user is old enough to play a game. Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this. Instructions to ensure that the information is displayed back...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT