Tuesday
March 28, 2017

Post a New Question

Posted by on .

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

  • Computers(Java Programming) - ,

    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
    }

    }

  • Computers(Java Programming) - ,

    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...

  • Computers(Java Programming) - ,

    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

  • Computers(Java Programming) - ,

    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!

  • Computers(Java Programming) - ,

    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...

  • Computers(Java Programming) - ,

    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.

  • Computers(Java Programming) - ,

    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();
    }
    }
    }

  • Computers(Java Programming) - ,

    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();
    }
    }
    }

  • Computers(Java Programming) - ,

    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;
    }
    }

  • Computers(Java Programming) - ,

    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.

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question