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

USING JAVA Write a program that lets the user guess whether the flip of a coin r

ID: 3846666 • Letter: U

Question

USING JAVA

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 0 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 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 in a rectangle. The 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 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 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


/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
class HeadTail
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int coin = rand.nextInt(2);
System.out.println(" Enter 1 for Head, 0 for Tail:");
int guess = scan.nextInt();
if(coin == guess)
{
System.out.print(" Correct");
}
else
{
System.out.println(" Incorrect");
}
}
}
OUTPUT
Enter 1 for Head, 0 for Tail:1
Incorrect

Enter 1 for Head, 0 for Tail:0
Correct
-------------------------------------------------------------------------------------------------------------------------------------------------------------


/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
class Lottery
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int lot = rand.nextInt() * 1000;
System.out.println(" Guess your 3 digit number:");
int guess = scan.nextInt();
int digit1 = lot / 100;
int digit2 = (lot % 100) / 10;
int digit3 = lot % 10;
int guess1 = guess / 100;
int guess2 = (guess % 100) / 10;
int guess3 = guess % 10;
System.out.println(" Lottery Number = " +lot);
if(guess == lot)
System.out.println(" Number Matches: You Won $10,000");
else if((guess1 == digit2 && guess2 == digit1 && guess3 == digit3)
|| (guess1 == digit2 && guess1 == digit3 && guess3 == digit1)
|| (guess1 == digit3 && guess2 == digit1 && guess3 == digit2)
|| (guess1 == digit3 && guess2 == digit2 && guess3 == digit1)
|| (guess1 == digit1 && guess2 == digit2 && guess3 == digit2))
System.out.println(" All Digits are Matched: You won $3,000");
else if(guess1 == digit1 || guess1 == digit2 || guess1 == digit3 || guess2 == digit1
|| guess2 == digit2 || guess2 == digit3 || guess3 == digit1
|| guess3 == digit2 || guess3 == digit3)
System.out.println(" One Digit is Matched : You won $1,000");
else
System.out.println(" No Match");
}
}


OUTPUT


Guess your 3 digit number:012
Lottery Number = 2041679592
One Digit is Matched : You won $1,000

---------------------------------------------------------------------------------------------------------------------------------------------------------


import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
class Rectangle
{
public static void main(String args[])
{
int width = (int)((Math.random() * 101) - 50);
int height = (int)((Math.random() * 201) - 100);
System.out.println(" Random Coordinates of Rectangle");
System.out.println(" Width = 100 Height = 200 Width = " +width +" Height = " +height);
  
}
}

OUTPUT


Random Coordinates of Rectangle
Width = 100, Height = 200 Width = 14 Height = 28
---------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
class SRP
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int c = (int)(Math.random() * 3);
System.out.println(" Scissor -> 0, Rock -> 1, Paper -> 2: ");
int u = scan.nextInt();
System.out.println(" The computer is: ");
switch(c)
{
case 0:
System.out.print("Scissor ");
break;
case 1:
System.out.print("Rock ");
break;
case 2:
System.out.print("Paper ");
}
System.out.print(" You are: ");
switch(u)
{
case 0:
System.out.print(" Scissor");
break;
case 1:
System.out.print(" Rock");
break;
case 2:
System.out.print(" Paper");
}
if(c == u)
{
System.out.println(" too. It's a Draw");
}
else
{
boolean win = (c == 0 && u == 1) || (c == 1 && u == 2) || (c == 2 && u == 0);
if(win)
System.out.println(" You Won");
else
System.out.println(" You Lose");
}
}
}

OUTPUT


Scissor -> 0, Rock -> 1, Paper -> 2:

The computer is:
Rock
You are: Rock too. It's a Draw


Scissor -> 0, Rock -> 1, Paper -> 2:

The computer is:
Scissor
You are: Paper You Lose