FileDocCategorySizeDatePackage
RandomNos.javaAPI DocExample1724Fri Aug 11 13:39:18 BST 2000None

RandomNos

public class RandomNos extends Applet

Fields Summary
private int
randomNumber
private int
textXPosition
private int
textYPosition
private double
sum
private double
average
Constructors Summary
Methods Summary
public voidpaint(java.awt.Graphics g)

		textXPosition = 0 ;  // initialise position of text
		textYPosition = 50 ;
		sum = 0.0 ;				// initialise sum to zero
		
		// define an int, count, and initialise it to one. Repeat the for
		// loop until count = 10, and increment count by one each time
		// round the loop.
		for (int count = 1 ; count <= 10 ; count++)
		{
			// get a value for the random number
			randomNumber  = (int) (Math.random() * 100) + 1; 
		
			// add the random number to sum
			sum = sum + randomNumber ;
			
			// write the random number
			g.setColor(Color.blue);
			g.drawString("" + randomNumber, textXPosition, textYPosition);
									
			// increment postion coordinates
			textXPosition = textXPosition + 30 ;
		}
		
		// calculate average
		average = sum / 10 ;
		
		g.drawString("The average of these numbers is: " + average, 0, 100) ;