System.out.println()
and System.out.print()
.
To get started: Download the lab12_programs
folder from
cs111d
account.
print() and println() are two methods that allow you to print text to the Java console window.
print() prints your text and leaves the cursor at the end of the line, ready to print the next thing.
println(), on the other hand, prints your text and then waits at the start of the next line to print the next thing.
This is best demonstrated by example. Each snippet of code produces the window below it.
System.out.print("Hi, there!");
System.out.print("Good");
System.out.println(" to see you!");
(Note: no spaces betweenthere!
andGood
)
System.out.println("Hi, there!"); System.out.println("Good"); System.out.println(" to see you!");
System.out.println("Hi, there!"); System.out.print("Good"); System.out.println(" to see you!");
labArray.java
in the folder labArray
in lab12_programs
. Use the project in the Test
folder as a sample.
We want to be able to print out the contents of our arrays (so that when we test our array methods, we can print out the arrays to make sure that the methods are working). Let's write a method called printArray(int [] arr) that prints out the contents of the integer array arr on one line. For example, say we have an array B = [2, 4, 6, 8]. Then invoking printArray(B) should produce the following in the Java Console window:
[2, 4, 6, 8]
Use a for loop to traverse the array.Now we want to reverse the contents of our array. So, if array C= [0, 1, 2, 3, 4] then reverse(C) should produce [4, 3, 2, 1, 0]. Now, aren't you glad that we wrote printArray above? We can use that to check that our reverse actually works! Our test code looks like this:
int[] a = { 9, -1, 3, 6, -10}; // creates an array named a
System.out.println("Original a ");
printArray(a); // print out the original array
System.out.println("Reversed a ");
reverseArray(a); // reverse the array
printArray(a); // print out the reversed array
System.out.println("--------");
Note that the method reverseArray()
must change the order
of elements of the given array. It does not do any printing itself (you may, of
course, insert print statements for debugging, and remove or comment out them later).
This method also uses a for loop to traverse the array.
FreezeTag.java
in the FreezeTag
folder.
As before, use the the program in the Test
folder as a sample.
In this problem you will be creating and traversing arrays of FreezeBuggles. A FreezeBuggle is an extension of a Buggle which has extra methods for setting a random position and a random color, and for making a random turn. FreezeBuggles play a Freeze Tag game.
To run the program, the user enters the size of the grid, the number of bagels and buggles, and the number of rounds in the small window. The program then generates the grid with a random configuration of bagels and creates an array of buggles with random positions and colors (except for blue). 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 right in front of the buggle,
move one step forward. If there is a wall, the buggle just stays in the same place.
After the game is played for the given number of rounds, the number of buggles still not tagged is displayed in the little window:
The positions of all buggles in the end of the game are printed on the Java console.
Run the project in the Test folder to see examples.
FreezeTag
(not FreezeBuggle
!) in the file FreezeTag.java
. You also need
to write the test code for some of these methods (see below).
Your methods should use for loops to traverse the array of FreezeBuggles.
Here are the methods that you need to write. Write them in the given order.
public FreezeBuggle [] createPlayers(int howMany)
In this method you need to create a new array of FreezeBuggles. The parameter howMany
specifies the number
of FreezeBuggles in the array.
After the array has been created, you need to go through it using a for loop, create the FreezeBuggles 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:
randomPosition(int width,int height)
Here 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,
invoking the method like this
fanny.randomPosition(width,height);
will work (assuming that fanny
is a FreezeBuggle).
randomColor()
randomly sets the color of a FreezeBuggle to one of the following:
red, yellow, green, cyan, or magenta.
The method is already invoked in run(), you don't need to write the testing code for it.
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. Reminder:
use equals() method of class Color to compare two colors.
If you return the result of this method from PlayTag
, it will appear in the small dialog
window. Initially (before you write the PlayTag
method) the number returned should be
the same as the number of buggles in the game.
public int playTag(FreezeBuggle [] players, int rounds)
Here players
is the array of FreezeBuggles playing the game, round
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.
You need nested loops to write this method, since 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 return the number of non-blue FreezeBuggles (using the method howManyNotBlue()).