FileDocCategorySizeDatePackage
Lottery.javaAPI DocExample3652Wed Feb 21 12:47:52 GMT 2001None

Lottery

public class Lottery extends Applet

Fields Summary
private int
circleXPosition
private int
circleYPosition
private int
textXPosition
private int
textYPosition
private int[]
numbers
private int[]
lotteryNumbers
private int
number_equal
String
winnings
Constructors Summary
Methods Summary
private voidcheckNumbers()

		for (int yourNumber = 0; yourNumber < numbers.length; yourNumber++)
		{
			for (int lottery = 0; lottery < lotteryNumbers.length; lottery++)
			{
				if (numbers[yourNumber] == lotteryNumbers[lottery])
				{
	         number_equal++;
	      	}
		      
			}  // end of lottery loop
		}  // end of yourNumber loop
	
	
private voiddetermineResults()

		switch(number_equal)
		{
			case 0:
			case 1:
			case 2: winnings = "Sorry - out of luck today";
					  break;
			case 3: winnings = "Hey - small £10 winnings";
					  break;
			case 4: winnings = "Things are looking up - £100";
					  break;
			case 5: winnings = "Smile - you've just won £10,000";
					  break;
			case 6: winnings = "Celebrate - you've just won £100,000";
					  break;
			default:winnings = "Error - number_equals is " + number_equal + " out of range";
					  break;
			}
	
private voiddisplayLotteryNumbers(java.awt.Graphics g)

		for (int index = 0; index < lotteryNumbers.length; index++)
		{
	  		g.setColor(Color.blue) ;
			g.drawOval(circleXPosition,circleYPosition,33,33) ;
			g.setColor(Color.red);
			g.drawString("" + lotteryNumbers[index], textXPosition,textYPosition);
			circleXPosition = circleXPosition + 40 ;
			textXPosition = textXPosition + 40 ;
		}
	
private voiddisplayNumbers(java.awt.Graphics g)

		textXPosition   = 14 ;	
		textYPosition   = 82 ;
			// now add your numbers below the lottery balls
			
		for (int index = 0; index < numbers.length; index++)
		{
				g.drawString("" + numbers[index], textXPosition, textYPosition);
				textXPosition = textXPosition + 40;	
		}
	
public voidinit()

		numbers = new int[6];
		lotteryNumbers = new int[7];
	  	circleXPosition = 1 ;	
		circleYPosition = 20 ;
		textXPosition   = 12 ;	
		textYPosition   = 42 ;
	   number_equal = 0;       	
	
private voidinitialiseArray(int[] numArray)

			for (int index = 0; index < numArray.length; index++)
		{
			numArray[index] = (int) (Math.random() * 49) + 1;
		}
	
public voidpaint(java.awt.Graphics g)

		
		initialiseArray(lotteryNumbers); // set up lottery numbers
		initialiseArray(numbers);			// set up your numbers
		sortArray(lotteryNumbers);
		sortArray(numbers);
		checkNumbers();						// compare your numbers with the lottery ones
		displayLotteryNumbers(g);			// print out the lottery number values
		displayNumbers(g);						// print out your numbers
		determineResults();					// check to see if you have won anything
		
		textYPosition = 102;
		textXPosition = 12;
		g.drawString(winnings,textXPosition,textYPosition);
		
	
private voidsortArray(int[] numArray)

		int temp;
		
		for (int outer = 0; outer < numArray.length - 1; outer++)
		{
			for (int inner = outer + 1; inner < numArray.length; inner++)
			{
				if (numArray[outer] > numArray[inner])
				{
					temp            = numArray[outer];
					numArray[outer] = numArray[inner];
					numArray[inner] = temp;
				}
			}
		}