We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

My First Applet

n2chiles
n2chiles asked
on
Medium Priority
281 Views
Last Modified: 2013-11-23
I am using JDK 1.1.6 to learn Java Programming, but just reading from a book I am having a hard time getting my first applet to work.  I want to make an applet with three labels/text fields.  The input will be for calculating simple interest on a bank account.  So, balance, rate and years left in account.  I can get the labels and text fields to work, but I can not understand how to get the data to loop through an iteration to display back to the applet what the year breakdown and new balance (balance + Interest) will be.

Here is the code that I have (w/o comments):

import java.applet.Applet;      
import java.awt.*;                  
import java.awt.event.*;      

public class Balance extends Applet implements ActionListener {
      Label balLabel;                  
      TextField balBox;
      Label rateLabel;                  
      TextField rateBox;            
      Label yearsLabel;                  
      TextField yearsBox;            
      double balance;                        
      double newbalance;            
      double year;                        
      double interest;                  
      double rate;

      
      public void init()

      {

            setBackground(Color.gray);
            setForeground(Color.black);
            setFont(new Font("SansSerif", Font.BOLD, 12));

            balLabel = new Label("Enter Initial Balance:");
            add(balLabel);      

            balBox = new TextField(10);
            add(balBox);       

            rateLabel = new Label("Enter Interest Rate:  ");
            add(rateLabel);      

            rateBox = new TextField(10);
            add(rateBox);       

            yearsLabel = new Label("Enter Total Years:    ");
            add(yearsLabel);            

            yearsBox = new TextField(10);
            add(yearsBox);             

            
            balBox.addActionListener(this);
            rateBox.addActionListener(this);
            yearsBox.addActionListener(this);

      }

      
      public void actionPerformed(ActionEvent e)
      {

      
            balance = Double.valueof(balBox.getText());
            rate = Double.valueof(rateBox.getText());
            year = Double.valueof(yearsBox.getText());

      }

}
Comment
Watch Question

The solution is to have a button and add the actionlistener on the button and do a settext with the new value. I will update your program and post it in a short while.
// I added a button (you can position it if it is not OK)
// and added the actionlistener to the button

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Balance extends Applet implements ActionListener {
Label balLabel;
TextField balBox;
Label rateLabel;
TextField rateBox;
Label yearsLabel;
TextField yearsBox;
double balance;
double newbalance;
double year;
double interest;
double rate;
Button Calculate;


public void init()

{

setBackground(Color.gray);
setForeground(Color.black);
setFont(new Font("SansSerif", Font.BOLD, 12));

balLabel = new Label("Enter Initial Balance:");
add(balLabel);

balBox = new TextField(10);
add(balBox);

rateLabel = new Label("Enter Interest Rate:  ");
add(rateLabel);

rateBox = new TextField(10);
add(rateBox);

yearsLabel = new Label("Enter Total Years:    ");
add(yearsLabel);

yearsBox = new TextField(10);
add(yearsBox);

Calculate = new Button("Calculate");
add(Calculate);

Calculate.addActionListener(this);
//balBox.addActionListener(this);
//rateBox.addActionListener(this);
//yearsBox.addActionListener(this);

}


public void actionPerformed(ActionEvent e)
{


balance = Double.valueOf(balBox.getText()).doubleValue();
rate = Double.valueOf(rateBox.getText()).doubleValue();
year = Double.valueOf(yearsBox.getText()).doubleValue();

balance = balance + (balance *year * rate /100.0) ; //(PTR / 100) if I am not wrong
balBox.setText(Double.toString(balance));
rateBox.setText("");
yearsBox.setText("");
}

}

Author

Commented:
I must explain further - what if more than one year is entered?  I don't see how this will iterate for each year - calculate the new interest and then add the interest to the balance - which creates a new balance - that is multiplied by the interest rate again and then added to the new balance which creates a newbalance again... until the last year iterates through the loop and then the yeay and new balance are displayed back on the applet at the bottom under the lables and textfields like example below

YEAR     BALANCE
1             $1100.00
2             $1250.00
3             $1375.00
4             $1500.00
5             $1650.00

This is more of what I had in mind - I can't understand how to get to the iteration point...

Thanks though your answer was nice...

map
This can be easily done if you take a TextArea and append text to it after calculating the intermediate values. The user can also scroll through the list if a scroll bar is provided. Remember to make the TextArea not editable. Do you need help in doing this ?

Author

Commented:
Aziz,

I would appreciate your help in doing this.  I am not very confident and my frustration level is very high at this point...  I thought that a for loop or array might be need to automatically loop through the number of years and write it back to the TextArea - and yes, I would appreciate your in help in doing this - I learn best by seeing and my frustration level is to the point where I am ready to give up - but I hate to quit because I feel that I am close to getting over the mental block that is holding me back...

Thanks mate...

map
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Aziz,

Thanks,  I can see now that I was missing the increment to the itertion loop (Dooh!)

I see that you have asked some Win 95 related questions give me a holler - I am a little better with HTML - and OS related stuff - I am trying to teach myself Java to be a better web developer - it is hard for a non-programmer...

Thanks again,

n2chiles@seatac.net

map
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.