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.

  • Using C++ COLOR MIXER- The color red, yellow, and blue are primary colors because they cannot...

    Using C++ COLOR MIXER- The color red, yellow, and blue are primary colors because they cannot be made by mixing other colors. When you mix 2 primary colors you get the following: When you mix red and blue, you get purple. When you mix red and yellow, you get orange. When you mix blue and yellow, you get green. Write a program to ask the user for 2 primary colors (an error if they are not primary). Then display the...

  • Write an interactive C++ program that asks a user to input the color code of a...

    Write an interactive C++ program that asks a user to input the color code of a resistor and determines its value and tolerance Tell the user how to input the color rings “Instructions” Tell the user to input the colors one after the other After the program receives the information it will display the resistance and tolerance Output an error message if the user enters an invalid color code (no garbage values should be displayed), and ask for another input...

  • 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...

  • 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 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...

  • Number Guessing Game Games Write program in C++. For this game, the computer will select a...

    Number Guessing Game Games Write program in C++. For this game, the computer will select a random number between 1 and 100 (inclusive). The computer will then ask the (human) player to guess the number the computer has selected. After the player’s guess is input to the computer, the computer will output one of three responses, depending on the relationship of the number the player guessed to the number the computer selected: “Your guess was too low.” “Your guess was...

  • 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...

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