Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

My First Applet

Posted on 1998-07-07
7
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

596 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