Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!