/    Sign up×
Community /Pin to ProfileBookmark

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());
    }

    }

    to post a comment
    Java

    1 Comments(s)

    Copy linkTweet thisAlerts:
    @Bert22Nov 26.2004 — 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
    ×

    Success!

    Help @keko4201 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...