![]() |
Lab 11
|
To get started: Download the lab11_programs
folder
from the cs111d
account.
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]
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("]");
}
insertion sort
. Today we will write a method to implement
selection sort
.
Here is the idea of selection sort:
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
.
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.
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:
randomTurn()
and, if there is no wall in front of the buggle,
move one step forward. If there is a wall, the buggle stays in the same place.
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.)
FreezeTag
in the file FreezeTag.java
.
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.
public FreezeBuggle [] createPlayers(int num)
This method creates, populates and returns an array of FreezeBuggle
objects. The
parameter num
specifies the number
of FreezeBuggle
s 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 FreezeBuggle
s methods:
randomPosition(int width,int height)
width
and height
are the width and the height of the grid. Instance variables width
and
height
are inherited from BuggleWorld
, you can use them directly.
In other words, you can call the method like this:
fanny.randomPosition(width,height); // fanny
is an instance of FreezeBuggle
randomColor()
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.
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.
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?
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()).