Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: In this homework you will learn about iteration (one of the three bas

ID: 3628542 • Letter: O

Question

Objective: In this homework you will learn about iteration (one of the three basic control mechanisms in programming, the others being sequence and branching). You will also develop a command-line interface for your software.

Specification: Use Java to write and run a guessing game program that will think of a number and invite a user to guess it. After each guess, the game will tell the user if his/her guess was high or low or if they guessed correctly, and then ask the user to guess again. The game will continue to ask for the next guess until either the user guesses correctly or gives up. When the game ends, if the user finally guessed the correct number, the game will tell the user how many guesses were needed and whether the user was a good guesser or not. If the user quits before guessing correctly, he/she will be suitably insulted.

Here are two examples of how the game should work:

Welcome to the Guessing Game.
I am thinking of a number between 0 and 1000.

What is your guess? 25
Good guess, but 25 is too low.

What is your guess? 768
Good guess, but 768 is too high.

What is your guess? 432
Correct! It took you only 3 guesses. You win!!

Or,
Correct, it took you 23 guesses. Don’t waste your money on the lottery and stay away from quiz shows.


Welcome to the Guessing Game.
I am thinking of a number between 0 and 1000.

What is your guess? 222
Good guess, but 222 is too low.

What is your guess? -55
Quitting so soon? Loser!
Note that a negative guess indicates that the user wants to quit.

Your program will have to use a random number generator to enable it to think of a different number each time it is run. Fortunately, there is one built into Java. Here is how you would invoke it:

int secretNumber;
secretNumber = (int)(Math.random() * 1000. + 1);
Math.random() generates a random double between 0.0 and 0.99999999. When it is multiplied by 1000 and then 1 is added to it, it has a value between 1.0 and 1000.999999. Casting it to an int produces an integer from 1 to 1000.

Once your game has its secret number, it begins asking a user to guess it until correct or he/she quits. Use a loop for this part, and I suggest a while-loop controlled by two Boolean variables: one (stop) that indicates when the user wants to quit and the other (correct) that indicates when the guess is correct, as follows:

while (!stop && !correct) {
Please use JOptionPane for your program's inputs and outputs.

As always, the source code must be indented properly, have a header with your name and a short description of the purpose of the program, and contain comments that describe important parts of the algorithm.

Explanation / Answer

please rate - thanks

import javax.swing.*;
public class GuessTheNumber {
public static void main(String [] args) {
int number,guess,guesses,over;
String input;
int num=(int)(1000 * Math.random ()) + 1;
guesses=0;
over=0;
do {
input= JOptionPane.showInputDialog(null, "Guess a number between 1 and 1000. ");
guess=Integer.parseInt(input);
guesses++;
if(guess<0)
        {over=1;
        JOptionPane.showMessageDialog(null,"Quitting so soon? Loser!");
        }
else if (guess<num)
        JOptionPane.showMessageDialog(null,"Your guess is too low.");
else if (guess>num)
        JOptionPane.showMessageDialog(null,"Your guess is too high.");
else
        {JOptionPane.showMessageDialog(null,"You got it in "+guesses+" guesses");
        if(guesses>=20)
                JOptionPane.showMessageDialog(null,"Don’t waste your money on the lottery and stay away from quiz shows.");
         else if (guesses>=10)
                JOptionPane.showMessageDialog(null,"This is not your game");
      else if(guesses>=8)
                JOptionPane.showMessageDialog(null,"That was pretty bad!");
        else if(guesses==7)
                JOptionPane.showMessageDialog(null,"That was ok!");
       else if(guesses>=5)
                JOptionPane.showMessageDialog(null,"That was really good!");
      else if(guesses>=2)
                JOptionPane.showMessageDialog(null,"That was amazing!");
      else
                JOptionPane.showMessageDialog(null,"That was lucky! ");
      over=1;
                }
}while(over==0);
}
}