©1994-2003 Kevin Boone
Home     Section index     K-Zone home Simple Java programs

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
Software development
Computing
Hello1.java
Add1_1.java
AwtTest.java
Button1.java
Button2.java
Calculator.java
CalculatorApp.java
ClockApplet.java
CountSpace1.java
DateTime.java
Div1.java
DoNothing.java
Factorial1.java
Factorial2.java
FontChooser.java
IfTest.java
Inheritance1.java
Loan.java
DiceThrower.java
MultiplicationTable.java
Queue.java
Reverse1.java
SoundApplet.java
Stressometer.java
TextReaderTest.java
WebMerge.java
Zener.java
`Add1' example program: add two numbers

Add1.java

// Add1.java
// A program that adds 2 and 2 
// Kevin Boone, May 1999

// Like most programs that use applets, we need the following
//  two lines to tell the compiler what an applet is
//  and how it works. 

import java.applet.Applet;
import java.awt.*;

// As in all Java programs we have to define a class. I am
//  calling this class `Add1', so this program must be
//  in a file called `Add1.java' 

public class Add1 extends Applet
{
public void paint (Graphics g)
	{
	// ``int'' is short for `integer', meaning a whole number.
	// So this line defines a thing called ``result'' which stores
	//  the result of adding 2 and 2.
	int result = 2 + 2;
	
	// The next line displays the text ``2+2='' on the screen
	g.drawString ("2+2=", 20, 20);

	// This line displays the result of the addition on the
	//  next line.
	g.drawString (Integer.toString(result), 20, 40);
	}
}