CS111, Wellesley College

Lab 11

Wedn, Nov 14 2007

Arrays

Today's agenda:

To get started: Download the lab11_programs folder from the cs111d account.

Task 1: Printing out the contents of an array

In the file labArray.java, within the folder lab11_programs, a method printArray(int [] arr), has been defined for you. It prints out the contents of the input array, in the standard output (Console), all in one line, separated by a comma. (printArray(int [] arr) would be useful when testing int array methods.) For example, given the array
B [2, 4, 6, 8] as an input, invoking printArray(B) will produce the following in the Java Console window:

[2, 4, 6, 8]

Here is the definition of this method. Please study it, before we move on to the next task!
     
     
     public static void printArray(int [] a) {
        System.out.print("[");
        for (int i = 0; i < a.length; i++) {
           System.out.print(a[i]);
           if (i < (a.length - 1)) 
           System.out.print(", ");
        }
        System.out.println("]");
     }
     
     
     
     
     

Task 2: Selection Sort on an array of Integers

As you talked in lecture, there are several sorting methods one could use. In lecture, you studied insertion sort. Today we will write a method to implement selection sort. Here is the idea of selection sort:
Scan the whole array to select the smallest value. Exchange it with the value in the first position of the array. Scan the rest of the array (all but the first value) to find the smallest value. Exchange it with the value in the second position of the array. Continue this process for each position in the array. When complete, the array will be sorted.

Write a method insertionSort() that takes as input an array of integers, and sorts that array in increasing order. (The method does not return anything.) Add your code into the file labArray.java in the folder lab11_programs.

Freeze Tag in the BuggleWorld

For this problem write your code in the file FreezeTag.java in the FreezeTag folder. You can run the the program FreezeTag in the Test folder to see how this program should run.

In this problem you will be creating and traversing arrays of FreezeBuggle objects. A FreezeBuggle is an extension of a Buggle which has extra methods for setting random initial positions, random color, and for making random turns. FreezeBuggles love to play Freeze Tag.

How do Buggles play Freeze tag?

In this game a buggle runs around randomly, in a BuggleWorld grid, until she gets over a bagel. Then the buggle becomes "tagged": she changes her color to blue and cannot move any more. We will simulate the game and see how many buggles are still in the game (i.e. are not tagged) after a specified number of rounds.

The details:

To run the program, the user enters -in the small window- the size of the grid, the number of bagels and buggles, and the number of rounds to be palyed. The program then generates the grid with a random configuration of bagels. The specified number of buggles is created. The buggles have random colors (except for blue), and are placed randomly on the grid. An example of the initial configuration is shown on the left picture below.

At every round of the game:

After several rounds of the game, the grid might look like the picture above on the right.

After the game is played for the specified number of rounds, the number of buggles still "alive" in the game (i.e., not tagged and frozen) is displayed in the Console. The positions of all buggles in the end of the game are printed on the Console as well.

Run the project in the Test folder to see Freeze Tag in action! (and make sure you look at the Console so you can see the printed results.)

What you need to do

Your task is to fill in the code for several methods of the class FreezeTag in the file FreezeTag.java.
Notice that you do not need to modify the FreezeBuggle class definition!

In the process of defining the methods for this task, you will get the opportunity to use FOOR-loops, simple and nested, which will be useful for your next assignment (and not only!).

Here are the methods that you need to write.

  1. public FreezeBuggle [] createPlayers(int num)

    This method creates, populates and returns an array of FreezeBuggle objects. The parameter num specifies the number of FreezeBuggles in the array.

    After the array has been created, you need to create the FreezeBuggle objects to populate the array (hint: you need to use the constructor to create a FreezeBuggle), and set their random position and color.

    The position and the color of buggles are set at random using these FreezeBuggles methods:

    The createPlayers() method is already invoked in run(). Initially it is commented out, so the initial file can complile. After you define this method, un-comment the relevant line in the run() method.

  2. public void printPositions(FreezeBuggle [] players)

    Prints out the positions of the FreezeBuggles in the array players. Invoke the method in PlayTag() on the array players to test.

  3. public int howManyNotBlue(FreezeBuggle [] players)

    Returns the number of FreezeBuggles in the array players whose color is not blue. Do you remember how we compare two colors?

  4. And last, but not least, public int playTag(FreezeBuggle [] players, int rounds)

    Here players is the array of FreezeBuggles playing the game, rounds is the number of rounds of the game. The rules of the game are given above. Use the FreezeBuggle method randomTurn() to turn the buggle randomly.

    Use nested loops in the definition of this method. Notice that the iteration goes through the number of rounds and, within each round, through the array of FreezeBuggles.

    In the end, print out the positions of all FreezeBuggles (using the method printPositions() and the number of non-blue FreezeBuggles (using the method howManyNotBlue()).

Now that you have written all the methods, relax and enjoy the game you programmed being played!