Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Overall it looks pretty good. A few pointers though...

  • Try to indent your code properly. Most of it looks good but on a few places you have missed an indentation. e.g.,

     do {
     setMax();
     setObjective();
     userGuess();
     playAgain();
     } while (playAgain == true);
    
  • Don't consume nor fail silently on exceptions (except in some rare cases but that's out of scope here)

     catch (NumberFormatException e) {} // don't do this
    
  • Surround your statements with appropriate curly brackets

     if (max == 1) // no brackets!
     objective = 1;
     else // no brackets!
     objective = rand.nextInt(max - 1) + 1;
    

Some people don't do it out of habbit, or coding style, etc. It may not be a problem but as your codebase expands it could become a problem and IIRC Java style docs advice against omitting curly brackets. Here's an example of why it may be a bad idea http://stackoverflow.com/a/8020255/1021726 https://stackoverflow.com/a/8020255/1021726

  • max > Integer.MAX_VALUE Will never happen

Because max is declared as an integer and assigned the value entered in the JOptionPane. In the event of an user would enter a value which is higher than Integer.MAX_VALUE it would throw an exception. And since you do not do anything with your exceptions it will just fail silently. See point #2.

  • static vs non-static

Do you understand static? If not, then you can get a quick brief here http://www.caveofprogramming.com/frontpage/articles/java/java-for-beginners-static-variables-what-are-they/

If you understand static, you may realize that you do not want two instances of your Guess class to share the same variable.

  • Declare the access modifier

     static boolean playAgain; // no access modifier
    

While your variables lack access modifiers (and I do not think it's by purpose), and it may not be a problem as then it defaults as package-private, but you need to beware that it's important to design your class, it's members and variables accordingly to how they should be used. Are your variables really package-private, or should they rather be declared as just private variables?

Overall it looks pretty good. A few pointers though...

  • Try to indent your code properly. Most of it looks good but on a few places you have missed an indentation. e.g.,

     do {
     setMax();
     setObjective();
     userGuess();
     playAgain();
     } while (playAgain == true);
    
  • Don't consume nor fail silently on exceptions (except in some rare cases but that's out of scope here)

     catch (NumberFormatException e) {} // don't do this
    
  • Surround your statements with appropriate curly brackets

     if (max == 1) // no brackets!
     objective = 1;
     else // no brackets!
     objective = rand.nextInt(max - 1) + 1;
    

Some people don't do it out of habbit, or coding style, etc. It may not be a problem but as your codebase expands it could become a problem and IIRC Java style docs advice against omitting curly brackets. Here's an example of why it may be a bad idea http://stackoverflow.com/a/8020255/1021726

  • max > Integer.MAX_VALUE Will never happen

Because max is declared as an integer and assigned the value entered in the JOptionPane. In the event of an user would enter a value which is higher than Integer.MAX_VALUE it would throw an exception. And since you do not do anything with your exceptions it will just fail silently. See point #2.

  • static vs non-static

Do you understand static? If not, then you can get a quick brief here http://www.caveofprogramming.com/frontpage/articles/java/java-for-beginners-static-variables-what-are-they/

If you understand static, you may realize that you do not want two instances of your Guess class to share the same variable.

  • Declare the access modifier

     static boolean playAgain; // no access modifier
    

While your variables lack access modifiers (and I do not think it's by purpose), and it may not be a problem as then it defaults as package-private, but you need to beware that it's important to design your class, it's members and variables accordingly to how they should be used. Are your variables really package-private, or should they rather be declared as just private variables?

Overall it looks pretty good. A few pointers though...

  • Try to indent your code properly. Most of it looks good but on a few places you have missed an indentation. e.g.,

     do {
     setMax();
     setObjective();
     userGuess();
     playAgain();
     } while (playAgain == true);
    
  • Don't consume nor fail silently on exceptions (except in some rare cases but that's out of scope here)

     catch (NumberFormatException e) {} // don't do this
    
  • Surround your statements with appropriate curly brackets

     if (max == 1) // no brackets!
     objective = 1;
     else // no brackets!
     objective = rand.nextInt(max - 1) + 1;
    

Some people don't do it out of habbit, or coding style, etc. It may not be a problem but as your codebase expands it could become a problem and IIRC Java style docs advice against omitting curly brackets. Here's an example of why it may be a bad idea https://stackoverflow.com/a/8020255/1021726

  • max > Integer.MAX_VALUE Will never happen

Because max is declared as an integer and assigned the value entered in the JOptionPane. In the event of an user would enter a value which is higher than Integer.MAX_VALUE it would throw an exception. And since you do not do anything with your exceptions it will just fail silently. See point #2.

  • static vs non-static

Do you understand static? If not, then you can get a quick brief here http://www.caveofprogramming.com/frontpage/articles/java/java-for-beginners-static-variables-what-are-they/

If you understand static, you may realize that you do not want two instances of your Guess class to share the same variable.

  • Declare the access modifier

     static boolean playAgain; // no access modifier
    

While your variables lack access modifiers (and I do not think it's by purpose), and it may not be a problem as then it defaults as package-private, but you need to beware that it's important to design your class, it's members and variables accordingly to how they should be used. Are your variables really package-private, or should they rather be declared as just private variables?

added 914 characters in body
Source Link
Max
  • 1.9k
  • 1
  • 13
  • 18

Overall it looks pretty good. A few pointers though...

  • Try to indent your code properly. Most of it looks good but on a few places you have missed an indentation. e.g.,

     do {
     setMax();
     setObjective();
     userGuess();
     playAgain();
     } while (playAgain == true);
    
  • Don't consume nor fail silently on exceptions (except in some rare cases but that's out of scope here)

     catch (NumberFormatException e) {} // don't do this
    
  • Surround your statements with appropriate curly brackets

     if (max == 1) // no brackets!
     objective = 1;
     else // no brackets!
     objective = rand.nextInt(max - 1) + 1;
    

Some people don't do it out of habbit, or coding style, etc. It may not be a problem but as your codebase expands it could become a problem and IIRC Java style docs advice against omitting curly brackets. Here's an example of why it may be a bad idea http://stackoverflow.com/a/8020255/1021726

  • max > Integer.MAX_VALUE Will never happen

Because max is declared as an integer and assigned the value entered in the JOptionPane. In the event of an user would enter a value which is higher than Integer.MAX_VALUE it would throw an exception. And since you do not do anything with your exceptions it will just fail silently. See point #2.

  • static vs non-static

Do you understand static? If not, then you can get a quick brief here http://www.caveofprogramming.com/frontpage/articles/java/java-for-beginners-static-variables-what-are-they/

If you understand static, you may realize that you do not want two instances of your Guess class to share the same variable.

  • Declare the access modifier

     static boolean playAgain; // no access modifier
    

While your variables lack access modifiers (and I do not think it's by purpose), and it may not be a problem as then it defaults as package-private, but you need to beware that it's important to design your class, it's members and variables accordingly to how they should be used. Are your variables really package-private, or should they rather be declared as just private variables?

  • Try to indent your code properly. Most of it looks good but on a few places you have missed an indentation. e.g.,

     do {
     setMax();
     setObjective();
     userGuess();
     playAgain();
     } while (playAgain == true);
    
  • Don't consume nor fail silently on exceptions (except in some rare cases but that's out of scope here)

     catch (NumberFormatException e) {} // don't do this
    
  • Surround your statements with appropriate curly brackets

     if (max == 1) // no brackets!
     objective = 1;
     else // no brackets!
     objective = rand.nextInt(max - 1) + 1;
    

Some people don't do it out of habbit, or coding style, etc. It may not be a problem but as your codebase expands it could become a problem and IIRC Java style docs advice against omitting curly brackets. Here's an example of why it may be a bad idea http://stackoverflow.com/a/8020255/1021726

  • max > Integer.MAX_VALUE Will never happen

Because max is declared as an integer and assigned the value entered in the JOptionPane. In the event of an user would enter a value which is higher than Integer.MAX_VALUE it would throw an exception. And since you do not do anything with your exceptions it will just fail silently. See point #2.

Overall it looks pretty good. A few pointers though...

  • Try to indent your code properly. Most of it looks good but on a few places you have missed an indentation. e.g.,

     do {
     setMax();
     setObjective();
     userGuess();
     playAgain();
     } while (playAgain == true);
    
  • Don't consume nor fail silently on exceptions (except in some rare cases but that's out of scope here)

     catch (NumberFormatException e) {} // don't do this
    
  • Surround your statements with appropriate curly brackets

     if (max == 1) // no brackets!
     objective = 1;
     else // no brackets!
     objective = rand.nextInt(max - 1) + 1;
    

Some people don't do it out of habbit, or coding style, etc. It may not be a problem but as your codebase expands it could become a problem and IIRC Java style docs advice against omitting curly brackets. Here's an example of why it may be a bad idea http://stackoverflow.com/a/8020255/1021726

  • max > Integer.MAX_VALUE Will never happen

Because max is declared as an integer and assigned the value entered in the JOptionPane. In the event of an user would enter a value which is higher than Integer.MAX_VALUE it would throw an exception. And since you do not do anything with your exceptions it will just fail silently. See point #2.

  • static vs non-static

Do you understand static? If not, then you can get a quick brief here http://www.caveofprogramming.com/frontpage/articles/java/java-for-beginners-static-variables-what-are-they/

If you understand static, you may realize that you do not want two instances of your Guess class to share the same variable.

  • Declare the access modifier

     static boolean playAgain; // no access modifier
    

While your variables lack access modifiers (and I do not think it's by purpose), and it may not be a problem as then it defaults as package-private, but you need to beware that it's important to design your class, it's members and variables accordingly to how they should be used. Are your variables really package-private, or should they rather be declared as just private variables?

Source Link
Max
  • 1.9k
  • 1
  • 13
  • 18
  • Try to indent your code properly. Most of it looks good but on a few places you have missed an indentation. e.g.,

     do {
     setMax();
     setObjective();
     userGuess();
     playAgain();
     } while (playAgain == true);
    
  • Don't consume nor fail silently on exceptions (except in some rare cases but that's out of scope here)

     catch (NumberFormatException e) {} // don't do this
    
  • Surround your statements with appropriate curly brackets

     if (max == 1) // no brackets!
     objective = 1;
     else // no brackets!
     objective = rand.nextInt(max - 1) + 1;
    

Some people don't do it out of habbit, or coding style, etc. It may not be a problem but as your codebase expands it could become a problem and IIRC Java style docs advice against omitting curly brackets. Here's an example of why it may be a bad idea http://stackoverflow.com/a/8020255/1021726

  • max > Integer.MAX_VALUE Will never happen

Because max is declared as an integer and assigned the value entered in the JOptionPane. In the event of an user would enter a value which is higher than Integer.MAX_VALUE it would throw an exception. And since you do not do anything with your exceptions it will just fail silently. See point #2.

lang-java

AltStyle によって変換されたページ (->オリジナル) /