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

Write this is Java. The program will randomly select a color from one of the 5 g

ID: 3879061 • Letter: W

Question

Write this is Java. The program will randomly select a color from one of the 5 green red blue yellow orange. And ask you to guess which was randomly selected. It will select a random color by choosing a random number from 0-4 0 can represent green and 1 red. It will ask you to guess the color randomly selected and reveal what it chose after you guessed. After it will do the same 10 times loop and will display how many you got correct. Separate the methods for each of the main task. Write this is Java. The program will randomly select a color from one of the 5 green red blue yellow orange. And ask you to guess which was randomly selected. It will select a random color by choosing a random number from 0-4 0 can represent green and 1 red. It will ask you to guess the color randomly selected and reveal what it chose after you guessed. After it will do the same 10 times loop and will display how many you got correct. Separate the methods for each of the main task.

Explanation / Answer

Code is given below:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.List;

import java.util.Random;

import java.util.concurrent.ThreadLocalRandom;

public class Game {

public static void guess(int[] correct, HashMap<Integer, String> colorMatrix, List<String> actual, List<String> predicted) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

for(int i=0;i<correct.length;i++) {

int randomNum = ThreadLocalRandom.current().nextInt(0, 4 + 1);

String actualColour = colorMatrix.get(randomNum);

System.out.println("Guess the Colour:(Green,Red,Blue,Yellow,Orange)");

String predictedColour = br.readLine();

if(actualColour.equals(predictedColour)) {

System.out.println("You guessed it correct. It is "+actualColour);

correct[i] = 1;

}

else {

System.out.println("Sorry it is "+actualColour);

correct[i] = 0;

}

actual.add(actualColour);

predicted.add(predictedColour);

}

}

public static void printResult(int[] correct, HashMap<Integer, String> colorMatrix, List<String> actual, List<String> predicted) {

System.out.println("Please Find the Result Below: ");

System.out.println("Actual Predicted Result");

for(int i=0;i<correct.length;i++) {

System.out.print(actual.get(i)+" ");

System.out.print(predicted.get(i)+" ");

if(correct[i] == 1) {

System.out.println("Correct");

}

else {

System.out.println("Wrong");

}

}

}

public static void main(String[] args) throws IOException {

HashMap<Integer, String> colorMatrix = new HashMap<Integer, String>();

int[] correct = new int[10];

List<String> actual = new ArrayList<String>();

List<String> predicted = new ArrayList<String>();

colorMatrix.put(0, "Green");

colorMatrix.put(1, "Red");

colorMatrix.put(2, "Blue");

colorMatrix.put(3, "Yellow");

colorMatrix.put(4, "Orange");

//Guess loop for 10 times

guess(correct,colorMatrix,actual,predicted);

//Printing the result

printResult(correct,colorMatrix,actual,predicted);

}

}