Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please assist i ' m trying to run the following source code on Apache NetBeans IDE 2 1 software so i can take screenshots for

Please assist i'm trying to run the following source code on Apache NetBeans IDE 21 software so i can take screenshots for my assisgnement ,it gives me over 30 errors please assist in correcting the errors.
NB. I want all calculations to be in South African Rands.
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_2

Step: 3

blur-text-image_3

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

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions