|
|
||
| You are here: | ||
|
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 /** 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. |
|
|
|||
|
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. 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 |