Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please assist by running the following source code on Apache NetBeans IDE 2 1 or any Java programming language. While testing, please capture and submit

Please assist by running the following source code on Apache NetBeans IDE 21 or any Java programming language. While testing, please capture and submit screenshots.
public class LoanCalculator extends JFrame {
private JTextField loanAmountField, interestRateField, termField;
private JLabel monthlyPaymentLabel;
private JButton calculateButton, resetButton;
public LoanCalculator(){
createView();
setTitle("Loan Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
setResizable(false);
}
private void createView(){
JPanel panel = new JPanel(new GridBagLayout());
getContentPane().add(panel);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
// Loan Amount
JLabel loanAmountLabel = new JLabel("Loan Amount (R):");
c.gridx =0;
c.gridy =0;
panel.add(loanAmountLabel, c);
loanAmountField = new JTextField();
c.gridx =1;
c.gridy =0;
panel.add(loanAmountField, c);
// Interest Rate
JLabel interestRateLabel = new JLabel("Annual Interest Rate (%):");
c.gridx =0;
c.gridy =1;
panel.add(interestRateLabel, c);
interestRateField = new JTextField();
c.gridx =1;
c.gridy =1;
panel.add(interestRateField, c);
// Loan Term
JLabel termLabel = new JLabel("Loan Term (years):");
c.gridx =0;
c.gridy =2;
panel.add(termLabel, c);
termField = new JTextField();
c.gridx =1;
c.gridy =2;
panel.add(termField, c);
// Calculate Button
calculateButton = new JButton("Calculate");
c.gridx =0;
c.gridy =3;
c.gridwidth =2;
panel.add(calculateButton, c);
calculateButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
calculateMonthlyPayment();
}
});
// Monthly Payment Display
monthlyPaymentLabel = new JLabel("Monthly Payment: ");
c.gridx =0;
c.gridy =4;
c.gridwidth =2;
panel.add(monthlyPaymentLabel, c);
// Reset Button
resetButton = new JButton("Reset");
c.gridx =0;
c.gridy =5;
c.gridwidth =2;
panel.add(resetButton, c);
resetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
resetFields();
}
});
}
private void calculateMonthlyPayment(){
double principal = Double.parseDouble(loanAmountField.getText());
double annualInterestRate = Double.parseDouble(interestRateField.getText());
int termYears = Integer.parseInt(termField.getText());
double monthlyInterestRate = annualInterestRate /100/12;
int totalPayments = termYears *12;
$$
M = P *(r(1+ r)^n)/((1+ r)^n -1)
$$
where:
M = monthly payment
P = principal loan amount
r = monthly interest rate
n = number of payments
double monthlyPayment =(principal * monthlyInterestRate * Math.pow(1+ monthlyInterestRate, totalPayments))/(Math.pow(1+ monthlyInterestRate, totalPayments)-1);
monthlyPaymentLabel.setText("Monthly Payment: R"+ String.format("%.2f", monthlyPayment));
}
private void resetFields(){
loanAmountField.setText("");
interestRateField.setText("");
termField.setText("");
monthlyPaymentLabel.setText("Monthly Payment: ");
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new LoanCalculator().setVisible(true);
}
});
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions

Question

Explain how cultural differences affect business communication.

Answered: 1 week ago

Question

List and explain the goals of business communication.

Answered: 1 week ago