Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Random Number Generation: There are several ways to generate random numbers in Java. We will use the method that utilized the Math library. Thiswill require

Random Number Generation:

There are several ways to generate random numbers in Java. We will use the method that utilized the Math library. Thiswill require you to include the following line of code at the topof your program, before your Lab4d class declaration.

import java.lang.Math;

The method, Math.random(), will return a random value between 0(inclusively) and 1.0 (exclusively). In order to modify thisto a range between the integers 1 and some upper limit, we need tomultiply the return value of Math.random() by the upper limit ofthe range we desire and add 1.

For example, if we wanted to generate a random number between 1and 50, we would use the code:

double randNum = (Math.random() * 50) + 1;

If we wanted to limit the result further to only include integervalues we would cast the results to ints.

int randNum = (int)(Math.random() * 50) + 1;

To make this code more useful and specify both an upper limitand a lower limit, we would modify the code as follows:

int randNum = (int)(Math.random() * (upperLimit - lowerLimit+ 1) )+ lowerLimit;

For example, if we wanted to generate a random number between 17and 1034, we would use the code:

int randNum = (int)(Math.random() * (1034 - 17 + 1)) +17;

or more generically:

//Define the range
int max = 1034;
int min = 17;
int range = max - min + 1;

//Generate a random number between min and max
int randNum = (int)(Math.random() * range) +min;

Program: Guessing Game

For this lab, you’ll need to use do-while loops and ifstatements to construct a guessing game. The computer will choose arandom number between 1 and 100 and the user will need to guesswhat the number is. If the user guesses incorrectly, the computerwill indicate whether the user’s guess was too high or too low. Ifthe user guesses correctly, the computer reports how many tries ittook to get the correct answer, and then asks if the user wouldlike to play again.

Technical Design:

This program will require four do-while loops:

  • The primary do-while loop (outermost) will contain almost allof the code in the program and keep running over and over until theuser indicates they no longer wish to play.
  • The game do-while loop is nested inside the primary do-whileloop and will keep running over and over again until the userguesses the correct answer and wins the game. As soon as the userwins a game, the program will exit this do-while loop.
  • The numeric input validation do-while loop is nested inside ofthe game do-while loop and will ensure that the user entered avalid whole number for each guess.
  • The non-numeric input validation do-while loop is located AFTERand OUTSIDE of the game do-while loop. It is only executed after agame is complete and asks the user if they would like to playagain. It is nested inside of the primary do-while loop and willensure that the user entered a valid ‘Y’ / ‘y’ / ‘N’ / ‘n’ inanswer to the “Would you like to play again (Y/N)?” question.

Big Hint:

When approaching a problem with several nested loops, it issometimes easier to start with the innermost loops and workoutward:

  • Create the numeric input validation loop
  • Indent the numeric input validation loop and surround it withthe game loop
  • Add the non-numeric input validation loop after the gameloop
  • Indent the game loop and the non-numeric input validation loopsand surround them with the primary program loop

The diagram provides a rudimentary architecture for theguessing game program:

Key Program Requirements:

  • At the beginning of the program, output a message to the userdescribing what the program will do as shown in the examplerun.
  • Perform numeric input validation to ensure the user is enteringa valid whole number.
  • Perform non-numeric input validation to ensure the user isentering a 'Y', 'y', 'N', or 'n' when asked if they want to playagain.

Example Run:

  • This program is a guessing game.
    The computer will generate a random integer between 1 and 100.The user will try to guess the number.
    Let's get started!

    I'm thinking of a number between 1 and 100.
    What is your guess? something
    Error: Please enter a whole number.

    What is your guess? 50
    Your guess is too high. Try again.

    What is your guess? 25
    Your guess is too low. Try again.

    What is your guess? 33
    CORRECT! You guessed it in 3 tries!!

    Would you like to play again? Not sure
    Error: Please answer with a 'Y' or 'N'.

    Would you like to play again? y


    This program is a guessing game.
    The computer will generate a random integer between 1 and 100.The user will try to guess the number.
    Let's get started!

    I'm thinking of a number between 1 and 100.
    What is your guess? 50
    Your guess is too high. Try again.

    What is your guess? 25
    Your guess is too high. Try again.

    What is your guess? 12
    Your guess is too high. Try again.

    What is your guess? 6
    Your guess is too high. Try again.

    What is your guess? 3
    Your guess is too low. Try again.

    What is your guess? 4
    CORRECT! You guessed it in 6 tries!

    Would you like to play again? n

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

=+How do you calculate the present value of a mixed stream?

Answered: 1 week ago