In: Computer Science
Code
Now here you can not give class name 5 only number so i just named Class5
import java.util.Random;
class CoinFlipper
{
private int headsCount;
private int tailsCount;
Random randomNumbers = new Random();
public CoinFlipper() {
headsCount=0;
tailsCount=0;
}
public int flipCoin()
{
return randomNumbers.nextInt(2);
}
public void countFlips()
{
for(int i=0;i<100;i++)
{
if(flipCoin()==1)
headsCount++;
else
tailsCount++;
}
}
@Override
public String toString() {
return "Heads Count: " + headsCount + ", Tails Count:" + tailsCount
;
}
}
public class Class5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CoinFlipper coinObj=new CoinFlipper();
coinObj.countFlips();
System.out.println(coinObj);
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.