0

I am trying to make a simple coin toss simulation in Java, and after playing around with initializations and such, I can't get the String side initialization to be overridden by the toss simulation.

    import java.util.*;

 public class Coin {
public static void main(String [] args)
{
    int randomNum = -1;
    String side = "";
    toss();
    sideUp(randomNum);
    getSideUp(side);
}

public static int toss()
{
    Random rn = new Random();
    int range = 2;
    int randomNum =  rn.nextInt(range);
    return randomNum;
}

public static String sideUp(int randomNum)
{
    String side;
    if (randomNum == 0)
        side = "heads";
    else
        side = "tails";
    return side;
}

public static void getSideUp(String side)
{
    System.out.println("The result is " + side + ".");
}

}
1
  • 2
    It appears you don't understand variable scoping. I highly suggest starting with a good beginner's book on Java or the Oracle tutorials. Commented Jun 16, 2013 at 20:00

5 Answers 5

2

You are throwing away the result of your "toss" method

See what happens if you do

int randomNum = toss();
Sign up to request clarification or add additional context in comments.

1 Comment

There's more being thrown away.
2

You return side from sideUp, but then you don't use that return value. You should assign its return value to your side variable in main. You also do the same for toss with randomNum. Remember that when you pass the variable into a function's parameter, you don't get to see the changes that function made to your variable. (Well, that's a simplification, but mutability is a different concern.)

Comments

0

You can do this -

getSideUp(sideUp(toss()));

Comments

0

change your main like this

 public static void main(String[] args) {

    int randomNum=toss();
   String side= sideUp(randomNum);
    getSideUp(side);
}

Comments

0

Here's one: Make Random a class member and don't use static methods.

package random;

/**
 * Coin
 * @author Michael
 * @link http://stackoverflow.com/questions/17137118/initializing-and-passing-strings-in-java
 * @since 6/16/13 4:00 PM
 */

import java.util.Random;

public class Coin {

    public static final int MAX_VALUE = 2;
    public static final int HEADS_VALUE = 0;
    private final Random random;
    private int numHeads = 0;
    private int numTosses =0;

    public static void main(String[] args) {
        int maxTosses = (args.length > 0 ? Integer.valueOf(args[0]) : 10);
        Coin coin = new Coin();
        for (int i = 0; i < maxTosses; ++i) {
            System.out.println(coin.toss());
        }
        int numHeads = coin.getNumHeads();
        int numTosses = coin.getNumTosses();
        System.out.println(String.format("# heads : %d", numHeads));
        System.out.println(String.format("# tosses: %d", numTosses));
        System.out.println(String.format("%% heads : %10.3f", ((double)numHeads/numTosses)*100.0));
    }

    public Coin() {
        this.random = new Random();
    }

    public Coin(long seed) {
        this.random = new Random(seed);
    }

    public synchronized String toss() {
        int value = this.random.nextInt(MAX_VALUE);
        if (value == HEADS_VALUE) {
            ++numHeads;
        }
        ++numTosses;
        return (value == HEADS_VALUE ? "heads" : "tails");
    }

    public synchronized int getNumHeads() {
        return numHeads;
    }

    public synchronized int getNumTosses() {
        return numTosses;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.