Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 14 December 2012

JAVA Program: Lottery Class

Posted on 18:14 by Unknown
Here's another JAVA Program I want to share with you guys, which is a demonstration of the Lottery class I was asked to design from my copy of Tony Gaddis' textbook.

Below is the code for the class:

import java.util.Random;

public class Lottery
{
    private int[] lotteryNumbers;
    private int matches;
   
    public Lottery()
    {
        final int maxNumber = 10;
       
        Random numberGenerator = new Random();
       
        lotteryNumbers = new int[5];
       
        for (int x = 0; x < lotteryNumbers.length; x++)
        {
            lotteryNumbers[x] = numberGenerator.nextInt(maxNumber);
        }
    }
   
    public void lotteryCheck(int[] playersNumbers)
    {
        int numberOfMatches = 0;
       
        for(int y : playersNumbers)
        {
            if (playersNumbers[y] == lotteryNumbers[y])
                numberOfMatches++;
        }
       
        matches = numberOfMatches;
    }
   
    public int getNumberOfMatches()
    {
        return matches;
    }
   
    public int[] getLotteryNumbers()
    {
        return lotteryNumbers;
    }
   
    public void noWin()
    {
        System.out.println("Sorry, you did not win the grand prize.");
    }
   
    public void grandPrizeWinner()
    {
        System.out.println("Congratulations! All of your numbers match the lottery; you win the grand prize!");
    }
}




Over here is the code that executes the class' methods:

import java.util.Scanner;

public class LotteryProgram
{
    public static void main(String[] args)
    {
        boolean safeEntry = true;
        final int arraySize = 5;
       
        int[] playerNumbers = new int[arraySize];
        int[] returnNumbers;
       
        Scanner Keyboard = new Scanner(System.in);
       
        System.out.println("This program will run a check for winners of a five-element lottery game. " +
                           "If all of your numbers matches the draw numbers, you win the big prize.");
       
        System.out.println("\nFor each element, enter a number between 0 and 9 according to your paper.\n");
       
        for (int x = 0; x < playerNumbers.length; x++)
        {
            System.out.println("Element " + (x + 1) + ":");
            playerNumbers[x] = Keyboard.nextInt();
           
            if (playerNumbers[x] < 0 || playerNumbers[x] > 9)
            {
                System.out.println("Invalid number. Please run the program again and enter a digit that is between 0 and 9.");
                x = 5;
                safeEntry = false;
            }
        }
       
        if (safeEntry)
        {
            Lottery drawNumbers = new Lottery();
           
            System.out.println("The numbers from the draw are as follows:");
            returnNumbers = drawNumbers.getLotteryNumbers();
           
            for (int p = 0; p < returnNumbers.length; p++)
            {
                System.out.print(returnNumbers[p]);
            }
           
            System.out.println();
           
            if (drawNumbers.getNumberOfMatches() == 5)
                drawNumbers.grandPrizeWinner();
            else
                drawNumbers.noWin();
        }
    }
}


Thanks for reading!
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • The Black Glove [Short Poem by me]
    This is a poem written by me back in April 2010. I'm not sure if this was for an assignment, but this is what I got. It may not be good ...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • Princess Peach In India [Story Idea]
    Here's a really fancy story I have made, although it is more of an initial idea and comes from a visual I had more than two years ago. E...
  • Super Mario Script 1 (v 2.3) [Super Mario Galaxy font, with a few photos]
    Hello there. Do you recognize the font of this text? I'll give you two clues. The one below is the a sample sentence of different sizes....

Blog Archive

  • ►  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ►  June (12)
    • ►  May (12)
    • ►  April (2)
    • ►  January (1)
  • ▼  2012 (24)
    • ▼  December (13)
      • Need some help for other holidays to come
      • JAVA Program: Coin Toss
      • JAVA Program: Lottery Class
      • JAVA Program: Car Class
      • String Letter Counter (JAVA Program)
      • Fun with Music, Drama and Comedy
      • JAVA Program - City Water Company Bill [The use of...
      • Inequality in Canadian Women through Politics and ...
      • Super Mario Bros: The Fighter Attack [Game Idea ma...
      • Princess Peach In India [Story Idea]
      • The Black Glove [Short Poem by me]
      • Longueuil RTL #83 (Sec. 5 English Short Story)
      • Super Mario Script 1 (v 2.3) [Super Mario Galaxy f...
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile