1.11M Members

java applet

 
0
 
import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.awt.event.*;


public class SchoolApplet8 extends JApplet implements ActionListener {


    double userAmount;
    double userInterest;
    double userTerm;


    int month = 0;
    double remainBalance = 0;
    double interestPayment = 0;
    double principalPaid = 0;

    JPanel row1;
    JLabel lblAmount;
    JTextField txtAmount;
    JLabel lblInterest;
    JTextField txtInterest;
    JLabel lblTerm;
    JTextField txtTerm;
    JLabel lblMonthlyPayments;
    JTextField txtMonthlyPayments;
    JLabel lblPayment;
    JTextField txtPayment;

    JPanel row2;
    JLabel lblHeader;
    JTextArea txtResults;
    JScrollPane textPane;


    JPanel row3;
    JButton calculateButton;
    JButton clearButton;
    JButton exitButton;



    public void init()
    {



    //Instantiate GUI Components

    // row1
    row1 = new JPanel();

    lblAmount = new JLabel(" Loan Amount: $", JLabel.LEFT);
    txtAmount = new JTextField(10);

    lblMonthlyPayments =  new JLabel("Monthly Payments", JLabel.LEFT);
    txtMonthlyPayments =  new JTextField(6);


    lblInterest = new JLabel("Interest Per Period %", JLabel.LEFT);
    txtInterest = new JTextField(6);

    lblTerm = new JLabel("Term in Years", JLabel.LEFT);
    txtTerm = new JTextField(6);



    // TO DO
    // create a new JLabel and assign it to lblPayment
    // create a new JTextField(10) and assign it to txtPayment



    // row2
    row2 = new JPanel(new BorderLayout());

    String pad = "                             ";
    lblHeader = new JLabel("Payment #"+pad+"Monthly Payment Amount:"+pad+"Interest Paid:"+pad+"Principal"+pad+"Balance:");

    txtResults = new JTextArea(10, 53);
    textPane = new JScrollPane(txtResults);



    //row 3
    row3 = new JPanel(new GridLayout(1,3,75,1));

    calculateButton = new JButton("Calculate");
    clearButton = new JButton("Clear");
    exitButton = new JButton("Exit");



    //Build GUI

    setSize(600, 250);


    row1.add(lblAmount);
    row1.add(txtAmount);

    row1.add(lblMonthlyPayments);
    row1.add(txtMonthlyPayments);

    row1.add(lblInterest);
    row1.add(txtInterest);

    row1.add(lblTerm);
    row1.add(txtTerm);




    // To DO
    // add lblPayment & txtPayment to row1
    // set editable of txtPayment to false: txtPayment.setEditable(false);
    //



    textPane.setPreferredSize(new Dimension(610,200));
    row2.add(lblHeader,BorderLayout.NORTH);
    row2.add(textPane,BorderLayout.CENTER);



    //Create Calculate button and add listener
    calculateButton.addActionListener(this);
    clearButton.addActionListener(this);
    exitButton.addActionListener(this);




    row3.add(exitButton);
    row3.add(clearButton);
    row3.add(calculateButton);
    getContentPane().add(row1,BorderLayout.NORTH);
    getContentPane().add(row2,BorderLayout.CENTER);
    getContentPane().add(row3,BorderLayout.SOUTH);


    }





    //Define actions performed for buttons
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == calculateButton)
        {


            double balance = 0;
            double monthPayment=0;



            userAmount = Double.parseDouble(txtAmount.getText());

            userInterest = Double.parseDouble(txtInterest.getText());

            userTerm = Double.parseDouble(txtTerm.getText());



            //formats answer in currency format then to string format for display
            NumberFormat fmt = NumberFormat.getCurrencyInstance();
            monthPayment = CalculateMonthlyPayment();
            String payment = fmt.format(monthPayment);

            // TO DO
            // Display payment in txtPayment : txtPayment.setText(" "+ payment);
            //



            balance = userAmount;
            month = (int)userTerm * 12;

            for(int index = 1; index <= month ; index++)
                {
                interestPayment = balance * (userInterest * .01/12);
                principalPaid = monthPayment - interestPayment;
                remainBalance = balance - principalPaid;


                txtResults.append(" " + index + "\t\t "+ fmt.format(balance) + "\t\t"
                + fmt.format(interestPayment) + "\t\t"  + fmt.format(remainBalance) + "\n");

                balance = remainBalance;
                }


        }

        else if (e.getSource() == clearButton)
        {
            txtAmount.setText(null);
            txtPayment.setText(null);
            txtResults.setText(null);
        }

        else if (e.getSource() == exitButton)
        {
            System.exit(0);
        }
    }


    //Mortgage Payment Calculations
    double CalculateMonthlyPayment()
    {

        return (userAmount * ((1 + (userInterest*.01)/12)-1) / (1-(Math.pow((1+(userInterest*.01)/12),-(userTerm*12)))));

    }



}
 
0
 

Do you have any questions or comments for the posted code?

 
0
 

Ya sorry thought I put them in. Ya I need assistance comming up with the code for caculating the interest / so when a user puts in loan amount/Interest rate/terms and monthly payments the interest will be calculated.

Any help would be greatly appreciated/ thanks

 
0
 

It does compile and when I print it out in html in a web form it keeps generating the calculations using 35,000 / so when you push clear it only clears the top fields / but when you put in a different loan amount it won't register still uses the old one.

 
0
 

code for caculating the interest / so when a user puts in loan amount/Interest rate/terms and monthly payments the interest will be calculated.

Can you explain what the code does now when it executes and what the problems are with it?
What do you input to the program and what does it output?
Do you have the formula for calculating interest?

For testing can you preload all the needed values into the text fields so that the only operation required for testing is to press a button?

 
0
 

It does compile and when I print it out in html in a web form it keeps generating the calculations using 35,000 / so when you push clear it only clears the top fields / but when you put in a different loan amount it won't register still uses the old one.

 
0
 

Are there any error messages?
For testing can you preload all the needed values into the text fields so that the only operation required for testing is to press a button?
for example:

     txtAmount = new JTextField("35000", 10);

Also posted at: http://www.javaprogrammingforums.com/java-applets/17823-interest-calculator-applet.html#post76182

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: