Lab Assignment Report - Flip a Coin

Categories: Technology

Objective

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.

Procedure

In this lab assignment, we will define a Java program with two main components: a method named coinFlip and the main program.

coinFlip Method

The 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";
}

Main Program

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:

  1. Prompt the user for the number of coin flips using the Scanner class.
  2. Create a Random object to generate random values for coin flips.
  3. Use a while loop to iterate through the desired number of coin flips:
    • Call the coinFlip() method to simulate a coin flip.
    • Print the result (either "Heads" or "Tails") to the console.
    • Decrement the counter for the number of remaining flips.

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--;
    }
}
}

Results

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.

Discussion

This lab assignment demonstrates the use of methods and random number generation in Java.

Get to Know The Price Estimate For Your Paper
Topic
Number of pages
Email Invalid email

By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email

"You must agree to out terms of services and privacy policy"
Write my paper

You won’t be charged yet!

Get quality help now
KarrieWrites
KarrieWrites
checked Verified writer

Proficient in: Technology

star star star star 5 (339)

“ KarrieWrites did such a phenomenal job on this assignment! He completed it prior to its deadline and was thorough and informative. ”

avatar avatar avatar
+84 relevant experts are online
Hire writer

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.

Conclusion

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.

References

1. Java Random Class Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Random.html

Updated: Jan 03, 2024
Cite this page

Lab Assignment Report - Flip a Coin. (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-1-2-flip-the-coin

Lab Assignment Report - Flip a Coin essay
Live chat  with support 24/7

👋 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