Lottery

image from http://www.casinocenter.com/rules-strategy-lottery/

CS 139 Algorithm Development
PA5: Lottery Checker

Part A - Due: Thursday, April 16 at 11:59

Part B - Due: Sunday, April 19 at 11:59

  • -10% on Monday, April 20
  • -20% on Tuesday, April 21
  • -30% on Wednesday, April 22
  • -40% on Thursday, April 23
  • -50% on Friday, April 24
  • Not accepted afterwards

Part C - Due: Monday, April 20 at 11:59 or 24 hours after successful submission of PartB

Objectives

Students will demonstrate:

Background

Welcome to the world famous JMU Computer Science lottery! In this assignment, you will implement the functionality to support a cash drawing, to be redeemed for fabulous prizes. Your task includes writing methods that: (1) generate a list of random numbers as specified by a user (quantity and range) as an array, (2) compare a "ticket" to the official lottery numbers selected by the contest administrator, and (3) create an array of prizes given the size of the "lottery" and total prize money to award.

As in previous assignments, a Driver class will be provided to illustrate how this application works for a particular lottery. This program is based on Programming Challenges #9 in your Gaddis textbook --- see Chapter 7 (Watermelon edition) or Chapter 8 (Puzzle edition).

Requirements

Start with the following files:

You only need to implement the methods described below, and then test each one individually using JUnit. As before, this PA will be easier if you test it incrementally---as you are building each method---rather than trying to test the whole program using the Driver.

Both Lottery.java and Lottery_Test.java must conform to the CS 139 Style Guide, including documentation. Use checkstyle after you have gotten your program to work and while you are working through the problem.

Some Definitions

PART A

Part A will require that you write the Lottery_Test.java file. You should read the requirements for each method and then think through the test cases that you will need. We are working with static methods, so you do not need to create Lottery objects. This then means that all data that we need to work with will be housed within the LotteryDriver. Note that the generatePicks method takes in a "seed". A seed is a value that is used as a starter for the Random number generation. To generate test data, you will need to see how the Random number generator operates using different seeds. Build a small program including a Random generator, and then look at the resulting "picks". You can use these as examples to use in testing different situations. In the PartB you should get full coverage of your methods including each branch and loop. Taking care now to provide complete test cases will save you time when you get to webcat.

You will submit part A by submitting your test file to Canvas.

PART B

Required Methods

These methods are called directly by LotteryDriver.java and Web-CAT, so they must be implemented exactly as specified. (Hint: copy-and-paste them to avoid any typos.) You may add additional methods if you like.

public static int[] generatePicks(int size, int lowValue, int highValue, long seed)
generatePicks will build an array of the given size, and load it with random numbers from lowValue to highValue inclusively. No number may be used more than once. The resulting array should be sorted in ascending order.

NOTE: In order to properly test this method, we will use a seed value for the random number generator. This will cause the picks to be "random" in a particular predictable sequence. (This technique was also used to generate the dictionary words in PA4). See http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#Random(long) for how this constructor looks.

public static int checkUserNumbers(int[] picks, int[] userNumbers)
checkUserNumbers checks the list of userNumbers against the numbers in the picks array to count how many lottery numbers the user guessed correctly. For example, if the picks are {14, 23, 24, 25, 30} and the userNumbers are {5, 19, 14, 25, 17}, the method should return 2 (because each array in this example contains both 14 and 25).

public static int[] sortArray(int[] unsorted)
sortArray takes in an unsorted array and returns a copy of that array sorted in ascending order. This is a helper method that may be used in generatePicks and/or checkUserNumbers. This method should not change its parameter.

public static int[] generatePrizes(int size, int prizeMoney)
generatePrizes creates an array of prize amounts based on the size of the lottery pick and the total prizeMoney. The array positions correspond to the count of correct numbers in a lottery pick (zero through size, inclusively).

The algorithm for generating prize money is as follows. If a player guesses 0, 1, or 2 numbers correctly the prize amount is 0. The remaining prize money is generated by awarding to each level (starting with correctly guessing all of the numbers) 3/4 of the available prize money, rounded to the nearest dollar, with the last count (3 correct) receiving whatever remains. For example, if the lottery were 6 numbers and the prizeMoney were 1000, 6 correct would get 750, 5 correct would get 3/4 of the remainder or 188, 4 correct would get 3/4 of the remainder or 47, and 3 correct would get the rest, 15. If there were only 3 numbers and 500 in prize money, 3 matching numbers would get all 500. Note that the return value (prizes) array length will be one more than the count of numbers in the lottery (size), since it must include the prize for zero and for size.

Argument Validation

In this assignment, you must check that argument values passed to your methods are appropriate. Specifically: If any of these conditions do not hold, your methods should return null or -1 (depending on the return type).

Hints

You may find some methods in the java.util.Arrays class to be helpful. Note however that the Arrays.sort method does not return a new array. You must implement a copy operation in your own sort method to ensure that you do not alter the unsorted array parameters! The Arrays.equals method will be helpful for testing, in comparing your expected output to the actual output of your methods that generate arrays.

Deliverables

  1. Put Lottery.java and Lottery_Test.java in a zip file, and submit via Web-CAT. Do not include any other java or class files in your zip file. You may find it helpful to run your code through checkstyle to ensure consistency with the class Style Guide.

  2. After you complete the assignment, submit a reflection document via Canvas (10 points).

Honor Code

This assignment should be viewed as a take home exam. Your work on the assignment and your submission must conform to the JMU Honor Code. Authorized help is limited to the lab handouts, the TAs for any CS 139 or CS 239 section, and the professor for your section. Copying work from another student or the Internet is an honor code violation, and will result in a zero on the assignment and possibly further sanctions. The same is true if you give your code to another student or post it on the Internet. Your code may be subject to evaluation by MOSS (Measure of Software Similarity) which is used to detect inappropriate similarities among programs.