Question: Start Eclipse and import the project named GuessingGame. Review the existing code. Note the use of the Random class. This code generates a random integer
Start Eclipse and import the project named GuessingGame.
Review the existing code. Note the use of the Random class. This code generates a random integer between 0 and upperLimit. For the purposes of this exercise, you dont need to understand how it works.
Write the code that prompts the user for an upper limit for the guess. This code should set the upperLimit variable which is already defined.
Write the code that prompts the user for their first guess.
Write the while loop that allows the user to guess again if their guess was wrong. This loop should inform the user whether their guess was too high or too low. Then, it should prompt the user for a new guess. The loop should exit when the users guess is equal to the random number the computer generated.
After the while loop, write code that tells the user they guessed correctly.
Run the application and make sure it works correctly.
Before the while loop, add a variable to keep track of the number of guesses the user has made. Remember to initialize this variable to 1 instead of 0 since the user will need at least one guess to get the correct number.
Inside the while loop, increment the variable by one each time the user guesses incorrectly.
After the while loop, add a message that tells the user how many guesses they took.
Modify the application so it uses some object-oriented features by creating a new class named NumberGame.
In this class, create one instance variable for storing the upper limit of the number, a second for storing the number, and a third for the number of guesses the user has made.
Add a constructor to the class that takes an integer value for the upper limit and uses it to set the upper limit instance variable. In this constructor generate the number that the user should try to guess and set that instance variable. Also initialize the instance variable for the number of guesses to 1.
Add get and set methods for all three instance variables.
Add a method named incrementGuessCount that adds 1 to the instance variable for the number of guesses.
In the Main class modify the code so it uses the new object.
Remove any unnecessary code from the main method that is now being accomplished by the new object.
Run the project again and makes sure it still works correctly.
In the NumberGame class, add a constructor to the class that takes no arguments. The code for this constructor can call the other constructor in this class and pass it a value of 50 for the upper limit.
In the Main class, modify the code so it uses the zero-argument constructor. Then, comment out the statements that get the upper limit from the user. These statements are no longer necessary since the constructor automatically sets the upper limit to 50.
Run the project again and makes sure it works correctly. It should now set an upper limit of 50 by default.
Use Eclipse to create a new package named murach.games.guessing. Then, move the Main class into it. Review chapter 5 if necessary.
Use Eclipse to create a new package named murach.games.guessing.business. Then, move the NumberGame class into it.
Review the code in the NumberGame class and note that Eclipse has automatically updated the package statement for you.
Review the code for the Main class and note that Eclipse has automatically updated the package statement. Also, note that Eclipse has automatically added an import statement for the NumberGame class.
Run the project to make sure it works correctly.
Modify the loop in the Main class so that it prompts the user for a number. (Comment out unnecessary code after the modification, if there is any.)
Modify the loop so that its an infinite while loop. (Comment out unnecessary code after the modification, if there is any.)
Modify the if/else statement so it uses a break statement to jump out of the loop if the user guesses the correct number. (Comment out unnecessary code after the modification, if there is any.)
Run the application to make sure it works correctly.
Within the loop, modify the if/else statement so the application says, "Way too high!" if the users guess is more than 10 higher than the random number. Otherwise, the application should just say, Your guess is too high.
After the loop, add an if/else statement that displays a message that depends on the users number of guesses based on the following:
Number of guesses Message
================= =======
<=3 Great work! You are a mathematical wizard.
>3 and <=7 Not too bad! You've got some potential.
>7 What took you so long? Maybe you should take some lessons
Run the application to make sure it works correctly.
In the Main class, add a try/catch statement so it catches the NumberFormatException thats thrown by the parseInt method. (Review chapter 8 if necessary.) If this exception is thrown, display a message to the console that says, Invalid number and jump to the top of the loop. This should prompt the user to enter the number again.
Run the application to make sure it works correctly.
Add an if/else statement to make sure the user enters a value between the minimum and maximum values. If the user enters a value thats less than or equal to 0, display a user-friendly error message and jump to the top of the loop. Conversely, if the user enters a value thats greater than or equal to the games upper limit, display a user-friendly error message and jump to the top of the loop.
Run the application to make sure it works correctly.
In the NumberGame class, add instance variables of the LocalDateTime type for the start and end time of the game. Then, add get and set methods for these instance variables.
In the NumberGame class, add a method that returns the number of seconds between the start and end time. To do that, you can use code like the following code to get the number of seconds that have elapsed since January 1, 1970: long startSeconds = startTime.toInstant(ZoneOffset.UTC).getEpochSecond();
Then, you can use subtraction to determine the number of seconds between the start and end time.
In the Main class, store the start time in the NumberGame object just before the code prompts the user for the first number. Then, store the end time in the NumberGame object just after the code that displays the message that says, Correct!
In the Main class, add code that displays a message that contains the number of seconds just before the code that displays the message that says, Bye!
Run the application to make sure it works correctly. If it takes the user 20 seconds to guess the number, the console should display a message something like this: You guessed the correct number in 20 seconds.
Add javadoc comments to all the classes in the project. Make sure these comments include @param and @return tags every where these tags are appropriate.
This documentation must include descriptions for all of the constructors and methods as well as information about all parameters and return values.
Generate the documentation for the entire project.
View the documentation.
MAIN.JAVA
package murach.games;
import java.util.Random;
public class Main {
public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); // TODO: Replace next line with code that prompts user for upper limit. int upperLimit = 10;
// Generate a random number between 0 and the upperLimit variable Random random = new Random(); int number = random.nextInt(upperLimit - 1) + 1; System.out.println("number: " + number); // remove this from final app } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
