Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Coin {
- public int value;
- private int random;
- private final int HEADS = 1; // let 1 be heads and 0 be tails
- public Coin () {}
- public Coin (int v) {
- value = v;
- }
- public void flip () {
- random = (int) (Math.random() * 2); // returns 0 or 1
- }
- public boolean isHeads() { return random==HEADS;}
- }
- class HeadsOrTails {
- public static void main (String[] args) {
- Coin twoPence = new Coin(2);
- twoPence.flip();
- System.out.println( "The "+twoPence.value+"p coin was flipped"
- +" and it came up "+( twoPence.isHeads() ? "heads" : "tails"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement