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 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....
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)...
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...
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;...
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 =...
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
4) Create the following in a Java program. Create a Scanner Prompt the user to Receive...
4) Create the following in a Java program. Create a Scanner Prompt the user to Receive the amount and declare the variable double amount for scanner Declare variable integer remaining amount which is equal to (int) (amount*100) Find the number of one dollars bt declaring the integer variable which is equal to remaining amount divided by 100 Declare: remaining amount %=100 Find the number of quarters: Declare integer variable number of quarters equal to remaining amount divided by 25 Declare:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT