Question

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.

Homework Answers

Answer #1

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.   
Know the answer?
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
Similar Questions
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January - December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
Create a Java Program to calculate luggage costs. The Business Rules are: A. Two bags per...
Create a Java Program to calculate luggage costs. The Business Rules are: A. Two bags per person are free. B. The Excess Bag Charge is $75 per bag. The program needs to do the following: 1. In your main method(),    Create integers to store the number of Passengers and also the total number of bags    Prompt for the number of passengers on a ticket.    Prompt for the total number of bags for all passengers on a ticket....
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
2). Question with methods use scanner (Using Java): You are going to invest the amount, then...
2). Question with methods use scanner (Using Java): You are going to invest the amount, then you going to compute that amount with annual interest rate. 1) Prompt the user to enter student id, student name, student major 2) Declare variables consistent with names that you are prompting and relate them to the scanner 3) Declare the amount invested in college, prompt the user to ask for amount and declare variable and scanner to input 4) Declare the annual interest...
Write a java program to display a given range of integer numbers (0 to 9) in...
Write a java program to display a given range of integer numbers (0 to 9) in the following order: Example : given (5 to 9), the program displays 56789 67895 78956 89567 95678 The first line displays the min to max range. Each line that follows should display the next higher number except when it hits the maximum number. In that situation, the line wraps back to the minimum sequence. For this problem write a static method to do the...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the countdown sequence for a rocket launch. However, it seems to loop infinitely. Fix the program so it counts down and terminates properly. starter code : import java.util.Scanner; /* * Counts down to a blastoff, starting from a given number. */ public class Countdown {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int countdown = 0;...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
Summary In this lab, you declare and initialize variables in a C++ program. The program, which...
Summary In this lab, you declare and initialize variables in a C++ program. The program, which is saved in a file named NewAge.cpp, calculates your age in the year 2050. Instructions Declare an integer variable named myNewAge. Declare and initialize an integer variable named myCurrentAge. Initialize this variable with your current age. Declare and initialize an integer variable named currentYear. Initialize this variable with the value of the current year. Use four digits for the year. Execute the program by...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT