Reputation: 81
I have a project that utilizes two classes, the first one is a coin and it randomizes on what face it lands on after being toss, the toss method randomizes the result and the getSideUp reveals the face and is returned to the beginning. The second class, the cointoss has a goal to display the results for 30 times, keep number of each face shown and calculate and show those numbers. What I need help in is finding a way to repeat the flipping method for 30 times, each time random and different from the one before. I can calculate the number of flips, but I need to also find out how to calculate the number of "Heads" and "Tails". here are the source codes, first one being the coin class which the coin toss is based off of, and the second is the cointoss which must be repeated 30 times. How I have it so far, the result is randomized but displays the same face 30 times.
public class FahrDylanCoin{
private String sideUp;
public FahrDylanCoin()
{
sideUp = "Heads";
}
public void toss(){
Random rand = new Random();
int cToss = rand.nextInt(2);
if (cToss == 0)
sideUp = "Heads";
else
sideUp = "Tails";
}
public String getSideUp()
{
return sideUp;
}
}
the second class
public class FahrDylanCoinToss{
public static void main (String [] args){
FahrDylanCoin flip = new FahrDylanCoin();
flip.toss();
for (int i =1; i <=30; i++)
System.out.println( i + "\t\t" + flip.getSideUp());
}
}
Upvotes: 0
Views: 64
Reputation: 4314
I guess you want to do the coin toss inside the for loop like this:
for (int i = 1; i <=30; i++)
{
flip.toss();
System.out.println( i + "\t\t" + flip.getSideUp());
}
This way, the result of the coin toss is different every time. And, if you want to count the number of heads and tails, keep a value that is incremented every time it is heads, and the number of tails is then 30 - numberOfHeads.
Upvotes: 1