Solved

My First Applet

Posted on 1998-07-07
7
215 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());

      }

}
0
Comment
Question by:n2chiles
  • 4
  • 3
7 Comments
 
LVL 2

Expert Comment

by:aziz061097
ID: 1225223
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
 
LVL 2

Expert Comment

by:aziz061097
ID: 1225224
// 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
 

Author Comment

by:n2chiles
ID: 1225225
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:aziz061097
ID: 1225226
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
 

Author Comment

by:n2chiles
ID: 1225227
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
 
LVL 2

Accepted Solution

by:
aziz061097 earned 200 total points
ID: 1225228
//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
 

Author Comment

by:n2chiles
ID: 1225229
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now