Jump to content

  • Log In with Google      Sign In   
  • Create Account

m3mb3rsh1p

Member Since 27 Mar 2008
Offline Last Active Aug 09 2013 09:39 PM

#4989608 Virtual Coin Flip

Posted by m3mb3rsh1p on 12 October 2012 - 05:28 PM

Hi. I'm also a beginner so I thought I'd share my solution. I'm stuck at how to count the flips without loops because I don't quite yet have a firm grasp of the Java utils.

I enjoyed this. Thanks to all!
import java.util.Random;
import java.util.Arrays;
import java.util.Collections;

public class Coin
{
public enum Side { heads, tails, edge };

public Coin()
{
	_side = Side.edge;
}
public Coin(Side side)
{
	_side = side;
}
public Coin(Random generator)
{
	flip(generator);
}
public void flip(Random generator)
{
	double value = generator.nextDouble();

	if(value < 0.5 )
	{_side = Side.heads;}
	else if(value > 0.5)
	{_side = Side.tails;}
	else
	{_side = Side.edge;}
}
public boolean equals(Coin other)
{
	return _side == other._side;
}
public String toString()
{
	return _side.toString();
}
	private Side _side;

public static void main(String[] args)
{
	Coin heads = new Coin(Side.heads);
	Coin tails = new Coin(Side.tails);
	
	Random rand = new Random();
	
	Coin[] coins = new Coin[]
	{
		new Coin(rand),
		new Coin(rand),
		new Coin(rand),
		new Coin(rand),
		new Coin(rand),
		new Coin(rand),
		new Coin(rand),
		new Coin(rand),
		new Coin(rand),
		new Coin(rand)
	};

	System.out.println(Arrays.toString(coins));

	int numHeads = Collections.frequency(Arrays.asList(coins), heads);
	System.out.println("There are " + numHeads + " heads.");

	int numTails = Collections.frequency(Arrays.asList(coins), tails);
	System.out.println("There are " + numTails + " tails.");
}
}

I encountered some challenges while finding the "right way"
- no easy way to "apply"
- countif/equality not working here
- *()&(SFHKJHSF(&(*&:>( code tags!!


#4913896 How a derived class cannot resolved members of its base class at compile time?

Posted by m3mb3rsh1p on 17 February 2012 - 07:28 AM

With all due respect, Washu, turning the question into an analysis of assembly language doesn't help make it easier to understand. If the concepts are straightforward to you, please simplify them for us.


PARTNERS