Integer Demo

In this example we see how integer variables are used.

By convention variable names are in lower case. In this example the variables are declared in the main class, which makes them available to all the methods in that class. These are referd to as global variables.

The init() method is part of the applet package and it is used to set up the initial conditions of a class.

Breaking up a class into separate methods like this makes it easier to read and debug.

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

public class IntegerDemo extends Applet
{

//Globally declare two integers and variable names for their sum, difference,
//product, quotient and remainder.
int n, m, sum, diff, prod, quot, remainder;
public void init()
{

n = 25;
m = 6;
sum = n + m;
diff = n - m;
prod = n * m;
quot = n / m;
remainder = n % m;

}
public void paint (Graphics g)
{

g.drawString("The original values are " + n + " and " + m, 25, 25);
g.drawString("The sume is " + sum, 25, 50);
g.drawString("The difference is " + diff, 25, 75);
g.drawString("The product is " + prod, 25, 100);
g.drawString("The quotient is " + quot, 25, 125);
g.drawString("The remainder is " + remainder, 25, 150);

}

}

This is the output

integerdemo.JPG
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License