Wednesday, October 31, 2001
lab7
from the download
directory.
It contains code for all 4 exercises that you will be working on. Within the
lab7
folder there is a test
folder which demonstrates
how your solution should work.
Recall the Buggle's Olympic Symbol from problem set 1. In this problem we use a list of 5 buggles to draw a version of this symbol that will draw the 5 rings simultaneously, each buggle draws one ring. The olympic symbol looks as shown below, note that it is slightly different from the one in the problem set 1:
The object list fiveBuggles
contains 5 buggles whose initial positions are
in the left lower corners of the rings facing east. The method run
has
the beginning of drawing
the rings. The part below draws the bottom part of the rings and makes the buggles turn left.
// buggles move together to create the olimpic symbol
// draw the bottom side of the rings
fiveBuggles = AllForward(6, fiveBuggles);
fiveBuggles = AllLeft(fiveBuggles);
Your task is to fill in the code for the methods
public static ObjectList AllForward(int n, ObjectList buggles) {
// move each of the buggles on the list n steps forward
// Don't forget the typecasting, otherwise you cannot invoke
// buggle methods on elements of the list!
// return the modified list
and
public static ObjectList AllLeft(ObjectList buggles) {
// turn left each of the buggles on the list
// Don't forget the typecasting, otherwise you cannot invoke
// buggle methods on elements of the list!
// return the modified list
The first methods makes all the buggles on the list move n
steps forward,
the second one makes all buggles turn left. Note that both methods return the modified list,
i.e. the list of buggles with the changed position or the changed direction.
You also need to fill in the rest of the code in run
to finish drawing the rings.
CoopHarvester
and have
a method harvestSquare
which makes a buggle harvest a 2-by-2 square to its
left and above. The method returns the number of bagels harvested on the square. This class
has been already written, you do not need to change anything in it.
The program starts by generating a random field of bagels according to the height, width, and
number of bagels entered in the small window near the applet (just as in HarvestWorld
).
In addition, the program creates an ObjectList of CoopHarvesters, one per each 2-by-2 square of the
field ( (height*width)/2 total). The program works only when both height and width are even numbers.
The pictures below show the initial and the final configuration of the program.
In the end the number of bagels harvested is displayed in the small window:
Your task is to fill in the code for the following method:
public int harvestAll(ObjectList harvesters) {
// The method uses a list of CoopHarvesters positioned each in the starting
// position of a 2-by-2 square to harvest bagels on the entire field
// by invoking the method harvestSquare for each harvester on the list
// The method returns the total number of harvested bagels obtained
// as the sum of values returned by harvestSquare invocations
To run the program, enter the size of the grid and the number of bagels and buggles in the small window. The program then generates the grid with a random configuration of bagels and creates a list of buggles with random positions and colors (except for blue). An example of the intial configuration is shown on the left picture below.
The game proceeds as follows:
randomTurn()
and, if there is no wall right in front of the buggle,
move one step forward.
After the specified number of rounds the number of buggles still in the game is displayed in the little window:
Your task is to fill in code for the following methods:
public int playTag(ObjectList players, int roundsLeft) {
// the method simulates a specified number of rounds of FreezeTag
// and returns the number of buggles still in the game
// it uses method oneRound to play a round of the game,
// and a method howManyNotBlue to count the number of buggles
// still in the game
public ObjectList oneRound(ObjectList players) {
// the method simulates one round of FreezeTag game
// For each buggle on the list does the following:
// 1. If the buggle is over a bagel, its color is set to blue
// 2. otherwise the buggle performs a random turn, and, if it is not
// facing a wall, a forward move
// The modified list of buggles is returned
public int howManyNotBlue(ObjectList players) {
// the method returns the number of buggles on the list which have color other than blue
ObjectList
s which hold strings. The program in the file
RandomSentenceTest.java
has three ObjectLists: nouns
which contains
three nouns, adjectives
which contains three adjectives, and actions
, which contains
three actions, s.a. "is walking". The program generates random sentences by choosing an adjective,
a noun, and an action at random. An example of the program's output looks like this:
The program illustrates how to use the static method
pickRandom
of the class ObjectListOps
to choose one element of the list at
random:
String word = (String) ObjectListOps.pickRandom(nouns);
println("A random noun is: " + word);
The first line in the output is a random string chosen from the list of nouns.
Your task is to fill in the code for the method
public ObjectList generateSentence(ObjectList L1, ObjectList L2, ObjectList L3) {
// The method returns a list made of one random word of L1
// followed by a random word of L2, followed by a random word of L3
The method is called 10 times in the program, so 10 random sentences are generated.