Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

word game java Problem: you are to write a java program allowing the user to pla

ID: 3712115 • Letter: W

Question

word game java

Problem: you are to write a java program allowing the user to play a word game. There should be two different lists of words: a set of misspelled words and a set of words spelled correctly.

Here are the steps to play the game:

Your program randomly selects a word from the list (it could be a word that is misspelled or a word that is spelled correctly.

The word will be displayed to the user

User needs to decide if the word is spelled correctly or not

If the user’s answer is correct then he/she will gain one point otherwise he/she will lose a point

Ask the user if he/she wants to play again If the answer is no, display the score

If the answer is yes go to the step 1.

How to write your program:

You can use arrays of strings to store the words. How many arrays do you need?

You need to have two files for the words: Misspelled words and correct words

Fill in you arrays by reading from the files

Use a variable to keep track of the score

The randomly selected word cannot be repeated. Therefore you need to keep track of the words already shown to the user.

Use the scanner class to interact with the user.

Use the Random class to generate random numbers to pick a word from the list. How many random numbers do you need? You need two random numbers but why?

Use while loop to repeat the game as long as the user is interested.

Requirements:

Write a method for displaying the description of your program

Write a method for filling in the array from the files

Write a method called play

Write the algorithm for this program before start coding

Provide a description of your program

You must use methods in your program

Appropriate identifier naming

Comments

Proper indentation

Follow the naming conventions

/*Sample output

The word is: Token

mis-spelled? y/n :y

Incorrect!

The word is: File

mis-spelled? y/n :y

Incorrect!

Here is the word: Ermahgerd

Mis-speeled? y/n :y

Correct!

The word is: Throws

mis-spelled? y/n :y

Incorrect!

The word is: Preceding

mis-spelled? y/n :y

Incorrect!

You've played 5 times, give someone else a turn!

You got 0 points.

Would you like to play again? y/n

fdf

Please enter "n" or "y".

fddf

Please enter "n" or "y".

y

Here is the word: Werd

Mis-speeled? y/n :y

Correct!

The word is: Numbers

mis-spelled? y/n :n

Correct!

Here is the word: Werld

Mis-speeled? y/n :y

Correct!

Here is the word: Amurica

Mis-speeled? y/n :n

Incorrect!!

Here is the word: Unicron

Mis-speeled? y/n :n

Incorrect!!

You've played 5 times, give someone else a turn!

You got 1 points.

Would you like to play again? y/n

yes

Please enter "n" or "y".

y

Here is the word: Incorect

Mis-speeled? y/n :y

Correct!

The word is: Program

mis-spelled? y/n :n

Correct!

Here is the word: Progrems

Mis-speeled? y/n :y

Correct!

Here is the word: Fiyuhl

Mis-speeled? y/n :n

Incorrect!!

The word is: Exception

mis-spelled? y/n :y

Incorrect!

You've played 5 times, give someone else a turn!

You got 1 points.

Would you like to play again? y/n

n
/*Sample output
The word is: Token
mis-spelled? y/n :y
Incorrect!
The word is: File
mis-spelled? y/n :y
Incorrect!
Here is the word: Ermahgerd
Mis-speeled? y/n :y
Correct!
The word is: Throws
mis-spelled? y/n :y
Incorrect!
The word is: Preceding
mis-spelled? y/n :y
Incorrect!
You've played 5 times, give someone else a turn!
You got 0 points.
Would you like to play again? y/n
fdf
Please enter "n" or "y".
fddf
Please enter "n" or "y".
y
Here is the word: Werd
Mis-speeled? y/n :y
Correct!
The word is: Numbers
mis-spelled? y/n :n
Correct!
Here is the word: Werld
Mis-speeled? y/n :y
Correct!
Here is the word: Amurica
Mis-speeled? y/n :n
Incorrect!!
Here is the word: Unicron
Mis-speeled? y/n :n
Incorrect!!
You've played 5 times, give someone else a turn!
You got 1 points.
Would you like to play again? y/n
yes
Please enter "n" or "y".
y
Here is the word: Incorect
Mis-speeled? y/n :y
Correct!
The word is: Program
mis-spelled? y/n :n
Correct!
Here is the word: Progrems
Mis-speeled? y/n :y
Correct!
Here is the word: Fiyuhl
Mis-speeled? y/n :n
Incorrect!!
The word is: Exception
mis-spelled? y/n :y
Incorrect!
You've played 5 times, give someone else a turn!
You got 1 points.
Would you like to play again? y/n
n
----jGRASP: operation complete.
*/

----jGRASP: operation complete.

*/

Explanation / Answer

import java.io.*;
import java.util.*;
class game{
public static void main(String []args)throws IOException{
List<String> correctWords = new ArrayList(); //list of correct words
List<String> wrongWords = new ArrayList(); //list of wrong words
populateWords("correctWords.txt",correctWords);
populateWords("wrongWords.txt",wrongWords);
play(correctWords,wrongWords);

}
public static void populateWords(String filename,List<String> list){ //method to populate words into the array
File file = new File(filename);
try{

BufferedReader br = new BufferedReader(new FileReader(file));

String st;
while ((st = br.readLine()) != null){
list.add(st);
  
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void play(List<String> correctWords,List<String> wrongWords)throws IOException{
Random rand = new Random();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(1==1){
int score=0;
for(int i=0;i<4;i++){
//generate a random number 1 or 0. If 1, we display a correct word.If 0 ,
// we display a wrong word
int randomNum = rand.nextInt(2);
String word="";
if(randomNum==0){ //display wrong word
int randomWordPos = rand.nextInt(wrongWords.size());
word = wrongWords.get(randomWordPos);
}
if(randomNum == 1){
int randomWordPos = rand.nextInt(correctWords.size());
word = correctWords.get(randomWordPos);
}
System.out.println("The word is: "+word);
System.out.println("mis-spelled?(y/n)");
String inp = in.readLine();
while((inp.equalsIgnoreCase("y") || inp.equalsIgnoreCase("n"))==false){
System.out.println("Please enter "n" or "y".");
inp = in.readLine();
}
if((inp.equalsIgnoreCase("y") && randomNum==0)||(inp.equalsIgnoreCase("n") && randomNum==1)){
score++;
System.out.println("Correct!");
}
else{
System.out.println("Incorrect!");
}

}
System.out.println("You've played 5 times, give someone else a turn!");
System.out.println("You got "+score+" points.");
System.out.println("Would you like to play again? y/n");
String input=in.readLine();
while((input.equalsIgnoreCase("y") || input.equalsIgnoreCase("n"))==false){
System.out.println("Please enter "n" or "y".");
input = in.readLine();
}   
if(input.equalsIgnoreCase("n")){
break;
}
}
}
}

Sample running of the code:

radas-macOS:Desktop radas$ java game
The word is: stile
mis-spelled?(y/n)
y
Correct!
The word is: whle
mis-spelled?(y/n)
y
Correct!
The word is: mat
mis-spelled?(y/n)
n
Correct!
The word is: whle
mis-spelled?(y/n)
n
Incorrect!
You've played 5 times, give someone else a turn!
You got 3 points.
Would you like to play again? y/n
n
radas-macOS:Desktop radas$ java game
The word is: pumpkin
mis-spelled?(y/n)
n
Correct!
The word is: dog
mis-spelled?(y/n)
f
Please enter "n" or "y".
n
Correct!
The word is: amther
mis-spelled?(y/n)
y
Correct!
The word is: mat
mis-spelled?(y/n)
y
Incorrect!
You've played 5 times, give someone else a turn!
You got 3 points.
Would you like to play again? y/n
y
The word is: boother
mis-spelled?(y/n)
n
Incorrect!
The word is: war
mis-spelled?(y/n)
y
Incorrect!
The word is: word
mis-spelled?(y/n)
n
Correct!
The word is: amther
mis-spelled?(y/n)
n
Incorrect!
You've played 5 times, give someone else a turn!
You got 1 points.
Would you like to play again? y/n
n

contents of the text files:

correctWords.txt

word
hat
dog
cat
whale
mat
pumpkin
bucket
love
riot
war

wrongWords.txt

whle
gto
amther
mther
boother
boott
stile