Exercise 1 - Number Guessing Game 2.0 Make a new Java Project called Lab3. Then, copy the NumberGuessGame class from the Lab2 project into the Lab3 srcfolder. Modify the Lab3 version so that the user will have to keep guessing the number until it is correct. To do this, you will need to first think about which part of the code should repeat. Then, you will need to enclose this code inside of a loop construct. There several such constructs in Java: for loop, while loop, do..while loop, and other variations. The for loop is typically used when we know how many iterations to loop, which is not the case in this exercise. The while loop is used to repeat code zero or more times until the specified condition becomes false. This is basically what we need, but we always want the loop to run at least once. For that purpose, we can use the do..while loop, which is similar to the while loop, but checks the condition at the end, hence executing the code at least once. To use it, put the do keyword on the line before the block of code you want repeated, and put an open brace afterwards. Then, after the block of code to be repeated, put a closing brace, the while keyword, and the condition expression that will test whether the values are different inside parenthesis. The expression inside the parenthesis should be a Boolean condition, which means it has to evaluate to either a True or a False value. This can be done by using the NOT EQUALS relational operator (!=). Run the code through the debugger to make sure it runs correctly in every case. You should tes a sample of cases, including edge cases. For example, check to make sure the program terminates if the user enter the correct guess on the first try. Then check what happens if the user enters several incorrect guesses. Lastly, think about what is the minimum number of guesses that will allow you to guess the correct number in all cases? What is the worst case?

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Use Java. 

1 þackage Lab2;
2® import java.util.Random;
4 public class NumberGuessGame {
public static void main(String[] args) {
int randNum;
int userGuessNum;
Scanner scan = new Scanner(System.in);
Random gen = new Random();
randNum = gen.nextInt(101);
System.out. print("Enter Your Guess Between e and 100, inclusive): ");
userGuessNum = scan.nextInt();
7
if (userGuessNum == randNum) {
System.out.println("You Guessed Correctly! Nice Work! ");
} else {
System.out.println("You Guessed Incorrectly! Try Again! ");
if (userGuessNum < randNum) {
System.out.println("Guess Higher ");
} else {
System.out.println("Guess Lower ");
}
}
7
Transcribed Image Text:1 þackage Lab2; 2® import java.util.Random; 4 public class NumberGuessGame { public static void main(String[] args) { int randNum; int userGuessNum; Scanner scan = new Scanner(System.in); Random gen = new Random(); randNum = gen.nextInt(101); System.out. print("Enter Your Guess Between e and 100, inclusive): "); userGuessNum = scan.nextInt(); 7 if (userGuessNum == randNum) { System.out.println("You Guessed Correctly! Nice Work! "); } else { System.out.println("You Guessed Incorrectly! Try Again! "); if (userGuessNum < randNum) { System.out.println("Guess Higher "); } else { System.out.println("Guess Lower "); } } 7
Exercise 1 - Number Guessing Game 2.0
Make a new Java Project called Lab3. Then, copy the NumberGuessGame class from the
Lab2 project into the Lab3 srcfolder. Modify the Lab3 version so that the user will have to
keep guessing the number until it is correct.
To do this, you will need to first think about which part of the code should repeat. Then, you
will need to enclose this code inside of a loop construct. There several such constructs in Java:
for loop, while loop, do..while loop, and other variations. The for loop is typically used
when we know how many iterations to loop, which is not the case in this exercise. The while
loop is used to repeat code zero or more times until the specified condition becomes false. This
is basically what we need, but we always want the loop to run at least once. For that purpose,
we can use the do..while loop, which is similar to the while loop, but checks the condition
at the end, hence executing the code at least once. To use it, put the do keyword on the line
before the block of code you want repeated, and put an open brace afterwards. Then, after the
block of code to be repeated, put a closing brace, the while keyword, and the condition
expression that will test whether the values are different inside parenthesis. The expression
inside the parenthesis should be a Boolean condition, which means it has to evaluate to either
a True or a False value. This can be done by using the NOT EQUALS relational operator
(!=).
Run the code through the debugger to make sure it runs correctly in every case. You should test
a sample of cases, including edge cases. For example, check to make sure the program
terminates if the user enter the correct guess on the first try. Then check what happens if the
user enters several incorrect guesses.
Lastly, think about what is the minimum number of guesses that will allow you to guess the
correct number in all cases? What is the worst case?
Transcribed Image Text:Exercise 1 - Number Guessing Game 2.0 Make a new Java Project called Lab3. Then, copy the NumberGuessGame class from the Lab2 project into the Lab3 srcfolder. Modify the Lab3 version so that the user will have to keep guessing the number until it is correct. To do this, you will need to first think about which part of the code should repeat. Then, you will need to enclose this code inside of a loop construct. There several such constructs in Java: for loop, while loop, do..while loop, and other variations. The for loop is typically used when we know how many iterations to loop, which is not the case in this exercise. The while loop is used to repeat code zero or more times until the specified condition becomes false. This is basically what we need, but we always want the loop to run at least once. For that purpose, we can use the do..while loop, which is similar to the while loop, but checks the condition at the end, hence executing the code at least once. To use it, put the do keyword on the line before the block of code you want repeated, and put an open brace afterwards. Then, after the block of code to be repeated, put a closing brace, the while keyword, and the condition expression that will test whether the values are different inside parenthesis. The expression inside the parenthesis should be a Boolean condition, which means it has to evaluate to either a True or a False value. This can be done by using the NOT EQUALS relational operator (!=). Run the code through the debugger to make sure it runs correctly in every case. You should test a sample of cases, including edge cases. For example, check to make sure the program terminates if the user enter the correct guess on the first try. Then check what happens if the user enters several incorrect guesses. Lastly, think about what is the minimum number of guesses that will allow you to guess the correct number in all cases? What is the worst case?
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY