Write a program that tests your ESP (extrasensory perception). The program shoul
ID: 3599271 • Letter: W
Question
Write a program that tests your ESP (extrasensory perception). The program should randomly select the name of a color from the following list of words:
Red; Green, Blue, Orange, Yellow
To select a word from the list, the program can generate a random number. For example, if the number is 0, the selected word is Red; if the number is 1, the selected word is Green; and so forth.
Next the program should ask the user to enter a color that the computer has selected. Check the user input for validity .After the user entered his or her guess (valid) , the program should display the name of the randomly selected color. The program should repeat 10 times and then display the number of times the user correctly guesses the selected color. Be sure to modularize the program into methods that perform each major task.
//Main method--> calls functions
public static void main(String[] args){
}
/**
computerChoice method
@return The computer's choice of "red“, ”green", “blue”, or "orange".
*/
public static String computerChoice(){
}
/**
userChoice method
@return The user's choice of "red“, ”green", “blue”, or "orange".
*/
public static String userChoice(){
}
/**
isValidChoice method
@param choice A string holding the user's choice.
@return true if the choice is valid, false otherwise.
*/
public static boolean isValidChoice(String choice){
}
/**
The determineWinner method displays the winner.
@param computer The computer's choice.
@param user The user's choice.
*/
public static void determineWinner(String computer, String user){
}
Explanation / Answer
import java.util.ArrayList;
import javax.swing.colorchooser.ColorSelectionModel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Guess {
public static ArrayList<String> colors = new ArrayList<>();
static int win=0;
public static void main(String[] args) throws IOException{
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Orange");
String choice;
String computerChoice;
for(int i=0;i<10;i++){
System.out.println("Enter a color from the list:[Red,Green,Blue,Orange]");
computerChoice = computerChoice();
choice=userChoice();
if(isValidChoice(choice)) {
determineWinner(computerChoice, choice);
System.out.println("Randomly selected color is: "+computerChoice);
}
else {
System.out.println("Invalid color input");
}
}
System.out.println("You won "+ win +" times.");
}
/**
computerChoice method
@return The computer's choice of "red“, ”green", “blue”, or "orange".
*/
public static String computerChoice(){
int randomNum = 1 + (int)(Math.random() * 4);
if(randomNum==1)
return colors.get(0);
else if(randomNum==2)
return colors.get(1);
else if(randomNum==3)
return colors.get(2);
else
return colors.get(3);
}
/**
userChoice method
@return The user's choice of "red“, ”green", “blue”, or "orange".
* @throws IOException
*/
public static String userChoice() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
/**
isValidChoice method
@param choice A string holding the user's choice.
@return true if the choice is valid, false otherwise.
*/
public static boolean isValidChoice(String choice){
return colors.contains(choice);
}
/**
The determineWinner method displays the winner.
@param computer The computer's choice.
@param user The user's choice.
*/
public static void determineWinner(String computer, String user){
if(computer.equals(user)) {
System.out.println("You guessed it right");
win++;
}
else
System.out.println("Wrong guess");
}
}
/*
Sample output:
Enter a color from the list:[Red,Green,Blue,Orange]
Red
Wrong guess
Randomly selected color is: Blue
Enter a color from the list:[Red,Green,Blue,Orange]
Reda
Invalid color input
Enter a color from the list:[Red,Green,Blue,Orange]
Blue
Wrong guess
Randomly selected color is: Orange
Enter a color from the list:[Red,Green,Blue,Orange]
Blue
You guessed it right
Randomly selected color is: Blue
Enter a color from the list:[Red,Green,Blue,Orange]
Orange
Wrong guess
Randomly selected color is: Blue
Enter a color from the list:[Red,Green,Blue,Orange]
Green
Wrong guess
Randomly selected color is: Red
Enter a color from the list:[Red,Green,Blue,Orange]
Green
Wrong guess
Randomly selected color is: Red
Enter a color from the list:[Red,Green,Blue,Orange]
Red
Wrong guess
Randomly selected color is: Blue
Enter a color from the list:[Red,Green,Blue,Orange]
Red
Wrong guess
Randomly selected color is: Blue
Enter a color from the list:[Red,Green,Blue,Orange]
Red
Wrong guess
Randomly selected color is: Blue
You won 1 times.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.