www.webdeveloper.com
Results 1 to 2 of 2

Thread: Some coding help

  1. #1
    Join Date
    Aug 2004
    Posts
    61

    Some coding help

    hey hows it going. ok im having a problem.

    this is the question i have to do.

    10. in the numberGame class, remove the declorationof luckyNumber the assignment value. Write a setLuckyNumber method to assign to luckNumber a random number between 1 and 25 and write a getLuckyNumber method. in the playTheGame class add an instruction to EXECUTE the setLuckNumber Method at the startof the game and an instrction that will print out luckyNumber at the end.

    here the code. i did the first part with the random number(i thin k i did it right). i need help with the part where is says exectute the method of random number. also i ran the prog and everythign works excpt it gives a random number of 0, all the time. hwy? what am i doing wrong?

    thnks for the help

    -keko-

    heres the class's

    public class NumberGame{

    //data members
    private int luckyNumber;
    private int guess;


    //method to set guess
    public void setGuess(int g){

    guess = g;
    }

    public void setLuckyNumber(){

    luckyNumber = (int) (25 * Math.random() + 1);
    }

    public int getLuckyNumber(){

    return luckyNumber;
    }

    //method to return appropriate message
    public String responce(){

    if(guess==luckyNumber){

    return "You guessed the lucky number";
    }


    else if(guess>luckyNumber){

    return"Guess to high";
    }

    else {

    return "Guess to low";
    }

    }
    }//end method


    2nd class

    import javax.swing.JOptionPane;

    public class PlayTheGame{

    public static void main(String args[]){

    NumberGame game = new NumberGame();

    String str1 = JOptionPane.showInputDialog("Guess the lucky Number");

    int guess = Integer.parseInt(str1);
    game.setGuess(guess);

    JOptionPane.showMessageDialog(null, game.responce() + " " + getLuckyNumber());
    }
    }

  2. #2
    Join Date
    Nov 2004
    Posts
    1
    Hi keko,

    You probably already figured this out, but it looks to me like you didn't call the setLuckyNumber method in your main program. Since luckyNumber was never set, it will always just return the default value of 0. Try putting game.setLuckyNumber(); right after the line game.setGuess(guess); in your PlayTheGame.java code.

    Hope this helps,
    Bert

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles