home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-tomcat-addon-1.4.9-installer.exe / NumberGuessBean.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-05-17  |  1.5 KB  |  55 lines

  1. package num;
  2.  
  3. import java.util.Random;
  4.  
  5. public class NumberGuessBean {
  6.    int answer;
  7.    boolean success;
  8.    String hint;
  9.    int numGuesses;
  10.  
  11.    public NumberGuessBean() {
  12.       this.reset();
  13.    }
  14.  
  15.    public void setGuess(String guess) {
  16.       ++this.numGuesses;
  17.  
  18.       int g;
  19.       try {
  20.          g = Integer.parseInt(guess);
  21.       } catch (NumberFormatException var4) {
  22.          g = -1;
  23.       }
  24.  
  25.       if (g == this.answer) {
  26.          this.success = true;
  27.       } else if (g == -1) {
  28.          this.hint = "a number next time";
  29.       } else if (g < this.answer) {
  30.          this.hint = "higher";
  31.       } else if (g > this.answer) {
  32.          this.hint = "lower";
  33.       }
  34.  
  35.    }
  36.  
  37.    public boolean getSuccess() {
  38.       return this.success;
  39.    }
  40.  
  41.    public String getHint() {
  42.       return "" + this.hint;
  43.    }
  44.  
  45.    public int getNumGuesses() {
  46.       return this.numGuesses;
  47.    }
  48.  
  49.    public void reset() {
  50.       this.answer = Math.abs((new Random()).nextInt() % 100) + 1;
  51.       this.success = false;
  52.       this.numGuesses = 0;
  53.    }
  54. }
  55.