b) Adding Integers:
J Addition Program
// File: Addition java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Addition extends Applet implements ActionListener
{
Label prompt; // Message that prompts user to input
‘TextField input; // Input values are entered here
number; // Variable that stores the input value
int sum; _// Variable that stores sum of integers
/ Set up the GUI components & Initialize variables
public void init)
{
prompt = new Label( “Enter integer and press Enter” );
add( prompt );_ // Put prompt on applet
input = new TextField( 10 ); // Width of the text field on the screen
add( input );_// Put input YextField on applet =
sum = 0;
11 “this? applet handles action events for TextField input
input.addActionListener( this );
}
1 Process user’s aetion in TextField input
public void actionPerformed( ActionEvent ¢)
{
1/ Get the number & convert it to an integer
number = Integer.parsefnt( e.getActionCommand() );
sum = sum + number;
input.setText(“*); // Clear data entry field
showStatus( Integer:toString( sum ));_// Display sum
}
dill
2
Ref #7
Same
Keywords
Java Keywords
abstract go implements private throw
boolean double import: protected throws:
break else instanceof public transient:
byte extends int return true
case false interface short ery
catch final long static void
char finally native super volatile
class float new switch while
continue for null synchronized
default if package this
y
Note — In Java, the literals true, false, and null are lowercase, not
uppercase as in the C++ language. Strictly, these are-not keywords but
literals. The distinction is academic, however.
Egg sega gs ess edge Es
Note — There is no sizeof operator; the size and representation of all
types is fixed and is rot implementation dependent.
Note — goto and const are actually keywords, but are not used in
Java.
24 Introduction to Java Programming
Copyright 1998 Sun Mierosystems, ne. AU Rights Reserved. Jan. 1998