Lab Assignment 8

Fall Semester, 2006

 

Outcomes:

 

Upon completion of the lab, the student will know how to

 

·        Create and manipulate one- and two-dimensional arrays in Java.

 

·        Use the Java Random class to generate pseudorandom integers.

 

·        Use methods of the class Arrays to sort arrays of primitive types.

 

1

Download the following files to your J: drive from Mr. Clark’s website:

·        PowerBall.java

 

2

In this lab you will create a Java class that

·        generates Powerball EZPick plays

·        checks a Powerball play against the Powerball numbers to determine the amount, if any, won by the player.

 

Here is some background information about the Powerball game.

 

Powerball™ is a multistate lottery game in which the player selects 5 numbers between 1 and 55, inclusive (called the white balls) and 1 number between 1 and 42, inclusive (called the red ball).  On Wednesday and Saturday nights the Powerball numbers are drawn and cash amounts are awarded on the following basis:

 

Number of matching white balls

Matching red ball

Winnings

 

5

yes

jackpot

 

5

no

200000

 

4

yes

10000

 

4

no

100

 

3

yes

100

 

3

no

7

 

2

yes

7

 

1

yes

4

 

0

yes

3

 

0

no

0

 

 

 

3

First, let’s develop the method generatePick, whose skeleton looks like this:

public int[] generatePick()
{
   int[] pick = new int[6];

   int number;

   int index = 0;

 

   /* generate 5 unique random white balls
      in the range 1..55 */


   /* sort the white balls in ascending order */
  
Arrays.sort(pick, 0, 5);


   /* generate a red ball in the range 1..42 */

 

   return pick;

}

 

The white balls should occupy elements 0 – 4 of the array picks, and the red ball should occupy element 5.  To help with this problem, here are some hints:

 

·        To generate a random integer between 0 and 9, you could use the code:

   number = rand.nextInt(10);

Generating a number between 1 and 10 can be accomplished by adding 1 to the result.

 

·        Pseudcode for generating unique white balls (this uses nested loops, and not necessarily for loops):

for index in the range 0 … 4 do the following:
   number = random value in range 1 … 55
   set a boolean variable, say unique, to true
   for p in the range 0 … index-1 do the following:
      is pick[p] == number?
         if so, set unique to false;
         otherwise, increment p

       if unique is true, set pick[index++] = number

 

·        The statement

Arrays.sort(pick, 0, 5);

sorts the values of pick (from index == 0 to 4) in ascending order.  This static method is one of many methods provided by the Java Arrays class.

 

When you have this method completed, test it with the following main method:


    public static void main(String args[])

    {

      PowerBall p = new PowerBall();   

      int[] myPicks;

      for (int n = 0; n < 10; n++)

      {        

         myPicks = p.generatePick();

         for (int k = 0; k < 6; k++)

         {          

            System.out.print(" " + myPicks[k]);

         }

         System.out.println();

      }

    }

 

The output of this program should look something like this.  Your numbers should be different because of the way they were generated:

2 8 24 36 46 0

7 18 21 25 40 37

11 23 34 36 48 41

9 19 33 40 54 11

18 25 28 29 49 28

2 6 7 10 32 19

3 8 26 30 53 21

11 19 25 37 50 29

2 4 10 31 39 37

1 20 44 50 54 6

 

When you get output that looks correct, show your code and output to the lab instructor.  Do not proceed to the next step until you have done so.

 

4

Write the code for the static method calculateWinnings, whose skeleton looks like this:

    public static int calculateWinnings(int[] pick, int[] drawing)

    {

      int winnings = 0;

 

      /* calculate number of matching white balls */

      int whiteCount = 0;

 

      /* for each white ball in pick ... */

      for (int i = 0; i < 5; i++)

      {

         /* for each in white ball in drawing */

         for (int j = 0; j < 5; j++)
         {

            /* see if pick[i] matches drawing[j] */

            /* if so, increment whiteCount and break out of loop

         }

      }

     

      int redCount = 0;

      /* see if red ball of pick matches that of drawing */

     

 

      /* determine winnings */

 

      /* 5 white and 1 red => JACKPOT */

      if (whiteCount + redCount == 6)

      {

         return jackPot;

      }

     

      /* 5 white and 0 red => 200000 */

     

      /* 4 white and 1 red => 10000 */

     

      /* 4 white and 0 red or 3 white and 1 red => 100 */

      

      /* 3 white and 0 red or 2 white and 1 red => 7 */

     

      /* 1 white and 1 red => 4 */

     

      /* 0 white and 1 red => 3 */

     

      /* if none of the above, nothing */

      return 0;

   }

 

You will need to flesh out the code above and then use the following main method to test your program. 

 

   public static void main(String args[])

    {

 

       int[][] myPicks = {

         {1,2,3,4,5,6},   // jackpot

         {1,2,3,4,5,7},   // 200000

         {2,3,4,5,7,6},   // 10000

         {1,2,3,4,6,7},   // 100

         {2,3,5,8,9,6},   // 100

         {1,2,5,8,9,7},   // 7

         {4,5,7,8,9,6},   // 7

         {1,7,8,9,10,6},  // 4

         {7,8,9,10,11,6}, // 3

         {7,8,9,10,11,1}  // 0

      };

     

      int[] drawing = {1,2,3,4,5,6}; 

     

      for (int n = 0; n < 10; n++)

      {       

         for (int k = 0; k < 6; k++)

         {          

            System.out.print(" " + myPicks[n][k]);

         }

         System.out.println("\twins "

            + PowerBall.calculateWinnings(myPicks[n], drawing));

      }

    }

 

Your output should look exactly like this:

 

 1 2 3 4 5 6    wins 100000000

 1 2 3 4 5 7    wins 200000

 2 3 4 5 7 6    wins 10000

 1 2 3 4 6 7    wins 100

 2 3 5 8 9 6    wins 100

 1 2 5 8 9 7    wins 7

 4 5 7 8 9 6    wins 7

 1 7 8 9 10 6   wins 4

 7 8 9 10 11 6  wins 3

 7 8 9 10 11 1  wins 0

 

When you are getting output that looks correct, show your code and output to the lab assistant/instructor. 

 

5

Print a listing of your code and get the lab assistant/instructor to sign it.  Hand this in for lab credit.