Computers(Java Programming)

Service Class:

Lottery

Write a Lottery class that simulates a lottery. The class should define an array of five integers named lotteryNumbers. The constructor should use the Random class (see the Java API) to generate a random number in the range of 0 to 50 for each element on the array such that each element contains a unique value. The class's match method should accept an array of five integers that represents a person's lottery picks and compares the array elements to the lotteryNumbers array. The method is to return the number of matching digits. For example, the following shows a randomly generated lotteryNumbers array and the user's array with sample numbers stored in each.

lotteryNumbers array

17 24 19 21 33

When the user's picks are

24 20 19 17 6

the match method returns 3 since 24, 19, and 17 are found within the lotteryNumbers array.

When the user's picks are

33 17 5 2 8

the match method returns 2 since 33 and 17 are found within the lotteryNumbers array.

When the user's picks are

11 6 15 24 0

the match method returns 1 since only the number 24 was found within the lotteryNumbers array.

In addition, the class should define a toString method which returns a copy of the lotteryNumbers array in an easy to understand String format.

Class UML diagram:

Lottery

- lotteryNumbers : int[]
+ match(int []) : int
+ toString() : String



Client Application:

Demonstrate the Lottery class in an application that generates an instance of the Lottery class and then allows the end user to check one or more tickets against the generated lottery numbers. The client application should allow the end user to enter five numbers, which are stored in an array; there is no need to verify the input values. The program should display the lottery numbers and the number of digits of the end user's ticket that matched. If all of the digits match, display a message proclaiming the user a grand prize winner. Any number of lottery tickets may be checked.

Sample run of the application:

Enter your lottery ticket numbers:
24
20
19
17
6


The lottery numbers were 17, 24, 19, 21 and 33.

You matched 3 numbers!

Do you have another ticket to check? (Y or N): Y

Enter your lottery ticket numbers:
11
6
15
24
0


The lottery numbers were 17, 24, 19, 21 and 33.

You matched 1 numbers!

Do you have another ticket to check? (Y or N): Y

Enter your lottery ticket numbers:
21
19
24
33
17


The lottery numbers were 17, 24, 19, 21 and 33.

You matched all 5 numbers! You're the winner of the Grand Prize ... An exclusive, all expense paid trip to AACC. This trip includes a walking tour from east campus to west campus, a tour of the CALT building, and a front row parking space for an entire day!!! Congratulations!

Do you have another ticket to check? (Y or N): N






NOTES:

1.You will be developing TWO classes: the Lottery class and the Client class. The Client class contains the main method and should be named Lab1.
2.There should be no I/O performed in the Lottery class. All interaction with the end user should be done in the Client class.
3.Since the data of the Lottery class is generated by the constructor and can not be changed, accessor and mutator methods for lotteryNumbers are not defined.
4.The Lottery class should be documented using Javadocs. Javadocs are not needed for the client class, although your name should be included within comments at the top of the program.
5.Adhere to the Style Guide for both classes.
6.The grand prize exists in your imagination only (have fun!)
7.Use an initial range of 0 - 9 for the lottery numbers while testing. You'll feel better while debugging!
Evaluation:

Classes must compile without syntax errors or will not be evaluated. This lab is worth 25 points. Points will be distributed as follows:

•Lottery class (18 points)
◦Use of an array of integers
◦Use of the Random class
◦Definition of methods that support the objectives of the class, including a match and toString method
◦Methods produce correct results
◦Methods contain no end-user I/O
◦Code aheres to the style guide
•Client class (6 points)
◦Supports communication with the end-user
◦Main method utilizes the Lottery service class to meet the objectives of the application
◦Code aheres to the style guide

  1. 👍
  2. 👎
  3. 👁
  1. The instructions to the question is very clear and objectives are clearly defined. I assume that you have already taken a Java course before, so you would know how to create a class, the state variables and the methods. Also, I assume you are capable of reading the Java API in order to use the Random class.

    Looking at the evaluation criteria, the Lottery class seems easier to create, and yet it carries 18 points out of 25. It would therefore be a good point to start.

    If you post the source code, I would be glad to give you comments or help you debug if there is a problem. Here's a pseudocode to help you get going. Sorry for the indentation which is removed by the forum's formating.

    public class Lottery
    {
    private int[] lotteryNumbers=new int[5];
    public Lottery() // constructor
    {
    // call method drawNumbers() to create array
    }
    private void drawNumbers()
    {
    // use Random to create numbers within
    range
    // be sure not to generate duplicate numbers
    }
    public int[] getNumbers()
    {
    // return array of 5 numbers
    }
    public int match(int[] picks)
    {
    // match all 5 numbers to the ticket and return the number of matches
    }
    public String toString()
    {
    // return a string which is the concatenation of all the lottery numbers
    }

    }

    1. 👍
    2. 👎
  2. I took Java 1 but really I did not understand. :( I don't even understand this. I have been sitting here and confused going through my books etc. So I have to have 2 classes but what goes where? Thanks so much for the help. I have 24hours and im so stuck...

    1. 👍
    2. 👎
  3. im really stuck. I am taking this online course online. this is what i have so far.

    public class Lottery
    {
    int lotteryNumbers = new int[5];
    public lotteryNumbers();// constructor

    1. 👍
    2. 👎
  4. If you took Java I and did not understand, how did you pass the course?
    What this question requires is expected of Java I students towards the end.
    What I suggest is you redo Java I and make sure you understand every part of it. Java is like math, if you miss one link, you will get stuck in no time.

    Although the Object-Oriented concepts are not completely introduced, but the basic elements of programming are required (learned in Java 1). Your teacher has guided you in every detail, and all he is expecting of you is the coding part. The design is all done.

    Have you looked at my pseudo-code, or skeleton? Can you make something out of it? If you fill in some meat (but make sure it compiles, or you will get a zero), you may salvage some points.

    Try to put some meat in the Lottery class and I will be glad to look over it for you. I will be at school all day tomorrow till the end of the day, so that gives you some time to get the work done!

    Good luck, and bon travail!

    1. 👍
    2. 👎
  5. Thank you...i will post something up when i wake up. Java 1 i took in class-teacher was not the best and being from another country most in the class new more than me. So I had to just try my best. Now im taking Java2 but im doing it online. Im working with a textbook too. Wish i could retake classes but being from another country i pay out of state tution...so :( yes...I have to just give it my best shot...Thanks a lot...

    1. 👍
    2. 👎
  6. If you have time to do practice problems in Java 1 (or 2) and need help, I'll be here. It is important that you understand the basics.

    1. 👍
    2. 👎
  7. Ok, here is what i have done so far. But there are a million errors coming up...I tried my best please help and thanks so much again...

    import java.util.Random;
    import java.io.*;

    public class Lottery
    {
    int[] LottoNumbers = new int[5];
    String LottoString = new string;

    public Lottery()
    {
    Random sequence = new Random();
    for (i = 0; i <= 4; i++){
    LottoNumbers[i] = sequence.nextInt(10) + 1;
    }
    }

    public int Match(int TicketNumbers[])
    {
    int Matches = 0;
    for(i = 0; i <= 4; i++){
    for(j = 0; j <= 4; j++){
    if(LottoNumbers[i] == TicketNumbers[j])
    Matches++;
    }
    }
    return Matches;
    }

    public String toString()
    {
    LottoString = "The lottery numbers were ";
    for (i = 0; i <= 2; i++){
    LottoString = LottoString + LottoNumbers[i] + ", ";
    }
    LottoString = LottoString + LottoNumbers[3] + " and " + LottoNumbers[4] + ".";
    return LottoString;
    }
    }

    class Lab1
    {
    public static void main(String args[])
    {
    String HaveTicket = new String;
    HaveTicket = "Y";
    Lottery NewLottery = new Lottery();
    int[] TicketNumbers = new int[5];
    int Number = new int;
    int Matches = new int;
    InputStreamReader istream = new InputStreamReader(System.in);
    BufferedReader bufRead = new BufferedReader(istream);
    while(HaveTicket == "Y"){
    System.out.println ("Please enter your lottery ticket numbers: ")
    for(i = 0; i <= 4; i++){
    Number = Integer.parseInt(bufRead.readLine());
    TicketNumbers[i] = Number;
    }
    System.out.println (" ");
    System.out.println (Lottery.toString());
    Matches = Lottery.Match(TicketNumbers);
    if(Matches == 5)
    System.out.println ("Congratulations!! You have 5 numbers matched and are the Grand Prize Winner!");
    else
    System.out.println ("You matched " + Matches + " numbers!");
    System.out.println ("Do you have another ticket to check? (Y or N): ");
    HaveTicket = bufRead.readLine();
    }
    }
    }

    1. 👍
    2. 👎
  8. did you get all the source code?
    ill paste again...
    class Lab1
    {
    public static void main(String args[])
    {
    String HaveTicket = new String;
    HaveTicket = "Y";
    Lottery NewLottery = new Lottery();
    int[] TicketNumbers = new int[5];
    int Number = new int;
    int Matches = new int;
    InputStreamReader istream = new InputStreamReader(System.in);
    BufferedReader bufRead = new BufferedReader(istream);
    while(HaveTicket == "Y"){
    System.out.println ("Please enter your lottery ticket numbers: ")
    for(i = 0; i <= 4; i++){
    Number = Integer.parseInt(bufRead.readLine());
    TicketNumbers[i] = Number;
    }
    System.out.println (" ");
    System.out.println (Lottery.toString());
    Matches = Lottery.Match(TicketNumbers);
    if(Matches == 5)
    System.out.println ("Congratulations!! You have 5 numbers matched and are the Grand Prize Winner!");
    else
    System.out.println ("You matched " + Matches + " numbers!");
    System.out.println ("Do you have another ticket to check? (Y or N): ");
    HaveTicket = bufRead.readLine();
    }
    }
    }

    1. 👍
    2. 👎
  9. import java.util.Random;
    import java.io.*;

    public class Lottery
    {
    int[] LottoNumbers = new int[5];
    String LottoString = new string;

    public Lottery()
    {
    Random sequence = new Random();
    for (i = 0; i <= 4; i++){
    LottoNumbers[i] = sequence.nextInt(10) + 1;
    }
    }

    public int Match(int TicketNumbers[])
    {
    int Matches = 0;
    for(i = 0; i <= 4; i++){
    for(j = 0; j <= 4; j++){
    if(LottoNumbers[i] == TicketNumbers[j])
    Matches++;
    }
    }
    return Matches;
    }

    public String toString()
    {
    LottoString = "The lottery numbers were ";
    for (i = 0; i <= 2; i++){
    LottoString = LottoString + LottoNumbers[i] + ", ";
    }
    LottoString = LottoString + LottoNumbers[3] + " and " + LottoNumbers[4] + ".";
    return LottoString;
    }
    }

    1. 👍
    2. 👎
  10. Sorry, pasting does not work here. If you have a file server site, or a photo site, you can post them there. A file server site would be preferable because if it is a .jpg file, I would have to type them up to compile.
    Also, you cannot, theoretically, post links either. If you are ingeneous, you may get around it.

    1. 👍
    2. 👎

Respond to this Question

First Name

Your Response

Similar Questions

  1. Business

    In a frequency distribution, what is the number of observations in a class called? A. class midpoint B. class interval C. class array D. class frequency E. none of the above

  2. statistic

    A lottery ticket has a grand prize of $28 million. The probability of winning the grand prize is .000000023. Based on the expected value of the lottery ticket, would you pay $1 for a ticket? Show your calculations and reasoning

  3. stats 101

    how do you find the lower class limit,upper class limit, class width, class midpoint, and class boundaries from a set of frequencies data

  4. algebra 1

    A charity fair raised $6,000 by selling 500 lottery tickets. There were two types of lottery tickets: A tickets cost $10 each, and B tickets cost $60 each. How many tickets of each type were sold?

  1. Math

    In the main draw of a lottery, six of the balls (which are numbered from 1 to 49) are selected at random, then the numbers chosen are rearranged and displayed in ascending numerical order. For instance: 3 17 20 29 34 45 In one

  2. Math (Stats)

    What is the difference between class limits and class boundaries? A) Class limits are the numbers that separate classes without forming gaps between them. Class boundaries are the least and greatest numbers that can belong to the

  3. math urgent

    The state lottery (which has a 6% lottery tax) offers to pay winnings in 25 annual payments or one lump sum, sometimes called a cash-out option. This week’s lottery has a jackpot of $30 million and a cash-out value of $18.2

  4. math help!

    A lottery game asks you to pick 5 numbers from the set 1, 2, 3, ... , 29, 30. How many ways are there to play this lottery game? (Assume that this is a game where the order of your picks does NOT matter.) A) 27,405 B) 84,825 C)

  1. Math

    In a certain lottery, five different numbers between 1 and 30 inclusive are drawn. These are the winning numbers. To win the lottery, a person must select the correct 5 numbers in the same order in which they were drawn. What is

  2. English

    In the short story 'The Lottery,' should I capitalize lottery when writing about it: EX- The winnings of the Lottery were undesired by all townsfolk.

  3. maths

    lisa won the lottery she used 1/2 of her lottery win to buy a house she gave 1/6 of her lottery win to charity lisa then shared the remainder of her lottery win equally amongst her 4 children what is the fraction of lisa's lottery

  4. Art

    based on their attire, playing music during leisure time is most likely a pursuit of which greek class citizen? lower class musical class upper class middle class i believe it is upper class

You can view more similar questions or ask a new question.