# Thread: BlueJ Revising GuessNumber Game

1. Member
Join Date
Dec 2013
Posts
12
Rep Power
0

## BlueJ Revising GuessNumber Game

I am having trouble with this excercise, Exercise 4.27* Revise the GuessNumber game to say "you're hot" if within 2 of the right answer, "you're warm" if within 6 of the right answer, and "too high" or "too low" otherwise. It always says you're hot no matter what the number is. I know that I need to add Math.abs() somewhere, but in what form and how.

I am also having trouble with this exercise: Exercise 4.29* Find the best strategy for getting the right answer in the fewest guesses for the revision of GuessNumber described in the preceding exercise.

Here is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import javax.swing.JOptionPane;
public class GuessNumber extends BasicGame {
private java.util.Random randy;
private int itsSecretNumber;
private int itsUsersNumber;
public GuessNumber(){
super();
randy = new java.util.Random();
} //=======================
itsSecretNumber = 1 + randy.nextInt (10);
} //=======================
String s = JOptionPane.showInputDialog
("Guess my number from 1 to 100:");
if (s != null && ! s.equals ("")) {
itsUsersNumber = Integer.parseInt (s);
}else{
itsUsersNumber = -1; // just to have a value there
}

} //======================
public boolean shouldContinue(){
return itsUsersNumber != itsSecretNumber;
} //======================
//Exercise 4.28
public void showUpdatedStatus(){
if (itsSecretNumber <= itsUsersNumber + 2 || itsSecretNumber >= itsUsersNumber - 2){
JOptionPane.showMessageDialog(null, "You're hot!");
}else
if (itsSecretNumber <= itsUsersNumber + 5 || itsSecretNumber >= itsUsersNumber - 5){
JOptionPane.showMessageDialog(null, "You're warm.");
}
if (itsUsersNumber > itsSecretNumber)
JOptionPane.showMessageDialog (null, "Too high");
else
JOptionPane.showMessageDialog (null, "Too low");
} //=======================
// inherited from BasicGame:
// playManyGames
// playOneGame
// showFinalStatus
}

2. ## Re: BlueJ Revising GuessNumber Game

[code]
[/code]
to get highlighting and preserve formatting.

Do you have specific questions about the problems you are having?

3. Member
Join Date
Dec 2013
Posts
12
Rep Power
0

## Re: BlueJ Revising GuessNumber Game

Java Code:
```import javax.swing.JOptionPane;
public class GuessNumber extends BasicGame {
private java.util.Random randy;
private int itsSecretNumber;
private int itsUsersNumber;
public GuessNumber(){
super();
randy = new java.util.Random();
} //=======================
itsSecretNumber = 1 + randy.nextInt (10);
} //=======================
String s = JOptionPane.showInputDialog
("Guess my number from 1 to 100:");
if (s != null && ! s.equals ("")) {
itsUsersNumber = Integer.parseInt (s);
}else{
itsUsersNumber = -1; // just to have a value there
}

} //======================
public boolean shouldContinue(){
return itsUsersNumber != itsSecretNumber;
} //======================
//Exercise 4.28
public void showUpdatedStatus(){
if (itsSecretNumber <= itsUsersNumber + 2 || itsSecretNumber >= itsUsersNumber - 2){
JOptionPane.showMessageDialog(null, "You're hot!");
}else
if (itsSecretNumber <= itsUsersNumber + 5 || itsSecretNumber >= itsUsersNumber - 5){
JOptionPane.showMessageDialog(null, "You're warm.");
}
if (itsUsersNumber > itsSecretNumber)
JOptionPane.showMessageDialog (null, "Too high");
else
JOptionPane.showMessageDialog (null, "Too low");
} //=======================
// inherited from BasicGame:
// playManyGames
// playOneGame
// showFinalStatus
}```

4. Member
Join Date
Dec 2013
Posts
12
Rep Power
0

## Re: BlueJ Revising GuessNumber Game

I am having trouble with this excercise, Exercise 4.27* Revise the GuessNumber game to say "you're hot" if within 2 of the right answer, "you're warm" if within 6 of the right answer, and "too high" or "too low" otherwise. It always says you're hot no matter what the number is. I know that I need to add Math.abs() somewhere, but in what form and how.

I am also having trouble with this exercise: Exercise 4.29* Find the best strategy for getting the right answer in the fewest guesses for the revision of GuessNumber described in the preceding exercise

My question is what is the revised version of the code so that it doesn't always say you're hot all of the time. Can you fix my code so that it is correct?

5. ## Re: BlueJ Revising GuessNumber Game

Java Code:
`         if (itsSecretNumber <= itsUsersNumber + 2 || itsSecretNumber >= itsUsersNumber - 2){`
When will that if test be false? Substitute some numbers for the variables to see what happens when that code is executed.
If the secret number is 40, then a guess of 38 to 42 would be ok.
What does the code compare if the guess is 10 or if the guess is 50?

When multiple subconditions are connected with a OR operator, the full condition returns true if any sub condition returns true.

When multiple subconditions are connected with an AND operator, all the sub conditions must be true for the full condition to return true.
Last edited by Norm; 12-31-2013 at 04:19 AM.

6. Member
Join Date
Dec 2013
Posts
12
Rep Power
0

## Re: BlueJ Revising GuessNumber Game

Ok, but what should I put to make certain choices for
Java Code:
`if (itsSecretNumber <= itsUsersNumber + 2 || itsSecretNumber >= itsUsersNumber - 2){`
false. What is the finalized version of the code? Thanks.

7. Member
Join Date
Dec 2013
Posts
12
Rep Power
0

## Re: BlueJ Revising GuessNumber Game

So do I change the or operator to and to fix or how should I fix it?

8. ## Re: BlueJ Revising GuessNumber Game

What happened when you tried that?