How to create a coin flip program
ok well to start off this is my first time on this forum and hopefully im in the right section, im in a highschool computer programing class and would like to do something with my coin flip class over the winter break, i would like to create a program to click a button and it displayes an animation of a coin fliping and then landing on either heads or tails, this is my current code. (oh yeah using blueJ, i dont know if there is another java programing program just thought id mention that just incase.)
Code Java:
public class Coin
{
// instance variables
private double value;
/**
* Constructor for objects of class Coin
*/
public Coin()
{
// initialise instance variables
value = 0;
}
/**
* flip will create a random number between 0 and 1 and store it in value
*
*/
public void flip()
{
value = Math.random();
}
/** isHeads will return true if the value is less then 0.5
*
*@return true if value < 0.5
* false otherwise
*/
public boolean isHeads()
{
if (value < 0.5)
return true;
return false;
}
/** istails will return true if the value is greater then or equal to 0.5
*
*@return true if value > 0.5
* false otherwise
*/
public boolean isTails()
{
if (value >= 0.5)
return true;
return false;
}
}
Re: How to create a coin flip program
The way I would recommend getting started with Java GUI programming is with Oracle's Java Swing tutorials (or a good book if you have access to one).
I would recommend getting familiar with creating simple GUI interfaces (buttons, labels, custom painting, etc.) before attempting something like an animation.
The animation part can be either difficult or easy, I would recommend starting out with a series of still coin shots and simply switching between the pictures. If you're feeling ambitious, you can try 3D animations via JOGL or LWJGL (jMonkeyEngine might also be a viable and easier alternative).
I've never personally used BlueJ, I'm kind of an Eclipse fan myself :P Another good IDE is Netbeans. I believe all three are free (Eclipse and Netbeans are open source, not sure about BlueJ).