Write a program that lets the user guess whether the flip of a coin results in h
ID: 3846624 • Letter: W
Question
Write a program that lets the user guess whether the flip of a coin results in heads or tails. The program randomly generates an integer theta or 1, which represents head or tail. The program prompts the user to enter a guess and reports whether the guess is correct or incorrect. Revise Listing 3.8 Lottery java to generate a lottery of a three-digit number. The program prompts the user to enter a three-digit number and determines whether the user wins according to the following rules: if the user input matches the lottery number in the exact order, the award is $10,000. If all digits in the user input match all digits in the lottery number, the award is $3,000. If one digit in the user input matches a digit in the lottery number, the award is $1,000. Write a program that displays a random coordinate rectangle is centered at (0, 0) with width 100 and height 200. Write a program that plays the popular scissor rock paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number theta, 1, or 2 representing scissor rock, and paper. The program prompts the user to enter a number theta, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here is a sample run:Explanation / Answer
//TossingACoin program
package com;
import java.util.Scanner;
public class TossingACoin {
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the guess whteher trail(0) or head(1)");
int choice= s.nextInt();
double rnum=2*Math.random();
int rnum1=(int)rnum;
//System.out.println(rnum1+" "+choice);
if(choice == rnum1)
System.out.println("Guess is correct and User wins");
else
System.out.println("Sorry ! Guess is Wrong and Computer Wins");
}
}
sample output
Enter the guess whteher trail(0) or head(1)
1
Guess is correct and User wins
=================================================================================
//program for lottery
package com;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Lottery {
public static void main(String args[])
{
int c;
Scanner s=new Scanner(System.in);
System.out.println("Enter a three digit Number");
int choiceNumber=s.nextInt();
Random rnd = new Random();
int n = 100 + rnd.nextInt(900);
System.out.println("Lottery Number generated by computer "+n);
if(choiceNumber == n)
{
System.out.println("yahoo ! you won $10000");
}
String user = Integer.toString(choiceNumber);
String computer = Integer.toString(n);
char[] chars1 = user.toCharArray();
char[] chars2 = computer.toCharArray();
Arrays.sort(chars1);
Arrays.sort(chars2);
if(Arrays.equals(chars1, chars2))
System.out.println("Your guess is right but the place of digits in number are different You have won $3000 ");
int count=0;
for(int i=0;i<chars1.length;i++)
{
if(chars1[i]==chars2[i])
count++;
}
if(count<=2)
System.out.println("You have won $1000");
else
System.out.println("sorry Better luck Next Time");
}
}
output for Lottery
------------------
Enter a three digit Number
789
Lottery Number generated by computer 887
You have won $1000
===================================================================================//RandomCordinatesOfRectangle program
package com;
import java.util.Scanner;
public class RandomCordinatesOfRectangel {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int x, y;
// x coordinate value must be from -50 to 50 because rectangle is centered at(0,0)
x = (int)(Math.random() * 101) - 50;
// ycoordinate value must be from -100 to 100 because rectangle is centered at(0,0)
y = (int)(Math.random() * 201) - 100;
System.out.print("Random Generated coordinates of rectangle is (" + x + ", " + y + ")");
}
}
output for RandomCordinatesOfRectangle
Random Generated coordinates of rectangle is (-16, 78)
===================================================================================
//RockPaperScissor program
package com;
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissor {
public static void main(String[] args) throws java.lang.Exception {
Scanner s = new Scanner(System. in );
Random rnd = new Random();
int input;
int B = 1;
System.out.println("Pick 0,1, or 2 for:");
System.out.println("SCISSOR (0) ROCK (1) or PAPER(2) ");
while (B != 0) {
// 1 = rock
// 2 = paper
// 3 = scissors
// N= Ilya.nextInt();
int Rock = 1, Paper = 2, Scissor = 0;
input = s.nextInt();
int randomNumber = rnd.nextInt(2 - 1 + 1) + 1;
if (randomNumber == Rock) {
if (input == Rock) {
System.out.println("Rock Vs. Rock: TIE");
} else if (input == Paper) {
System.out.println("Paper Vs. Rock: YOU WIN");
} else if (input == Scissor) {
System.out.println("Scissor Vs. Rock: YOU LOSE");
}
} else if (randomNumber == Paper) {
if (input == Rock) {
System.out.println("Rock Vs. Paper: YOU LOSE");
} else if (input == Paper) {
System.out.println("Paper Vs. Paper: TIE");
} else if (input == Scissor) {
System.out.println("Scissor Vs. Paper: YOU WIN");
}
} else if (randomNumber == Scissor) {
if (input == Rock) {
System.out.println("Rock Vs. Scissor: YOU WIN");
} else if (input == Paper) {
System.out.println("Paper Vs. Scissor: YOU LOSE");
} else if (input == Scissor) {
System.out.println("Scissor Vs. Scissor: TIE");
}
}
}
}
}
Output for RockPaperScissor
---------------------------
Pick 0,1, or 2 for:
SCISSOR (0) ROCK (1) or PAPER(2)
1
Rock Vs. Paper: YOU LOSE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.