Loan Calculator

Only available on StudyMode
  • Download(s) : 325
  • Published : April 13, 2012
Open Document
Text Preview
Using Responsibility-driven design concepts, construct an online Java application (i.e. a Java applet to be rendered on a web-browser) that asks user for input amount of the loan, the interest rate and the number of years. The program validates the input checking for valid numeric data before continuing. If the interest rate was entered in percentage form it converts to decimal before calculation process. The program then calculates the monthly payment. The values are displayed rounded to 2 decimal places. The output for the program is the user defined input values as well as the calculated monthly payment. All the input values are accepted from the keyboard. The program uses the following formula:

[Payment = (InterestRate*LoanAmount/12)/(1.0 – Math.pow(((InterestRate/12)+1.0), (-(12*Years))))]. The final output can be displayed when all the inputs and calculations are complete. Java applet for computing loan payments embedded in an HTML page. The user can use the applet to compute the loan payments.

[
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;

/*
* <applet code="Loan calculator" width=280 height=200> </applet> */

public class LoanCalculator extends Applet implements ActionListener {

TextField rateText, paymentText, periodText,amountText, totalpaymentText ;

Button doIt;

double loanamount; // original princial

double intRate; // interest rate

double numYears; // length of loan in years

double totalpayment;
/*
* 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("This is a loan calculator.");

Label rateLab = new Label("Annual Interest Rate");
Label periodLab = new Label("Number of Years");
Label amountLab = new Label("Loan Amount");
Label paymentLab = new Label("Monthly Payments");
Label totalpaymentLab = new Label("Total Payment");

amountText = new TextField(16);
periodText = new TextField(16);
paymentText = new TextField(16);
rateText = new TextField(16);
totalpaymentText = new TextField(16);
// Payment field for display only.
paymentText.setEditable(false);

doIt = new Button("Compute");

// 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(periodLab, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbag.setConstraints(periodText, gbc);

gbc.gridwidth = GridBagConstraints.RELATIVE;
gbag.setConstraints(rateLab, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbag.setConstraints(rateText, gbc);

gbc.gridwidth = GridBagConstraints.RELATIVE;
gbag.setConstraints(paymentLab, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbag.setConstraints(paymentText, gbc);

gbc.gridwidth = GridBagConstraints.RELATIVE;
gbag.setConstraints(totalpaymentLab, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbag.setConstraints(totalpaymentText, gbc);...
tracking img