Calculate the monthly payment on a loan Sample Console Welcome to the Loan Calcu
ID: 3561797 • Letter: C
Question
Calculate the monthly payment on a loan
Sample Console
Welcome to the Loan Calculator
DATA ENTRY
Enter loan amount: ten
Error! Invalid decimal value. Try again.
Enter loan amount: -1
Error! Number must be greater than 0.0
Enter loan amount: 100000000000
Error! Number must be less than 1000000.0
Enter loan amount: 500000
Enter yearly interest rate: 5.6
Enter number of years: thirty
Error! Invalid integer value. Try again.
Enter number of years: -1
Error! Number must be greater than 0
Enter number of years: 100
Error! Number must be less than 100
Enter number of years: 30
FORMATTED RESULTS
Loan amount: $500,000.00
Yearly interest rate: 5.6%
Number of years: 30
Monthly payment: $2,870.39
Continue? (y/n):
Error! This entry is required. Try again.
Continue? (y/n): x
Error! Entry must be 'y' or 'n'. Try again.
Continue? (y/n): n
Operation
Explanation / Answer
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.*;
import java.awt.event.*;
public class TestCalc{
static JFrame window;
static JPanel p;
static JLabel label1, label2;
static JTextField JTextField1, JTextField2;
static JButton b;
static JTextArea textArea;
static JScrollPane scrollPane;
static ButtonHandler handler;
public static void main(String args[])
{
window = new JFrame(); // create window
p = new JPanel();
//Create the first label.
label1 = new JLabel("Loan Amount", JLabel.LEFT);
p.add( label1);
//Set the position of its text, relative to its icon:
label1.setVerticalTextPosition(JLabel.BOTTOM);
label1.setHorizontalTextPosition(JLabel.LEFT);
// construct textfield with 10 columns
JTextField1 = new JTextField( 10 );
p.add( JTextField1 );
//Create the other labels.
label2 = new JLabel("Number of Years");
p.add(label2);
// construct textfield with default text
JTextField2 = new JTextField(3);
p.add( JTextField2 );
label1.setToolTipText("A label containing both image and text");
label2.setToolTipText("A label containing only text");
b = new JButton("Show Table" );
p.add(b);
textArea = new JTextArea(20, 40);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
p.add( scrollPane );
// create new ButtonHandler for button event handling
handler = new ButtonHandler();
b.addActionListener( handler );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.add(p);
window.setSize( 520, 360); // set frame size
window.setVisible( true ); // display frame
} // end main
// inner class for button event handling
private static class ButtonHandler implements ActionListener
{
// handle button event
public void actionPerformed( ActionEvent event )
{
String temploan = JTextField1.getText();
double principle;
int lifeofloan;
textArea.setText(temploan );
}
}
} // end class TestCalc
import javax.swing.JOptionPane; // include the JOptionPane library in swing
public class MonPay // class header
{ // begin class
public static void main(String args[ ] ) // main method header
{ // begin main method
System.out.println("Welcome to the Loan Calculator "); // display program title
String strAmount, strIRate, strYears; // declare variables to be entered by user as strings
double amount, iRate, years, monPay, totalPay; // declare variables to be used in the formula
strAmount =
JOptionPane.showInputDialog(" Enter the amount of the Loan $: "); // enter amount as a string
strIRate =
JOptionPane.showInputDialog("Enter the % interest rate: "); // enter % interest as a string
strYears =
JOptionPane.showInputDialog("Enter the # of years: "); // enter years as a string
amount = Double.parseDouble(strAmount); // convert the input string strAmount to number
iRate = Double.parseDouble(strIRate); // convert the input string strIRate into a real number
years = Double.parseDouble(strYears); // convert the input string strYears into a real number
System.out.println("Enter the amount of the Loan $: " + amount + " "); // echo input
System.out.println("Enter the % interest rate: " + iRate +" ");
System.out.println("Enter the # of years: " + years +" ");
monPay = (amount * iRate / 1200) /
( 1 - Math.pow( 1 / ( 1 + iRate / 1200), 12 * years)) ; // calculate payment
monPay = (int)(monPay * 100 + 0.5 ) / 100.0; // round distance to the nearest 1/100
totalPay = monPay * 12 * years; // calculate the total pay back for the loan
totalPay = (int)(totalPay * 100 + 0.5 ) / 100.0; // round total to the nearest 1/100
System.out.println("The monthly payment is : $" + monPay +" "); // display monthly payment
System.out.println("The total amount paid is : $" + totalPay +" " ); // display total payment
System.exit( 0 ); // exit the system to close all windows
} // end main method
} // end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.