To install StudyMoose App tap and then “Add to Home Screen”
Save to my list
Remove from my list
The objective of this lab assignment is to create a Java program that simulates flipping a coin and generates "Heads" or "Tails" based on random values. The program should take the desired number of coin flips as input, call the coinFlip() method accordingly, and display the results.
In this lab assignment, we will define a Java program with two main components: a method named coinFlip
and the main
program.
coinFlip
MethodThe coinFlip
method takes a Random
object as an argument and returns either "Heads" or "Tails" based on a random value (1 or 0).
The value 1 represents "Heads," and 0 represents "Tails." The method is defined as follows:
public static String coinFlip(Random rand) {
int result = rand.nextInt(2);
return (result == 0) ? "Tails" : "Heads";
}
The main
program reads the desired number of coin flips as input, calls the coinFlip()
method repeatedly according to the number of coin flips, and outputs the results. It follows these steps:
Scanner
class.Random
object to generate random values for coin flips.while
loop to iterate through the desired number of coin flips:
coinFlip()
method to simulate a coin flip.
import java.util.Scanner;
import java.util.Random;
public class Lab1_2 {
public static String coinFlip(Random rand) {
int result = rand.nextInt(2);
return (result == 0) ? "Tails" : "Heads";
} public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
// Prompt for the number of coin tosses
System.out.print("How many times do you want to flip the coin? ");
int numOfFlips = scnr.nextInt();
Random rand = new Random();
// Simulate coin flips and display results
while (numOfFlips > 0) {
System.out.println(coinFlip(rand));
numOfFlips--;
}
}
}
The program successfully simulates coin flips based on the user's input and displays the results as "Heads" or "Tails" in accordance with random values.
This lab assignment demonstrates the use of methods and random number generation in Java.
The coinFlip
method encapsulates the logic for simulating a coin flip, while the main
program interacts with the user, generates random values, and displays the outcomes. By using a Random
object, the program ensures that the results are unpredictable and represent a fair coin flip.
In conclusion, this lab assignment has provided hands-on experience in Java programming by creating a coin flipping simulation. The program successfully achieves its goal of generating "Heads" and "Tails" based on random values, and it can be easily extended for further experimentation with randomization and probability.
1. Java Random Class Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Random.html
Lab Assignment Report - Flip a Coin. (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-1-2-flip-the-coin
👋 Hi! I’m your smart assistant Amy!
Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.
get help with your assignment