98.1k views
4 votes
What conclusion can be made about the state of the program when the while loop terminates? Assume answer is a declared and initialized String. (4 points)

while(answer.equals("N"))
{
// code not shown
}

In the Guess My Number game, there is a lower limit and an upper limit for the range of possible numbers to guess. If the lower limit were exclusive and the upper limit inclusive, which expression would properly generate values for the secret number? (4 points)

a
(int)(Math.random() * (upper − lower) ) + lower

b
(int)(Math.random() * (upper − lower + 1) ) + lower

c
(int)(Math.random() * (upper − lower) ) + lower + 1

d
(int)(Math.random() * (upper − 1 − lower) ) + lower + 1

e
(int)(Math.random() * (upper − lower + 1) ) + lower - 1



In the Guess My Number program, the user continues guessing numbers until the secret number is matched. The program needs to be modified to include an extra criterion that requires all guesses to be within the lower and upper bounds. If the user guesses below or above the range of the secret number, the while terminates. How would the while statement be modified to include the new criterion? (4 points)


a

while(userGuess != secretNumber || userGuess >= lowerLimit || userGuess <= upperLimit)


b

while(userGuess != secretNumber || userGuess >= lowerLimit && userGuess <= upperLimit)


c

while(userGuess != secretNumber && userGuess >= lowerLimit && userGuess <= upperLimit)


d

while(userGuess == secretNumber || userGuess >= lowerLimit && userGuess <= upperLimit)


e

while(userGuess == secretNumber && userGuess >= lowerLimit || userGuess <= upperLimit)

2 Answers

4 votes

(int)(Math.random() * (upper − 1 − lower) ) + lower + 1

while(userGuess != secretNumber && userGuess >= lowerLimit && userGuess <= upperLimit)

User Jon Gjengset
by
5.9k points
3 votes

Answer:

1. The loop will run unless the value of answer equals to Capital N. The method equals is “case-sensitive” that is lower case letter and upper case letters are considered different. That “a” and “A” means the same to us, but for the method equals it is different and equals method will return false if the case is not matching.

2. a (int)(Math.random() * (upper − lower) ) + lower

Since we need to consider the lower limit value together to get the desired results we need to add the value of lower limit to the multiplied answer.

3. b while(userGuess != secretNumber || userGuess >= lowerLimit && userGuess <= upperLimit)

Here the program is required to check the while loop and exit when user guess the number or we can say the loop should continue until the user guess the number, so that is why we have taken userGuess!=secretNumber. Next the loop should be exited if it is not within the range or we can say that the loop should run only if the guessed number is within the upper and lower limit. That is why we have opted for the condition userGues>=lowerlimit && userGuess<=upperlimit. Why we have taken && as the operator is that, it’s a range of values so && operator best suit for this kind of logical condition.

User Pperrin
by
5.8k points