Question

In: Computer Science

rite a Java Program to play your version of the Texas Pick 3 Lottery. You will...

rite a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program.

Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number.

  1. In the main() method, declare an int named myPick3choice.
  2. Create a prompt for myPick3choice as "Enter your Pick 3 choice:0-999".
  3. Create a void method named playPick3 that has an int parameter named myPick3.
  4. In the playPick3 method, do the following:
    1. Create an int variable called playCounter and initialize it to 0.
    2. Create an int variable called myPick3Result.
    3. Create a loop that calls the method officialPlayPick3 (Provided below) and store the result in myPick3Result.
      1. If myPick3Result matches the variable myPick3, then display the result of playcounter as "PlayCounter = ...." and exit the loop.
      2. If myPick3Result does not match, increment the variable playCounter and continue the loop.

Enter your Pick 3 choice 0-999: 554

PlayCounter = 1343   ( This result will vary )

Include the code below in your program. The code also requires the following import line:

import java.util.concurrent.ThreadLocalRandom;

/**
* officialPlayPick3()
* Returns a random number from 0 to 999
* @return int
*/

public static int officialPlayPick3() {


      final int MIN_RANGE_VALUE = 0;

final int MAX_RANGE_VALUE = 999;

       return ThreadLocalRandom.current().nextInt(MIN_RANGE_VALUE,MAX_RANGE_VALUE + 1);
}

Also Create a loop in the main method to allow you to play as often as you wish.

Solutions

Expert Solution

Texas_Pick_3_Lottery.java

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

class Texas_Pick_3_Lottery {
    public static int officialPlayPick3() {


        final int MIN_RANGE_VALUE = 0;

        final int MAX_RANGE_VALUE = 999;

        return ThreadLocalRandom.current().nextInt(MIN_RANGE_VALUE,MAX_RANGE_VALUE + 1);
    }
    public static void main(String[] args) {
        int myPick3choice;
        System.out.print("Enter your Pick 3 choice:0-999 : ");
        Scanner sc = new Scanner(System.in);
        myPick3choice = sc.nextInt();
        playPick3(myPick3choice);
    }

    private static void playPick3(int myPick3) {
        int playCounter = 0;
        int myPick3Result;
        while (true){
            myPick3Result = officialPlayPick3();
            playCounter ++;
            if(myPick3==myPick3Result){
                System.out.print("PlayCounter = " + playCounter);
                break;
            }
        }
    }
}

Output :

Explanation :

Class Texas_Pick_3_Lottery :

main() :

  • I have created an integer variable "myPick3choice" to store the choice of the user.
  • System.out.print("Enter your Pick 3 choice:0-999: "): It tells the user to enter its choice between 0 to 999 by displaying the message on the screen.
  • Scanner sc = new Scanner(System.in) : It creates Scanner object "sc" to take user input from console.
  • myPick3choice = sc.nextInt() : It takes Integer input from the user through console and assigns it to the variable  "myPick3choice".
  • playPick3(myPick3choice): This method is called to print the number of attempts taken to match the random number so far generated with the choice of the user.
int officialPlayPick3() : 
  • This method is used to return the random number generated between lower limit (0) and the upper limit (1000).
  • It will return the number between 0 to 999 i.e inclusive 0 and exclusive 1000.
  • This method is inserted the same as defined in the question.
void playPick3(int myPick3) : 
  • This method is used for printing the number of attempts taken to match the random number so far generated with the choice of the user.
  • We have declared two variables "playCounter" and "myPick3Result" as integers and initialized  "playCounter" to 0 to set the counter.
  • "myPick3Result" variable is used to store the randomly generated number.
  • Then we created a while loop which loop which loops indefinitely and the condition tho break through the loop is specified inside the loop.
  • This loop is created according to the given description in the question.
  • Inside loop,
    • officialPlayPick3() is called and the randomly generated number so returned is assigned to the "myPick3Result" variable.
    • The variable "playCounter" is then incremented.
    • if(myPick3==myPick3Result) : Here, the value of the "myPick3" parameter is compared with the variable "myPick3Result". If true it prints the "playCounter" variable value and breaks from the loop and thus ending the loop otherwise, the loop continues to run.   

Related Solutions

Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program. Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number. In the main() method, declare an int named myPick3choice. Create a prompt for myPick3choice as "Enter your Pick 3...
Let’s suppose that you are going to play the lottery game Powerball. To play, you pick...
Let’s suppose that you are going to play the lottery game Powerball. To play, you pick five different numbers from 1 through 69 plus one Powerball number from 1 through 26. Which is a more likely combination of winning numbers: 1, 2, 3, 4, 5, 6 or 7, 21, 25, 32, 40, 56? Explain your answer. For a $500,000,000 jackpot, which of the two combinations would likely be more lucrative for you if it were to win? In other words,...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line. The...
1. In an instant lottery, your chances of winning are 0.1. If you play the lottery...
1. In an instant lottery, your chances of winning are 0.1. If you play the lottery six times and outcomes are independent, determine the probability that (i) you win at most once. (ii) you lose all six times. (iii) you win exactly two times. Please show work will rate!!!
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
rite a java program that uses the methods written in this section to do the following:Prompt...
rite a java program that uses the methods written in this section to do the following:Prompt the user for a number of grades using the getValidInput method. Ensure the number of grades is greater than 1 and less than 30. The error message is “You must enter a number between 1 and 30, please re-enter:”Prompt the user to enter the requested grades and total marks using the calculateLetterGrade method. The grades should be between 0 and 100. Display an appropriate...
Java: Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers...
Java: Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line.    The numbers drawn cannot repeat Sort...
You enter to play the lottery. To your surprise, you've won! You are given the option...
You enter to play the lottery. To your surprise, you've won! You are given the option to accept either a lump-sum payment of $105 million today or annual life-time payments of $3,582,000 beginning today. Assuming current rates of interest are at 3.25% which is the better decision. Be sure to discuss all the aspects and assumptions in making your decision and any calculations you have made to support that decision. How old would you have to live to be indifferent...
Write a Python Version 3 program that will ask you the loan amount and the interest...
Write a Python Version 3 program that will ask you the loan amount and the interest rate. Then the program will tell you - The monthly payment, How many months it will take you to pay off the loan, The total amount of interest paid over the life of the loan. Program should also indicate the final payment as in many cases the final payment at the end of the loan would not be exactly the normal monthly payment and...
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT