Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 15 December 2012

JAVA Program: Coin Toss

Posted on 09:00 by Unknown
This is a JAVA program for an automatic coin toss; you enter the number of tosses you want to make and then the program does the tosses by itself, where it also checks to see if the result of the toss is heads or tails. This is from Chapter 5 of Tony Gaddis' Starting out with JAVA, 4th Edition.

Here's the program; a series of details follows onwards.

import javax.swing.JOptionPane;
import java.util.Random;

public class Chapter5CoinToss
{
    public static void main(String[] args)
    {
        String input;
        int numberOfTosses;
       
        input =
            JOptionPane.showInputDialog("How many times do you want this virtual coin to be tossed?");
        numberOfTosses = Integer.parseInt(input);
       
        coinToss(numberOfTosses);
       
        JOptionPane.showMessageDialog(null, "Thank you and come again!");
       
    }
   
    public static void coinToss(int number)
    {
        int numberOfHeads = 0,
            numberOfTails = 0,
            currentNumber,
            currentToss = 1;
   
        Random tossMachine = new Random();
   
        while (currentToss <= number)
        {
        currentNumber = tossMachine.nextInt(1 + 1);
       
        if (currentNumber == 0)
            numberOfHeads++;
        else if (currentNumber != 0)
            numberOfTails++;
       
        currentToss++;
        }
       
        JOptionPane.showMessageDialog(null, "Out of the number of tosses you asked, there were " +
                                      numberOfHeads + " heads resulted and " + numberOfTails + " tails.");
       
    }
}


It is simply three JOptionPane windows where the first, you enter the number of tosses to execute. Then, through one of the Random class methods and a custom method named coinToss consisting of four integer variables, one Random object, a while loop, an if-else-if statement, and the JOptionPane's showMessageDialog method, the program executes the tosses and checks if the resulting toss is heads or tails.

There are no images used; it's all computer logic, in binary numbers.  All of this invisible process happens within just less than 50 lines of code.

Well, I'll see you around! 
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • JAVA Program: Military Time to Real Time
    Hello, people. How you doing? I'm really busy with my college homework, but I am working so hard to pass my courses and continue in my s...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • Java Programming : For Loop Example
    Here's an example of using a for loop in JAVA. This program uses a loop inside another to create a list of integers where the number on ...
  • Java Program : Fat Gram Calculator
    Here's another program I want to share with you guys, which is simply a program that multiplies the number of grams of fat in food by ni...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • JAVA Program: Lottery Class
    Here's a nother JAVA Program I want to share with you guys , which is a demonst ratio n of the Lottery class I w as as ked to design fro...
  • List of Played N64 Games
    Hello, guys. Let me introduce to you a list of N64 games I have played when I was young; the N64 was one of the two consoles I had, besides ...
  • Inequality in Canadian Women through Politics and Employment [Gender and Knowledge Essay]
    This is a research essay I did in my Gender and Knowledge class with Rachel Morris as my instructor at Champlain. It was finished on April ...

Blog Archive

  • ►  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ►  June (12)
    • ►  May (12)
    • ►  April (2)
    • ►  January (1)
  • ▼  2012 (24)
    • ▼  December (13)
      • Need some help for other holidays to come
      • JAVA Program: Coin Toss
      • JAVA Program: Lottery Class
      • JAVA Program: Car Class
      • String Letter Counter (JAVA Program)
      • Fun with Music, Drama and Comedy
      • JAVA Program - City Water Company Bill [The use of...
      • Inequality in Canadian Women through Politics and ...
      • Super Mario Bros: The Fighter Attack [Game Idea ma...
      • Princess Peach In India [Story Idea]
      • The Black Glove [Short Poem by me]
      • Longueuil RTL #83 (Sec. 5 English Short Story)
      • Super Mario Script 1 (v 2.3) [Super Mario Galaxy f...
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile