Question

I HAVE A PROBLE WITH THIS JAVA PROBLEM CAN ANYONE HELP ME PLEASE.Exercise 2 Write a program that simulates the Texas Powerball lottery. You will have to do research to find out how many numb

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//class powerball
public class Powerball {
   /*
   * first play method to compare the passed numbers to compare random generated
   * numbers
   */
   public static String play(int... numbers) {
       Random rand = new Random();
       List<Integer> guessedNumbers = new ArrayList<>();
       String str;
       int count = 0;
       for (int num : numbers) {
           if (num == 1) {
               int random_number = rand.nextInt(num + 1);
               if (num == random_number) {
                   count++;
                   guessedNumbers.add(num);
               }
           } else {
               int random_number = rand.nextInt(num);
               random_number += 1;
               if (num == random_number) {
                   count++;
                   guessedNumbers.add(num);
               }
           }

       }
       if (count > 0) {
           str = "\"Congratulations, you guessed " + count + " numbers correct : ";
           for (Integer i : guessedNumbers) {
               str += i + ",";
           }
           str = str.substring(0, str.length() - 1) + "\"";
       } else {
           str = "Sorry !, Unfortunately you didn't win";
       }
       return str;
   }

   /*
   * second play method to take another input boolean which if passed as true,
   * performs the number number drawing over and over again until there occurs a
   * drawing in which all the players numbers match the randomly generated numbers
   */ public static String play(boolean input, int... numbers) {
       Random rand = new Random();
       String str;
       int count = 0;
       if (input == true) {
           for (int num : numbers) {
               int random_number = rand.nextInt(num);
               random_number++;
               if (random_number == num) {
                   count++;
               } else {
                   count++;
                   while (random_number != num) {
                       random_number = rand.nextInt(num);
                       random_number++;
                       count++;
                   }

               }
           }
           str = count + " many times drawing is necessary to match user's numbers";
       } else {
           List<Integer> guessedNumbers = new ArrayList<>();
           for (int num : numbers) {
               if (num == 1) {
                   int random_number = rand.nextInt(num + 1);
                   if (num == random_number) {
                       count++;
                       guessedNumbers.add(num);
                   }
               } else {
                   int random_number = rand.nextInt(num);
                   random_number += 1;
                   if (num == random_number) {
                       count++;
                       guessedNumbers.add(num);
                   }
               }

           }
           if (count > 0) {
               str = "\"Congratulations, you guessed " + count + " numbers correct : ";
               for (Integer i : guessedNumbers) {
                   str += i + ",";
               }
               str = str.substring(0, str.length() - 1) + "\"";
           } else {
               str = "Sorry !, Unfortunately you didn't win";
           }

       }

       return str;

   }

   public static void main(String[] args) {

      
       String str = play(1,3,4,8);
       System.out.println(str);
         
       String output = play(true, 1,3,4,8);
       System.out.println(output);

   }

}

Output

Eile Edit ource Refactor Navigate Search Project Bun Window Help Quick Access! : E | | Console X terminated> Powerbell leve A

Add a comment
Know the answer?
Add Answer to:
Exercise 2 Write a program that simulates the Texas Powerball lottery. You will have to do resear...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program...

    Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program should have an array of 5 integers named lottery and should generate a random number in the range of 1 through 99 for each element of the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that...

  • c++ please help You are going to create a PowerBall Lottery game with functions. First, you...

    c++ please help You are going to create a PowerBall Lottery game with functions. First, you are going to generate 5 UNIQUE numbers between 1 and 69. Then you are going to generate the powerball number, which is a number between 1 and 26. Next you are going to ask the user for 5 numbers. Users should only be allowed to enter numbers in the range 1-69 for regular numbers, 1-26 for powerball pick. You will compare each of the...

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

  • Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an...

    Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an array of six integers named lotteryNumbers, and another array of six integers named userLotteryPicks. The class' constructor should use the Random class to generate a unique random number in the range of 1 to 60 for each of the 6 elements in the lotteryNumbers array. Thus, there should be a loop that keeps generating random numbers until all 6 numbers are unique.  The Lottery class...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery...

    Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery game, the player picks a set of numbers. A random drawing of numbers is then made, and the player wins if his/her chosen numbers match the drawn numbers (disregarding the order of the numbers). More specifically, a player picks k distinct numbers between 1 and n (inclusive), as well as one bonus number between 1 and m (inclusive). "Distinct" means that none of the...

  • (Microsoft Visual Source, C++, Course: CIS-5, Intro to Programming) Write a program that starts a player...

    (Microsoft Visual Source, C++, Course: CIS-5, Intro to Programming) Write a program that starts a player off with a bank of $15.00. A coin will flip and randomly choose heads or tails.The user will guess heads or tails to win. If the coin flip matches the player's guess his bet will be doubled. It costs 1 dollar to play and the program will bet that amount automatically each time as long as there is the available bank amount. Note: Do...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user...

    question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user to enter the number of times n to roll a six-sided die. The program should print to the screen the iteration of the loop and result for each roll 1 ) How many times would you like to roll? 3 roll 6 6 5 1 2) PE 05b 02 (Flash Cards) Write a counter-controlled program that creates addition problems for an elementary kid to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT