Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
//Math function returning a random number
public int randWord(int length) {
 return (int)(Math.random() * length);
}

A better way to return a random number is to use the Random class. random.nextInt(length); is preferred. Also, this method does not need to be public, it should be private as it is only called from this method:

public String selectWord(String[] words) {
 return words[randWord(words.length)];
}

I'd suggest that you take a look at the randomElement method in my answer here in my answer here and use that method. Note that when using a Random object, you should only create it once. So you can put this inside your class:

private final Random random = new Random();

You have three methods that all three have unnecessary number of lines, and can all be static. And all should use proper Javadoc instead of a simple comment above the method.

/**
 * Returns true if the character entered by the user is equal to the character sent from the word in the game class
 */
public boolean testCharacter(char testChar, char charFromWord) {
 return equalsIgnoreCase(testChar, charFromWord);
}
/**
 * Method that uses .toLowerCase to make sure the if test ignores the case of the characters
 */
public boolean equalsIgnoreCase(char testChar, char charFromWord) {
 return Character.toLowerCase(testChar) == Character.toLowerCase(charFromWord);
}
/**
 * Checks if the word the user entered is equal to the word selected as the hangman word
 */
public boolean testWord(String testWord, String hangmanWord) {
 return testWord.equalsIgnoreCase(hangmanWord);
}
//Math function returning a random number
public int randWord(int length) {
 return (int)(Math.random() * length);
}

A better way to return a random number is to use the Random class. random.nextInt(length); is preferred. Also, this method does not need to be public, it should be private as it is only called from this method:

public String selectWord(String[] words) {
 return words[randWord(words.length)];
}

I'd suggest that you take a look at the randomElement method in my answer here and use that method. Note that when using a Random object, you should only create it once. So you can put this inside your class:

private final Random random = new Random();

You have three methods that all three have unnecessary number of lines, and can all be static. And all should use proper Javadoc instead of a simple comment above the method.

/**
 * Returns true if the character entered by the user is equal to the character sent from the word in the game class
 */
public boolean testCharacter(char testChar, char charFromWord) {
 return equalsIgnoreCase(testChar, charFromWord);
}
/**
 * Method that uses .toLowerCase to make sure the if test ignores the case of the characters
 */
public boolean equalsIgnoreCase(char testChar, char charFromWord) {
 return Character.toLowerCase(testChar) == Character.toLowerCase(charFromWord);
}
/**
 * Checks if the word the user entered is equal to the word selected as the hangman word
 */
public boolean testWord(String testWord, String hangmanWord) {
 return testWord.equalsIgnoreCase(hangmanWord);
}
//Math function returning a random number
public int randWord(int length) {
 return (int)(Math.random() * length);
}

A better way to return a random number is to use the Random class. random.nextInt(length); is preferred. Also, this method does not need to be public, it should be private as it is only called from this method:

public String selectWord(String[] words) {
 return words[randWord(words.length)];
}

I'd suggest that you take a look at the randomElement method in my answer here and use that method. Note that when using a Random object, you should only create it once. So you can put this inside your class:

private final Random random = new Random();

You have three methods that all three have unnecessary number of lines, and can all be static. And all should use proper Javadoc instead of a simple comment above the method.

/**
 * Returns true if the character entered by the user is equal to the character sent from the word in the game class
 */
public boolean testCharacter(char testChar, char charFromWord) {
 return equalsIgnoreCase(testChar, charFromWord);
}
/**
 * Method that uses .toLowerCase to make sure the if test ignores the case of the characters
 */
public boolean equalsIgnoreCase(char testChar, char charFromWord) {
 return Character.toLowerCase(testChar) == Character.toLowerCase(charFromWord);
}
/**
 * Checks if the word the user entered is equal to the word selected as the hangman word
 */
public boolean testWord(String testWord, String hangmanWord) {
 return testWord.equalsIgnoreCase(hangmanWord);
}
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311
//Math function returning a random number
public int randWord(int length) {
 return (int)(Math.random() * length);
}

A better way to return a random number is to use the Random class. random.nextInt(length); is preferred. Also, this method does not need to be public, it should be private as it is only called from this method:

public String selectWord(String[] words) {
 return words[randWord(words.length)];
}

I'd suggest that you take a look at the randomElement method in my answer here and use that method. Note that when using a Random object, you should only create it once. So you can put this inside your class:

private final Random random = new Random();

You have three methods that all three have unnecessary number of lines, and can all be static. And all should use proper Javadoc instead of a simple comment above the method.

/**
 * Returns true if the character entered by the user is equal to the character sent from the word in the game class
 */
public boolean testCharacter(char testChar, char charFromWord) {
 return equalsIgnoreCase(testChar, charFromWord);
}
/**
 * Method that uses .toLowerCase to make sure the if test ignores the case of the characters
 */
public boolean equalsIgnoreCase(char testChar, char charFromWord) {
 return Character.toLowerCase(testChar) == Character.toLowerCase(charFromWord);
}
/**
 * Checks if the word the user entered is equal to the word selected as the hangman word
 */
public boolean testWord(String testWord, String hangmanWord) {
 return testWord.equalsIgnoreCase(hangmanWord);
}
lang-java

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