CSCI 51, Spring 2009

Program 5: Working with arrays

100 points

Assigned: Thursday, March 5, 2009
Due: Friday, March 13 at 11:55 pm

This assignment has you turn in three java files, Arr.java, Ttt.java, and TttBoard.java.

Program 1: Working with Arrays (50pts)
Create a new class called ArrArr will contain several static methods dealing with arrays:
  1. int[] randomIntArray(int size, int high) that returns an array of integers of size size where each element
    of the array is an int from 0 to high-1.
  2. double[] randomArray(int size, double a, double b) that returns an array of doubles of size size where
    each element of the array is a double between a and b.
  3. void printArray(int[] a) that prints the elements of the array, 10 elements per line.
  4. void printArray(double[] a) that prints the elements of the array, 10 elements per line.
  5. int find(int[] a, int key) that returns the index of the first occurance of key in the array a, or -1 if no
    occurance is found.
  6. int countMultiplesOf(int[] a, int common) that returns the number of elements in the array that are
    multiples of the second parameter. You might want to define an auxiliary function that tests to see if
    a number is a multiple of another and returns a boolean value.
  7. int[] buildHistogram(int[] a) that returns an integer array that is the histogram of the input array a. You
    will build the histogram as follows. If the array a contains 5 occurrences of 15, then 5 will be stored as
    the value in the array index 15 of the array that will be returned in this method.
  8. void printHistogram(int[] hist) that prints the input histogram in the following format:
    i: <n> <n of *’s for the index i of the array. n is the element in that index i>
    Example output may be:
    0: 4: ****
    1: 2: **
    2: 5: *****
    3: 7: *******
    4: 1: *
    5: 0:
    6: 12: ************
    7: 5: *****
    Try to solve this problem incrementally.  For example, you may want to test your method on an array
    with non-random entries, so that you know what the result should be.
You should put in a main method to test your code.  I will be using my own ArrTester to test whether your
implementations do what is required of them.

Program 2: Tic-Tac-Toe (50 pts)
In this problem, you will implement a Tic Tac Toe game. You are given an implementation of Tic Tac Toe
called Ttt.java which you will modify. To start create a class called TttBoard (in a file called TttBoard.java).
In Ttt you will see nine static variables, like 'private static char s9' for the elements of the board.  Replace
these variables with one 'TttBoard board;'. You should use a 2D array of chars as the underlying structre
within TttBoard: 'private static char[][] theBoard;' 

Obviously, the rest of the program should be updated in such a way that the program now works with this new
data structure, namely the TttBoard object.  4 parts then:
  1. Methods you need to define in TttBoard should include setting a particular square to x or o (and perhaps
    returning some value to indicate if the square was already taken) and getting the current state of
    particular squares.
  2. Update the remainder of Ttt.java and make it work without any errors. Do not change the signature of the
    methods given in Ttt.java unless you absolutely have to, in which case you must justify the change.
  3. The given version of Tic Tac Toe continues until the entire board is filled up. Improve your game so that
    it can detect a tie game if it is a tie game even if the board is not completely filled up.  During playing, the
    game should stop with output like "The current board leads to a tie game."
  4. The given version of Tic Tac Toe is played by two humans. Revise it so that it is played by a human and a
    computer. That is, write it so that the user is responsible only for x's or o's, not both. Think about how to
    simulate “tossing a coin.”