I have been searching online for a program that will:
-reads in number of times a user wants a coin flipped
-flip the coin that many times, printing out result of each coin flip
-print out the total number of heads flipped and total number of tails flipped

the application should contain the class method called flipCoin( )

Can anyone help out?

Member Avatar for ztini
public class Finger {
	public static void flip(String finger) {
		// your code here
	}
	
	public static void main(String[] args) {
		Finger.flip("middle");
	}
}

Wait, that's not quite right....

Excuse ztini's rudeness, but I think what s/he meant to say was that we aren't going to solve a problem for you u need to show effort and then we can help you out. However, if you are at a loss as to where to turn, you might want to look in the Java API under the Random class or at the random() method in the Math class. Good luck!

Hey! You're on the right track with your idea—it’s a great beginner project to practice loops, input, and class methods in Python.

Here’s a sample program that does exactly what you're asking. It defines a class with a flipCoin() method, reads in how many times the user wants to flip a coin, prints each result, and then shows the totals at the end:

import random

class CoinFlipper:
    def __init__(self):
        self.heads = 0
        self.tails = 0

    def flipCoin(self, times):
        for i in range(times):
            flip = random.choice(['Heads', 'Tails'])
            print(f"Flip {i+1}: {flip}")
            if flip == 'Heads':
                self.heads += 1
            else:
                self.tails += 1

        print("\nResults:")
        print("Total Heads:", self.heads)
        print("Total Tails:", self.tails)

# Main program
try:
    flips = int(input("How many times would you like to flip the coin? "))
    flipper = CoinFlipper()
    flipper.flipCoin(flips)
except ValueError:
    print("Please enter a valid integer.")

This version uses a class method called flipCoin() and handles user input nicely. It will flip a coin as many times as requested, show each result, and summarize at the end.

Let me know if you want a version with file output or a GUI later on—happy to help!

commented: And only 14 years too late -3
commented: Really late. Try to avoid this in the future. -4
commented: Thanks For sharing +0
commented: You are very late, but you give an authoritative answer. +0
commented: I searched the web and found your answer. This answer is very useful. Thanks For sharing +0
commented: Very helpful +0
commented: Not java -4

Better late than never! Thanks for your post. It looks like it's really helped a few people already.

It looks like it's really helped a few people already.

Yeah, 4 new members with consecutive user ID's, smelling like sock-puppet accounts.

commented: I saw that as well. +1 for you. +17

Yeah, 4 new members with consecutive user ID's, smelling like sock-puppet accounts.

I realized that after I posted. I’m on my phone on the sofa so it wasn’t as easy to tell. Although I still can’t see the motivation.

Edit: Nevermind. Perhaps they were trying to improve their member reputation/quality score?? (Won’t work because you need reputation to give reputation.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.