Question

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

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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:\n");

System.out.println("Actual\tPredicted\tResult");

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

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

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

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);

}

}

Add a comment
Know the answer?
Add Answer to:
Write this is Java. The program will randomly select a color from one of the 5...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A bag of M&MS was randomly selected from the grocery store shelf, and the color counts...

    A bag of M&MS was randomly selected from the grocery store shelf, and the color counts were: Brown 20, Red 5, Yellow 10, Orange 4, Green 5, Blue 6. Mars Inc. claims that they produce M&Ms with the following distributions Brown 30% Red 20% Yellow 20% Orange 10% Green 10% Blue 10%. 1. n = 50, is there evidence of difference between the random bag that was picked and Mars Inc.'s claims? 2. Graph the results.

  • State the null and alternative hypotheses. It has been rumored that the color distribution of M&M’s...

    State the null and alternative hypotheses. It has been rumored that the color distribution of M&M’s is 30% brown, 20% yellow, 20% red, 10% orange, 10% blue, and 10% green. You suspect this rumor is incorrect and decide to count how many of each color are contained in a randomly chosen bag in order to perform a Chi-Square goodness-of-fit test at a 5% significance level. Color Brown Yellow Red Orange Blue Green Total Observed 27 16 21 12 9 15...

  • JAVA MASTERMIND The computer will randomly select a four-character mastercode. Each character rep...

    JAVA MASTERMIND The computer will randomly select a four-character mastercode. Each character represents the first letter of a color from the valid color set. Our valid color choices will be: (R)ed, (G)reen, (B)lue and (Y)ellow. Any four-character combination from the valid color set could become the mastercode. For example, a valid mastercode might be: RGBB or YYYR. The game begins with the computer randomly selecting a mastercode. The user is then given up to 6 tries to guess the mastercode....

  • yellow red green orange purple blue Select the color of the solution using the indicator thymol...

    yellow red green orange purple blue Select the color of the solution using the indicator thymol blue. When you first add indicator to your Na2CO3 solution, the color is blue When you reached the first equivalence point when moles of acid equals moles of base, the color is orange x. When you weren't paying attention and added too much HCl, the color is purple lx. When you really weren't paying attention and reached the second equivalence point, which is NOT...

  • Write an interactive Java program, ColorRange.java, which when given a wavelength in nanometers will return the...

    Write an interactive Java program, ColorRange.java, which when given a wavelength in nanometers will return the corresponding color in the visible spectrum. Color Wavelength (nm) Violet 380-450 Blue 451-495 Green 496-570 Yellow 571-590 Orange 591-620 Red 621-750 Task You must implement the following using a suitable decision statement. 1. Use a GUI prompt for the user to enter the wavelength, the wavelength should be of type double. 2. For each range (e.g. 380-450) the number on the left is included...

  • 5. One bag contains twenty candies with the following amounts of each color: 5-red, 6-blue, 3-green,...

    5. One bag contains twenty candies with the following amounts of each color: 5-red, 6-blue, 3-green, 2-yellow, and 4-orange. One candy is drawn at random. a. What is the probability of drawing a blue candy? b. What is the probability of drawing a red candy or a green candy? c. You draw one candy and then a second candy without putting back the first. What is the probability of drawing a green candy on the second draw given that you...

  • Write a Java program for a fortune teller. The program should begin by asking the user...

    Write a Java program for a fortune teller. The program should begin by asking the user to enter their name. Following that the program will display a greeting. Next, the program will ask the user to enter the month (a number 1-12) and day of the month (a number 1-13) they were born. Following that the program will display their zodiac sign. Next, the program will ask the user their favorites color (the only choices should be: red, green and...

  • IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one...

    IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one and 100. Then it will ask the user to make a guess as to what the number is. If the guess is incorrect, but is within the range of 1 to 100, the user will be told if it is higher or lower and then asked to guess again. If the guess was outside the valid range or not readable by Scanner class, a...

  • 17.-/1 pointsBBUnderStat1242.034 At Litchfield College of Nursing, 87% of incoming freshmen nursing students are female and...

    17.-/1 pointsBBUnderStat1242.034 At Litchfield College of Nursing, 87% of incoming freshmen nursing students are female and 13 % are male. Recent records indicate that 70% of the entering female students will graduate with a BSN degree, while 80 % of the male students will obtain a BSN degree. If an incoming freshman nursing student is selected at random, find the following probabilities, (Enter your answers to three decimal places.) (a) P(student will graduate I student is female) (b) P(student will...

  • For this lab you will write a Java program using a loop that will play a...

    For this lab you will write a Java program using a loop that will play a simple Guess The Number game. Th gener e program will randomly loop where it prompt the user for a ate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a r has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT