Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Page Title

AddMe

We are going to modify the PressMe basic code a little bit to create an Applet that adds two numbers together.

As for PressMe, start a new Class of type Appletsimple (if you are using BlueJ, otherwise copy and paste).

Refer back to PressMe.html for the meaning of uncommented lines.

 

AddTwo is the name I chose for this Class. It has to be in a file called AddTwo.java to compile!

Now we have two text fields, the button and label have altered text, otherwise the same.

Each of the text fields will hold a number to be added.

The answer will appear in the greeting Label.

 

We add the new text fields to the Applet.

 

 

 

Text Strings cannot be added together so the text has to be converted to a number first.

parseInt is a method in the Integer Class to convert text to whole numbers.

AddMe - an Applet that adds two (whole) numbers together.

Source code: AddTwo.java

 

The code:

// <<<<<<<<<<<<<<<  this indicates line which have changed from PressMe.java


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
* Adds two numbers together!
* Lines changed from PressMe
* are indicated with this symbol: // <<<<<<<<<<<<<<<
*
* @author Mr J
* @version 281003
*/
public class AddTwo extends Applet implements ActionListener
{
   private TextField number1 = new TextField(20);     
// <<<<<<<<<<<<<<<
   private TextField number2 = new TextField(20);     
// <<<<<<<<<<<<<<<
   private Button pressMe = new Button("Add");        
// <<<<<<<<<<<<<<<
   private Label greeting = new Label("The answer");  
// <<<<<<<<<<<<<<<

  /**
   * Add objects to the Applet
   */

   public void init()
   {
     add(number1); // <<<<<<<<<<<<<<<
     add(number2); // <<<<<<<<<<<<<<<
     add(greeting);
     add(pressMe);
     pressMe.addActionListener(this);
   }
   /**
   * When the button is pressed, do the calculation
   *
   * @param e carries details about the event that occurred
   */

   public void actionPerformed(ActionEvent e)
   {
     // assume user types in whole numbers only
     int x, y; // int is for whole numbers         // <<<<<<<<<<<<<<<
     x = Integer.parseInt(number1.getText());      // <<<<<<<<<<<<<<<
     y = Integer.parseInt(number2.getText());      // <<<<<<<<<<<<<<<
     greeting.setText("Answer: " + (x + y));       // <<<<<<<<<<<<<<<
   }
}

Modifications you might like to experiment with:

You can modifiy the Applet to add doubles (real numbers, eg 2.445).

You can use panels to improve layout.

Related: [ Java Home | previous:methods | next:Java arithmetic ]

You can get the BlueJ IDE from http://bluej.org if you need it.

If no Applet appears you can get the JRE from http://www.java.com.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009