• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

My First Applet

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

      }

}
0
n2chiles
Asked:
n2chiles
  • 4
  • 3
1 Solution
 
aziz061097Commented:
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.
0
 
aziz061097Commented:
// 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("");
}

}

0
 
n2chilesAuthor 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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
aziz061097Commented:
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 ?
0
 
n2chilesAuthor 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
0
 
aziz061097Commented:
//Note that I have added a TextArea and made the year as int
//instead of double which is more logical. I have set
//the TextArea to be 5 rows and 30 columns and have a Vertical
//scroll bar only and not be editable. Reset the TextArea
//whenever you display a new computation

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;
int year;
double interest;
double rate;
Button Calculate;
TextArea balList;


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

balList = new TextArea("",5,30,TextArea.SCROLLBARS_VERTICAL_ONLY);
balList.setEditable(false);
add(balList);

Calculate.addActionListener(this);

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

}


public void actionPerformed(ActionEvent e)
{
balList.setText("");
balance = Double.valueOf(balBox.getText()).doubleValue();
rate = Double.valueOf(rateBox.getText()).doubleValue();
year = Integer.valueOf(yearsBox.getText()).intValue();
for(int i = 1;i<=year;i++){
balance = balance + (balance *year * rate /100.0) ; //(PTR / 100) if I am not wrong
balList.append(Double.toString(balance)+"\n");
}
}

}

0
 
n2chilesAuthor 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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now