vendredi 20 mars 2015

Randomizing output based on a separate class JAVA

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());
}


}





Aucun commentaire:

Enregistrer un commentaire