user2450335
user2450335

Reputation: 29

initializing and passing strings in java

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 + ".");
}

}

Upvotes: 0

Views: 103

Answers (5)

duffymo
duffymo

Reputation: 308743

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

Upvotes: 0

Nikos
Nikos

Reputation: 1

change your main like this

 public static void main(String[] args) {

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

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

You can do this -

getSideUp(sideUp(toss()));

Upvotes: 0

Score_Under
Score_Under

Reputation: 1216

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.)

Upvotes: 2

DaveH
DaveH

Reputation: 7335

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

See what happens if you do

int randomNum = toss();

Upvotes: 2

Related Questions