Question
Java Develop a simple class that represents a number guessing game. The game is played by the program randomly generating a number and the user
Java
Develop a simple class that represents a number guessing game. The game is played by the program randomly generating a number and the user attempting to guess that number. After each guess the program will provide a hint to the user identifying the relationship between the number and the guess. If the guess is above the answer then Too High is returned, if the guess is below the answer then Too Low. Also if the difference between the answer and the guess is less than the difference between the answer and the previous guess, Getting warmer is returned. If the difference between the answer and the guess is more than the difference between the answer and the previous guess, then Getting Colder is returned.
- The program will allow the user to play multiple games. Once a game is completed the user will be prompted to play a new game or quit.
- Design and build a GuessingGame class.
- Seven instance variables.
- answer - an integer representing the randomly generated number.
- generator a random Generator object (created from the Java API Random class)(zyBooks Section 2.14)
- gameOver a Boolean, false if game still in progress, true if the game is over.
- differential an integer representing the difference between a guess and the answer.
- max maximum value of the number to guess. For example, if the maximum number is 100 then the number to guess would be between 0 and 100. (inclusive)
- maxGuessesAllowed the maximum number of guesses the user gets, once this value is passed the game is over.
- numGuessesTaken an integer that stores the number of guessed taken so far in any game.
- Constructor and Methods
- Default Constructor
- Sets max to zero
- Creates the random number generator object.
- Parameterized Constructor
- Takes an integer parameter representing the maximum value of the number to guess.
- Creates the random number generator object.
- newGame method
- Takes in an integer as a parameter representing the maximum number of guesses and sets maxGuessesAllowed . In other words, the parameter represents how many guesses the user gets before the game is over.
- Generates the answer using the random number generator. (0 - max).
- Sets gameOver to false.
- Sets differential to the max value.
- Sets numGuessTaken to zero.
- guess method
- Takes in an integer as a parameter representing a new guess.
- Compares the new guess with the answer and generates and returns a String representing an appropriate response.
- The response is based on:
- The relation of the guess and answer (too high, too low or correct).
- The comparison of difference between the current guess and the answer and the previous guess and the answer. (warmer, colder)
- Guess out of range error, if the guess is not between 0 and the max number (inclusive) (see sample run below)
- User has taken too many guess because numGuessesTaken is greater than maxGuessesAllowe If this is the case set isGameOver to true.
- isGameOver method - returns the state of game.
- true if game is over
- false if still in progress.
- Accessor and mutator methods for all instance fields except the Random number generator. Use the Accessor or mutator methods within the other methods of the class rather than directly accessing the instance fields. For example, use mutator methods in the parameterized constructor to modify instance variables.
- Default Constructor
Design and build GuessingGameTester class
- This program will create GuessingGame objects and completely test the GuessingGame Class.
- The tester will also provide two loops.
- The first loop will allow the user to play a new game after the previous game is completed.
- The second or nested loop will prompt the user for a new guess and provide a response based on the guess.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started