Solved

My First Applet

Posted on 1998-07-07
7
229 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split string containing \r\n in Java 46 44
Way to decrease size of apk file 9 69
jdbc error in jsp application 20 45
Facing this issue for maven proxy setting 2 12
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

777 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