Java Applet Program Write a java applet to calculate a monthly loan payment and
ID: 3687052 • Letter: J
Question
Java Applet Program
Write a java applet to calculate a monthly loan payment and the total amount to be paid over the life of a loan.
Ask the user for the amount of the loan, the interest rate, and the number of months of the loan.
The user should press a button called 'Calculate' to perform the calculations and another button called 'Reset' to reset the input and output fields.
If the input values for the amount, interest rate, or months is not valid (contains characters, symbols, or is blank), then the 'Amount of Payment' text box needs to display the message "input ERROR" and the cursor must be automatically placed back into the 'Loan Amount" text box.
The formula for calculating a loan payment is:
(principle*rate/12*Math.pow(rate/12+1, time)) / (Math.pow(rate/12+1, time)-1)
The window dimensions are 270 by 300.
The JTextField size is 8.
Must be a single .java file!
Your output must look like the sample output below.
Appl...- Applet Loan Amount Yearly Interest Rate Number of Loan Months Calculate Reset Amount of Payment Total Payments Applet started.Explanation / Answer
import java.applet.Applet; import java.awt.Button; import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.NumberFormat; /* * */ public class Test extends Applet implements ActionListener { TextField amountText, paymentText, totalText, periodText, rateText; Button doIt, reset; double principal; // original princial double intRate; // interest rate double numYears; // length of loan in years /* * Number of payments per year. You could allow this value to be set by the * user. */ final int payPerYear = 12; NumberFormat nf; public void init() { // Use a grid bag layout. GridBagLayout gbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbag); Label heading = new Label("Compute Monthly Loan Payments"); Label amountLab = new Label("Loan Amount"); Label periodLab = new Label("Number of Loan Months"); Label rateLab = new Label("Yearly Interest Rate"); Label paymentLab = new Label("Amount of payment"); Label totalLab = new Label("Total payments"); amountText = new TextField(8); periodText = new TextField(8); paymentText = new TextField(8); totalText = new TextField(8); rateText = new TextField(8); doIt = new Button("Calculate"); reset = new Button("Reset"); // Define the grid bag. gbc.weighty = 1.0; // use a row weight of 1 gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.NORTH; gbag.setConstraints(heading, gbc); // Anchor most components to the right. gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(amountLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(amountText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(rateLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(rateText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(periodLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(periodText, gbc); gbc.anchor = GridBagConstraints.CENTER; gbag.setConstraints(doIt, gbc); gbc.anchor = GridBagConstraints.CENTER; gbag.setConstraints(reset, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(paymentLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(paymentText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(totalLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(totalText, gbc); // Add all the components. add(heading); add(amountLab); add(amountText); add(rateLab); add(rateText); add(periodLab); add(periodText); add(doIt); add(reset); add(paymentLab); add(paymentText); add(totalLab); add(totalText); // Register to receive action events. amountText.addActionListener(this); periodText.addActionListener(this); rateText.addActionListener(this); doIt.addActionListener(this); reset.addActionListener(this); nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); } /* * User pressed Enter on a text field or pressed Compute. */ public void actionPerformed(ActionEvent ae) { repaint(); if (ae.getActionCommand().equals("Reset")) { amountText.setText(""); rateText.setText(""); periodText.setText(""); paymentText.setText(""); totalText.setText(""); } } // Display the result if all fields are completed. public void paint(Graphics g) { double result = 0.0; String amountStr = amountText.getText(); String periodStr = periodText.getText(); String rateStr = rateText.getText(); try { if (amountStr.length() != 0 && periodStr.length() != 0 && rateStr.length() != 0) { principal = Double.parseDouble(amountStr); numYears = Double.parseDouble(periodStr); intRate = Double.parseDouble(rateStr); result = compute(); paymentText.setText(nf.format(result)); totalText.setText(nf.format(result*numYears)); } showStatus(""); // erase any previous error message } catch (NumberFormatException exc) { showStatus("Invalid Data"); paymentText.setText(""); totalText.setText(""); } } // Compute the loan payment. double compute() { double numer; double denom; double b, e; double a = (principal*intRate/12*Math.pow(intRate/12+1, numYears)) / (Math.pow(intRate/12+1, numYears)-1); return a; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.