CS100M Spring 2001        Prelim T1 *Java* Solutions                 April 10

Q1. omitted

Q2. Write a function to capitalize the first letter in each word of a string
    that contains only letters and spaces.

matlab solution:

    function str = capitalize(str)
    % str = capitalize(str): capitalize first letter of each word.
    % assume $str$ contains only letters and spaces

        str(1) = upper(str(1));
        for i = 2:length(str)
            if str(i-1) == ' '
                str(i) = upper(str(i));
            end
        end

java solution:

    // capitalize first letter of each word.
    // assume $str$ contains only letters and spaces
    public static String capitalize(String str) {
        char[] S = str.toUpperCase().toCharArray();
        char[] s = str.toCharArray();
        s[0] = S[0];
        int i = 1;
        while (i < s.length) {
            if (s[i-1] == ' ') {
                s[i] = S[i];
            }
            i += 1; // [4/11] added this missing line
        }
        return new String(s);
    }

Q3. part of project P6

Q4.

(a) Write a function to roll $D$ fair dice $N$ times and return
    the frequencies of outcomes.

matlab:

    function counts = roll(D, N)
    % counts=roll(D,N): outcome frequencies of rolling $D$ fair dice $N$ times
        sides = 6;                          
        counts = zeros(1, D*sides);         
        for i = 1:N                         
            j = 0;                          
            for k = 1:D                     
                j = j + ceil(rand*sides);   
            end                             
            counts(j) = 1+counts(j);        
        end        

java -- note, includes frequency = 0:

    // outcome frequencies of rolling $D$ fair dice $N$ times
    public static int[] roll(int D, int N) {
        int sides = 6;
        int[] counts = new int[D * sides+1]; // +1 since start at 0
        int i = 0; // number of trials done so far
        while (i < N) {
            int outcome = 0; // outcome so far
            int j = 0; // number of dice rolled so far
            while (j < D) {
                outcome += (int) (Math.ceil(Math.random()*sides));
                j += 1;
            }
            counts[outcome] += 1;
            i += 1;
        }
        return counts;
    }                     

(b) Write a function to take an array of frequencies and compute the mean.

matlab:

    function mean = meanfreq(counts)
    % mean = meanfreq(counts): arithmetic mean, given frequencies $counts$
    % (i.e. there are $counts(i)$ occurrences of $i$)
        n = 0; % number of data points so far
        sum = 0; % sum of data points so far
        i = 0; // already processed $counts(1:i)$
        while i < length(counts)
            i = i+1;
            n = n + counts(i);
            sum = sum + i * counts(i);
        end
        mean = sum / n;

java -- note, starts at frequency = 0 not at frequency = 1:

    // arithmetic mean, given frequencies $counts$ 
    // (i.e. there are $counts(i)$ occurrences of $i$)
    public static double meanfreq(int[] counts) {
        int n = 0; // number of data points so far
        double sum = 0; // sum of data points so far
        int i = 0; // already processed $counts[1..i]$
        while (i < counts.length) {
            n += counts[i];
            sum += i * counts[i];
            i += 1;
        }
        returm sum / n;
    }