Programkod från föreläsning 12

Jag hinner tyvärr inte kommentera det jag gjorde, men här är den programkod vi skrev:

import se.lth.cs.pt.die.*;

// från förra gången:
class DieInvestigation {

    public static void main(String[] args) {
        new DieInvestigation().run();
    }

    void run() {
        Die die = new Die();
        int[] count = new int[7];   // ger oss index 0, 1..6
        for (int k = 1; k <= 6000; k++) {
            die.roll();
            count[die.getNbrOfSpots()]++;
        }
        for (int k = 1; k <= 6; k++) {
            System.out.printf("%2d: %5d\n", k, count[k]);
        }
    }
}

// nu vill vi testa kast med flera tärningar
class SeriousDieInvestigation {

    public static void main(String[] args) {
        new SeriousDieInvestigation().run();
    }

    void run() {
        HandOfDice hand = new HandOfDice(5);
        Histogram histogram = new Histogram(1 * hand.nbrOfDice(),
                                            6 * hand.nbrOfDice());
        for (int k = 1; k <= 10000; k++) {
            hand.rollDice();
            histogram.add(hand.totalNbrOfSpots());
        }
        for (int k = histogram.minValue(); k <= histogram.maxValue(); k++) {
            System.out.printf("%2d: %5d\n", k, histogram.getCount(k));
        }
    }
}

class HandOfDice {

    private Die[] dice;

    public HandOfDice  (int nbrOfDice) {
        dice = new Die[nbrOfDice];
        for (int k = 0; k < nbrOfDice; k++) {
            dice[k] = new Die();
        }
    }

    public int nbrOfDice() {
        return dice.length;
    }

    public void rollDice() {
        for (Die die : dice) {     // fungerar eftersom vektorn är 'full'
            die.roll();
        }
    }

    public int totalNbrOfSpots() {
        int sum = 0;
        for (int k = 0; k < dice.length; k++) {   // vi skulle kunna ha samma
                                                  // loop som ovan...
            sum += dice[k].getNbrOfSpots();
        }
        return sum;
    }
}

class Histogram {

    private int[] count;
    private int min;       // det minsta värde vi registrerar

    public Histogram  (int min, int max) {
        count = new int[max - min + 1];
        this.min = min;
    }

    public void add(int value) {
        count[value - min]++;
        // count.set(value - min, count.get(value - min) + 1);
    }

    public int minValue() {
        return min;
    }

    public int maxValue() {
        return min + count.length -1;
    }

    public int getCount(int value) {
        return count[value - min];
    }
}