0

So I need to have 3 instances of coin run in A1, and simulate a coin flip independently, but every time they run they all return the same value. I'm not sure what the problem is so I'll just link all of my code.

public class A1main {
public static Coin nickel = new Coin();
public static Coin dime = new Coin();
public static Coin quarter = new Coin();
public static double balance = 0.00;

public static void main(String[]args){
tossall();
checkAndAdd();
System.out.print(balance);
}
public static void tossall(){
    nickel.toss();
    dime.toss();
    quarter.toss();
    System.out.print(nickel.getSideUp()+ dime.getSideUp() + quarter.getSideUp());
}
public static void checkAndAdd(){
    if(nickel.getSideUp().equals("heads")){
        balance += .05;
}
    if(dime.getSideUp().equals("heads")){
        balance += .10;
}
    if(quarter.getSideUp().equals("heads")){
        balance += .25;
}
}
}

package A1;
import java.util.*;

public class Coin {
    public static Random rand = new Random();
    public static String sideUp; //Can this be static?
   public Coin(){
boolean side = rand.nextBoolean(); //used to store/determine side up Heads == true Tails == false.
if(side){
    sideUp = "heads";
}else{
    sideUp = "tails";
}
}
public static void toss(){
    boolean side = rand.nextBoolean(); //used to store/determine side up Heads == true Tails == false.
    if(side){
        sideUp = "heads";
    }else{
        sideUp = "tails";
    }
}
public static String getSideUp(){
    return sideUp;
}
}

0 Answers0