Results 1 to 8 of 8
  1. #1
    bluejpersona is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default 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();
    } //=======================
    public void askUsersFirstChoice(){
    itsSecretNumber = 1 + randy.nextInt (10);
    askUsersNextChoice();
    } //=======================
    public void askUsersNextChoice(){
    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. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: BlueJ Revising GuessNumber Game

    Please remove the numbers and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Do you have specific questions about the problems you are having?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    bluejpersona is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default 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();
        } //=======================
        public void askUsersFirstChoice(){ 
            itsSecretNumber = 1 + randy.nextInt (10);
            askUsersNextChoice();
        } //=======================
        public void askUsersNextChoice(){
            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. #4
    bluejpersona is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default 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. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default 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 03:19 AM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    bluejpersona is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default 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. #7
    bluejpersona is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: BlueJ Revising GuessNumber Game

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

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: BlueJ Revising GuessNumber Game

    What happened when you tried that?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. BlueJ IDE
    By jcbmack in forum New To Java
    Replies: 9
    Last Post: 08-08-2012, 07:26 AM
  2. BlueJ - UML
    By Nghi97 in forum New To Java
    Replies: 5
    Last Post: 11-28-2011, 11:03 AM
  3. Bluej Hangman Game Help
    By P0CK3Y in forum New To Java
    Replies: 0
    Last Post: 03-29-2011, 10:14 AM
  4. BlueJ help
    By Cid17 in forum New To Java
    Replies: 2
    Last Post: 06-29-2009, 07:38 PM
  5. Replies: 3
    Last Post: 04-20-2009, 09:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •