Question

Java Guessing Game Class The class will generate a random number of 1 to 15, and...

Java Guessing Game Class

The class will generate a random number of 1 to 15, and then check to see if the user guessed the number correctly. If the number is incorrect, the user should have the chance to guess again, until they guess the right number or they guess 10 times. The class method should keep track of the number of guesses the user has had, and return this value.  

The class should have one constructors, a default and a defined. The class should have a method to calculate the random number. It should also have a method to check the guess to see if it was correct. The guessing method should return a Boolean value to determine if the user keeps guessing.  

The program should check for valid input. It should print out either that the user’s guess was incorrect, printing out the value the user last guessed and how many guesses it took; or that the guess was correct, printing out how many guesses it took to get the correct answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

import java.util.ArrayList; //importing array list class

import java.util.Scanner;

import java.util.Random;

class Main {

public static int no_of_guesses;

public static int guess;

//default constructor to set the values initially

Main(){

no_of_guesses = 10;

}

//method to calculate and return random number

public static int generate_random(int max, int min){

Random r = new Random();

    //generating random number between max and min values

return r.nextInt((max - min) + 1) + min;

}

public static boolean is_guess_correct(int user_guess){

if(user_guess == guess){

return true;

}else{

return false;

}

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

//create object of game

Main game = new Main();

//generate a random number between 1 and 15

game.guess = generate_random(15, 1);

//user can guess untill he reaches max number of guesses

for(int i=0;i<game.no_of_guesses;i++){

//ask the user to guess

System.out.print("\n\nGuess the number : ");

int user_guess = sc.nextInt();

//check if user guesses correctly

if(is_guess_correct(user_guess)){

System.out.printf("\n\nEntered value %d is correct guess, it took %d guesses to make it right.",user_guess,i+1);

break;

}else{

System.out.printf("\n\nEntered value %d is incorrect guess, you have guessed %d times.",user_guess,i+1);

}

}

}

}

Add a comment
Know the answer?
Add Answer to:
Java Guessing Game Class The class will generate a random number of 1 to 15, and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...

    Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower. Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that represent the highest possible number to 100 (what type should this be?) Initialize a Boolean variable that represents if we’ve achieved the correct guess to false Initialize a variable in which to...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Number Guessing Game Games Write program in C++. For this game, the computer will select a...

    Number Guessing Game Games Write program in C++. For this game, the computer will select a random number between 1 and 100 (inclusive). The computer will then ask the (human) player to guess the number the computer has selected. After the player’s guess is input to the computer, the computer will output one of three responses, depending on the relationship of the number the player guessed to the number the computer selected: “Your guess was too low.” “Your guess was...

  • Exercise 4-4      Create an object and use it with the Number Guessing Game In this exercise,...

    Exercise 4-4      Create an object and use it with the Number Guessing Game In this exercise, you’ll convert a number guessing game so it uses some object-oriented features. The class we create will have 3 private instancevariables, 2 constructors, 3 getmethodswhich return a value, and 1 methodthat acts as a sub procedure. The logic for creating a random number and tracking the # of guesses is moved to this new class. Review the project Start NetBeans and open the project...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • python code for guessing game users enter the number the computer will guess 8:38 PM 100...

    python code for guessing game users enter the number the computer will guess 8:38 PM 100 nstructure.com 1. Number guessing game : (5 points) 1) Requirements: Your program will ask the user to enter a number between 1 and 1000. Then it will try to guess the correct number with the guaranteed minimal average number of guesses. Which search algorithm should you use? 2) Expected Output: Enter a number between 1 and 1000? 784 Computer guesses: 500 Type 'l' if...

  • IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one...

    IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one and 100. Then it will ask the user to make a guess as to what the number is. If the guess is incorrect, but is within the range of 1 to 100, the user will be told if it is higher or lower and then asked to guess again. If the guess was outside the valid range or not readable by Scanner class, a...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...

    Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT