Chapter 5

Exercise 5.14
import java.util.Random;


public class RandomTester
{
    private Random random;  // the random number generator

    public RandomTester()
    {
        random = new Random();
    }

    public void printOneRandom()
    {
        System.out.println(random.nextInt());
    }

    public void printMultiRandom(int howMany)
    {
        for(int i=0; i<howMany; i++) {
            printOneRandom();
        }
    }
}
Exercise 5.16
public int throwDice()
{
    return random.nextInt(6) + 1;
}
Exercise 5.17
public String getResponse()
{
    int answer = random.nextInt(3);
    if(answer == 0) {
        return "yes";
    }
    else if(answer == 1) {
        return "no";
    }
    else {
        return "maybe";
    }

}
Exercise 5.18
private ArrayList responses;

public RandomTester()   // constructor
{
    responses = new ArrayList();
    responses.add("yes");
    responses.add("don't know");
    ...
}

public String getResponse()
{
    return responses.get(random.nextInt(responses.size()));
}