1) Develop a simple class that represents a number guessing game. The game is pl
ID: 3539490 • Letter: 1
Question
1) 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 %u201CToo High%u201D is returned, if the guess is below the answer then %u201CToo Low%u201D. Also if the difference between the answer and the guess is less than the difference between the answer and the previous guess, %u201CGetting warmer%u201D is returned. If the difference between the answer and the guess is more than the difference between the answer and the previous guess, then %u201CGetting Colder%u201D is returned.
2) The program will allow the user to play multiple games. Once a game is complete the user will be prompted to play a new game or quit.
3) Design and build a GuessingGame class.
a. Seven instance variables.
i. answer - an integer representing the randomly generated number.
ii. generator %u2013 a random Generator object (create from the Java API Random class) (pg 250 in text)
iii. gameOver %u2013 a Boolean, false if game still in progress, true if the game is over.
iv. differential %u2013 an integer representing the difference between a guess and the answer.
v. max %u2013 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)
vi. maxGuessesAllowed %u2013 the maximum number of guesses the user gets, once this value is passed the game is over.
vii. numGuessesTaken %u2013 an integer that stores the number of guessed taken so far in any game.
b. Constructor and Methods
i. Default Constructor
1. Sets max to zero
2. Creates the random number generator object.
ii. Parameterized Constructor
1. Takes an integer parameter representing the maximum value of the number to guess.
2. Creates the random number generator object.
iii. newGame method
1. 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.
2. Generates the answer using the random number generator.(0 - max).
3. Sets gameOver to false.
4. Sets differential to the max value.
5. Sets numGuessTaken to zero.
iv. guess method
1. Takes in an integer as a parameter representing a new guess.
2. Compares the new guess with the answer and generates and returns a String representing an appropriate response.
3. The response is based on:
a. The relation of the guess and answer (too high, too low or correct).
b. The comparison of difference between the current guess and the answer and the previous guess and the answer. (warmer, colder)
c. Guess out of range error, if the guess is not between 0 and the max number (inclusive) (see sample run below)
d. User has taken too many guess because numGuessesTaken is greater than maxGuessesAllowed. If this is the case set isGameOver to true.
v. isGameOver method - returns the state of game.
1. true if game is over
2. false if still in progress.
vi. Accessor and mutator methods for all instance fields except the Random number generator. Use the Accessor or mutator methods within the other method of the class rather than directly accessing the instance fields.
4) Design and build GuessingGameTester class
a. This program will create GuessingGame objects and completely test the GuessingGame Class.
b. The tester will also provide two loops.
c. The first loop will allow the user to play a new game after the previous game is completed.
d. The second or nested loop will prompt the user for a new guess and provide a response based on the guess.
Sample Output:
Welcome to the Guessing Game
Enter the maximum number
100
answer is: 8 (Included for testing only, should not be display in final program)
Enter your guess, remember it must be between 0 and 100
50
Too High
Getting Warmer
Enter your guess, remember it must be between 0 and 100
25
Too High
Getting Warmer
Enter your guess, remember it must be between 0 and 100
12
Too High
Getting Warmer
Enter your guess, remember it must be between 0 and 100
6
Too low
Getting Warmer
Enter your guess, remember it must be between 0 and 100
8
Congratulation
Would you like to play again, enter Y for yes, N for no.
y
Welcome to the Guessing Game
Enter the maximum number
50
answer is: 36
Enter your guess, remember it must be between 0 and 50
60
Guess out of range, The guess must be between 0 and 50
Enter your guess, remember it must be between 0 and 50
25
Too low
Getting Warmer
Enter your guess, remember it must be between 0 and 50
48
Too High
Getting Colder
Enter your guess, remember it must be between 0 and 50
37
Too High
Getting Warmer
Enter your guess, remember it must be between 0 and 50
36
Congratulation
Would you like to play again, enter Y for yes, N for no.
n
Implementation Notes:
1) The inner loop, (request the next guess) should use the class%u2019s isGameOver method to determine if a new guess is needed.
2) Boolean values and predicate method, (methods that return a Boolean) are essential in this project.
3) No loops are required in the class%u2019s methods, they are only needed in the Tester.
Submission Requirements:
Your project should follow the instructions below. Any submissions that do not follow the stated requirements will not be graded.
1. Follow the submission requirements of your instructor.
2. You should have three files for this assignment:
a. GuessingGame.java - The GuessingGame class,
b. GuessingGame.html - The documentation file generated from your class (you do not have to turn this file in),
c. GuessingGame Tester.java - A driver program for your GuessingGame class.
3. Remember to compile and run your program one last time before you submit it. If your program will not compile, the graders will not be responsible for trying to test it.
Important Notes:
1. Projects will be graded on whether they correctly solve the problem, and whether they adhere to good programming practices.
2. Projects must be submitted by the time specified on the due date. Projects submitted after that time will get a grade of zero.
3. Please review UWF%u2019s academic conduct policy. Note that viewing another student's solution, whether in whole or in part is considered academic dishonesty. Also note that submitting code obtained through the Internet or other sources, whether in whole or in part, is considered academic dishonesty. All programs submitted will be reviewed for evidence of academic dishonesty, and all violations will be handled accordingly.
Explanation / Answer
please rate - thanks
any problems, I will fix
import java.util.*;
public class GuessingGameTester
{public static void main(String [] args)
{char again;
int n,maxG=7;
Scanner in=new Scanner(System.in);
do
{
System.out.println("Welcome to the Guessing Game");
System.out.print("Enter the maximum number: ");
int max=in.nextInt();
GuessingGame g=new GuessingGame(max);
g.newGame(maxG);
while(!g.isGameOver())
{System.out.println("Enter your guess, remember it must be between 0 and "+g.getMax());
n=in.nextInt();
System.out.println(g.guess(n));
}
System.out.println("Would you like to play again, enter Y for yes, N for no.");
again=in.next().charAt(0);
}while(again=='Y');
}
}
-------------------------------------------------
import java.util.*;
public class GuessingGame
{
private int answer;
private Random generator;
private boolean gameOver;
private int differential;
private int max;
private int maxGuessesAllowed;
private int numGuessesTaken;
public GuessingGame()
{max=0;
generator=new Random();
}
public GuessingGame(int n)
{max=n;
generator=new Random();
}
public void newGame(int n)
{maxGuessesAllowed=n;
answer=generator.nextInt(max+1);
//System.out.println(answer);
gameOver=false;
differential=max;
numGuessesTaken=0;
}
public int getMax()
{return max;
}
public String guess(int n)
{String g;
int d;
numGuessesTaken++;
if(numGuessesTaken>=maxGuessesAllowed)
{gameOver=true;
g="Too many guesses taken-game over ";
return g;
}
if(n<0||n>max)
{g="Guess out of range, The guess must be between 0 and "+max;
numGuessesTaken--;
return g;
}
if(n<answer)
g="Too Low ";
else if(n>answer)
g="Too High ";
else
{g="correct ";
gameOver=true;
return g;
}
d=Math.abs(n-answer);
if(d<differential)
g=g+"Getting warmer ";
else
g=g+"Getting colder ";
differential=d;
return g;
}
public boolean isGameOver()
{ return gameOver;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.