6.
• Examine FlipCoin.java:
– Execute the following program and observe the random number that chance generated.
– If chance < 0.5, record the result as “heads”; else record the result as “tails.”
– Repeat this many time.
import java.util.Random;
public class FlipCoin {
public static void main(String[] args) {
// 50% chance heads, 50% chance tails
Random rand = new Random();
double chance = rand.nextDouble();
System.out.println(chance);
}
}
Exercise 2
• Examine RockPaperScissor.java.
– Perform the following:
– Simulate the RockPaperScissor game by generating a random integer number in the range of 0 to 3.
– Compare the generated number with the following numbers:
– if number=0 : “rock”
– if number=1: “paper”
– if number=2: “scissors”
– Record the result and repeat many times.
public class RockPaperScissor {
public static void main(String[] args) {
}
}
7.
Exercise 2
• On paper, evaluate the following Java statements
and record the results:
– Math.abs(-1.23)
– Math.pow(3, 2)
– Math.sqrt(121.0) - Math.sqrt(256.0)
– Math.abs(Math.min(-3, -5))
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Question 6 :
Here a new java program with name "FlipCoin.java" is created, which contains following code.
FlipCoin.java :
import java.util.Random;
public class FlipCoin { //class
//main method
public static void main(String[] args) {
// 50% chance heads, 50% chance tails
//creating object of random class
Random rand = new Random();
//generate random number
double chance = rand.nextDouble();
//checking value of chance
if(chance < 0.5)
{
System.out.println("heads"); //print result
}
else
{
System.out.println("tails"); //print result
}
}
}
======================================================
Output : Compile and Run above program to get the screen as shown below
Screen 1 :FlipCoin.java

Screen 2:

**********************************
Ex.2 :
Here a new java program with name "RockPaperScissor.java" is created, which contains following code.
RockPaperScissor.java :
import java.util.Random;
public class RockPaperScissor {
public static void main(String[] args) {
//creating object of random
class
Random rand = new Random();
//generate random number
int number = rand.nextInt(3);
//checking number
if(number==0)
{
System.out.println("rock");//print
}
else if(number==1)
{
System.out.println("paper");
}
else if(number==2)
{
System.out.println("scissors");
}
}
}
======================================================
Output : Compile and Run above program to get the screen as shown below
Screen 1 :RockPaperScissor.java

*****************************
Question 7 :
Below is the screen for output

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.
6. • Examine FlipCoin.java: – Execute the following program and observe the random number that chance...
Exercise 1 • Examine ShoppingCart.java. – Perform the following: – Use the indexOf method to get the index for the space character (" ") within custName. Assign it to spaceIdx. – Use the substring method and spaceIdx to get the first name portion of custName. Assign it to firstName and print firstName. public class ShoppingCart { public static void main (String[] args){ String custName = "Steve Smith"; String firstName; int spaceIdx; // Get the...
Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified name to access the Jlabel component with an import statement. – To import classes from the util package, replace multiple import statements with a single import statement. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.util.Calendar; import java.util.Date; public...
JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in); System.out.println("Seed:"); int seed = scanner.nextInt(); System.out.println("Length"); int length = scanner.nextInt(); Random random...
# JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...
Ihave the following code of which the structure I do not want to change. How can I upgrade it so that one number can not be chosen twice on the same ine? package lotterirader import javax.swing. import java.util.Random public class Bandomnumbers ( public static void main(StringD args) Random myRan new Random0 String result int randomNum 1; row 3; row++ for (int row result+"Line"+ row +- for (int i 1:io 6;i++ randomNum myRan.nextint(99)+1 result randomNum+a result result.replaceAl, S.")+"n JOptionPane.showMessageDialoginul, result)
my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first number current num in main method but when I store after 10 second i'm not geting the update current number. NOTE: ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the...
my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...
I need help asking the user to half the value of the displayed random number as well as storing each number generated by the program into another array list and displayed after the game is over at the end java.util.*; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); ArrayList<Integer> data = new ArrayList<Integer>(); int count = 0; while (!choice.equals("No")) { int randomInt =...
import java.util.Scanner; public class Client{ public static void main(String args[]){ Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5); Scanner keyboard = new Scanner(System.in); int i = 0; int total = 0; while(true){ i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue(); dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...
my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...