How do I fix my code, so it passes all of the test in the test method Number 1)*
ID: 3700906 • Letter: H
Question
How do I fix my code, so it passes all of the test in the test method
Number 1)****************************test method******************************************
public void testIsValidWord() {
assertFalse(Hangman.isValidWord("Cool"));//false because only 4 letters
assertTrue(Hangman.isValidWord("anxiety"));
assertFalse(Hangman.isValidWord("aapaple"));//false because there are 3 As
assertFalse(Hangman.isValidWord("renovate the room"));//fall because there are spaces
}
*************************************************************************
my code
*************************************************************8
public static boolean validW(String word) {
int alphOccur=0;
boolean flag=true;
for(int i=0;i<word.length()-1;i++) {
if(word.charAt(i)>='a'&&word.charAt(i+1)<='z'||word.charAt(i)>='A'&&word.charAt(i+1)<='Z') {
flag = true;
}
if(alphOccur+1 >=3) {
flag=false;
}
for(int j=i+1;j<word.length()-i;j++) {
if(word.charAt(i)==word.charAt(j)) {
alphOccur++;
}
}
}
if(word.length()>=6) {
flag=true;
}
System.out.println(flag);
return flag;
}
--------------Number 2---------------------------------------------------------------------------------------------------------
//how do i
//test method
public void testMakeGuess() {
assertEquals("optio_", Hangman.makeGuess("g", "option", "optio_"));
assertEquals("option", Hangman.makeGuess("n", "option", "optio_"));
assertEquals("opt___", Hangman.makeGuess("opt", "option", "______"));
assertEquals("_p_ion", Hangman.makeGuess("ion", "option", "_p____"));
assertEquals("option", Hangman.makeGuess("pt", "option", "op_ion"));
}
//my code
/*the disguisedWord is the blank string with underscores like _ _ _ _ _ _ _, everytime a user guess a letter, if the letter is in the secretWord, it it assign the letter at the spot of the word in the blank space, like the test method.
*/
public static String mGuess(String guess, String secretWord, String disguisedWord) {
String newWord=disguisedWord;
if(secretWord.contains(guess)) {
for(int i=0;i<secretWord.length();i++) {
if(secretWord.charAt(i)==guess) {
newWord.charAt(i)=guess;
}
}
}
else {
newWord=disguisedWord;
}
return newWord;
Explanation / Answer
Modified Method 1:
public static boolean isValidWord(String word) {
boolean flag = true;
if(word.length()<6) {
flag = false;
System.out.println(flag);
return flag;
}
if(!word.matches("[a-zA-z]+")) flag = false;
int alphOccur;
for(int i=0;i<word.length()-1;i++) {
alphOccur = 1;
for(int j=i+1;j<word.length();j++) {
if(word.charAt(i)==word.charAt(j)) {
alphOccur++;
}
}
if(alphOccur>=3) flag=false;
}
System.out.println(flag);
return flag;
}
Modified Method 2:
public static String makeGuess(String guess, String secretWord, String disguisedWord) {
String newWord=disguisedWord;
if(secretWord.contains(guess)) {
int id = secretWord.indexOf(guess);
int m = guess.length();
StringBuffer buf = new StringBuffer(disguisedWord);
buf.replace(id, id+m, guess);
newWord = new String(buf);
}
return newWord;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.