Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Interest calculator applet

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Interest calculator applet

    Sorry to bother everyone but I'm having an issue trying to find or help myself with the code to implement calculating interest. Basically a user has to be able to input loan amount/interest %/ terms of the loan and loan payments. My issue is the code for calculating the interest. Here is what i have so far.

    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)))));
     
    	}
     
     
     
    }

    Any help would greatly be appreciated Thanks
    Last edited by helloworld922; September 22nd, 2012 at 07:00 PM. Reason: please use [code] tags


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Simple Applet Troubles

    Quote Originally Posted by bruizer View Post
    Sorry to bother everyone but I'm having an issue trying to find or help myself ...
    @bruizer: I'm not a moderator on this forum, but most forums that I've been involved with frown on a poster hijacking another poster's thread. Your question is important, so why not create a new thread for it and ask it there? If you need to reference this thread, then provide a link to it.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Applet Troubles

    Well figured it states applet troubles / and creating new threads and clogging up others posting I figured to keep it within the same category or topic / thanks though no worries. I'll just keep trying on my own thx.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Interest calculator applet

    For future reference, the best course of action when asking a question is a new thread. If you feel the need you can link to the other thread(s) in your thread. I've moved this for you.

    Also, please use code tags to highlight your code:

    [code=java]
    // your code goes here
    [/code]

    This looks like:

    // your code goes here

    You said you're having problems calculating the interest. What kind of problems are you having? Are you getting a compiler error? Incorrect results? The more info you provide the easier it is for us to help you. If you have a compile error, please post the error message.


    p.s.:

    There's no need to apologies about having a question, that's the main purpose of this forum

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Interest calculator applet

    Also posted at java applet | DaniWeb
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Working on a code to calculate interest rates...
    By ColeTrain in forum Java Theory & Questions
    Replies: 7
    Last Post: September 20th, 2012, 06:49 AM
  2. Age Calculator
    By Sagittarian in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 1st, 2012, 08:06 PM
  3. Replies: 29
    Last Post: May 18th, 2012, 02:16 PM
  4. investment interest display problem
    By df75douglas in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 4th, 2011, 07:32 AM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM