Assigned: Thursday, March 12, 1998
Due: Thursday, March 12, 1998
Overview: In this assignment, you will be building strategies for racing balloons and then racing balloons. You will start by using predetermined strategies for racing the balloons and observing the effects of those strategies. You will then start creating your own strategies.
Purpose: In this assignment, you will attempt to enhance your understanding of Java's object model while developing reactive algorithms.
Recall that there are three basic steps to building a Java program (assuming that SamR has helped you make the change that allows this shorthand).
jc Program.java
ji Program
Of course, when creating a program from scratch, you should begin by sketching the design of the program: what it does and how it does it.
This exercise relies on a number of preexisting programs and libraries. Begin by making copies of the following programs:
Compile each of the programs. Then execute MyRace.
You will note that soon (i.e., approximately one minute) after you start
MyRace, a window will appear on the screen. You will see
a few balloons at the lower left hand side of the window and a finish
line near the right-hand-side. You can run one step (i.e. about one
minute of simulated time) by typing s. You can quit the
race by typing q.
Look at each of the code files you've received to see what's going on.
Note that most of the simulation is supplied by utilities relating
to rebelsky.fun.balloons, and the primary purpose of most
of these files is to determine a strategy by which to run races.
In addition, MyRace.java sets up the race. You may want
to create other races between different color balloons with multiple
balloons using the same strategy. How do you get different colors?
You can use any of the built in colors, which are
Color.black,
Color.blue,
Color.cyan,
Color.darkGray,
Color.gray,
Color.green,
Color.lightGray,
Color.magenta,
Color.orange,
Color.pink,
Color.red,
Color.white, and
Color.yellow.
You can also create new colors with
new Color(red, green, blue)
where red, green, and
blue are numbers between 0 and 255 that reflect
the "amount" of each color in the compound color.
At this point, you can determine and implement your own strategy. How?
By building another object like CoinFlipper.java and the
rest that follows some procedure you deem appropriate. Note that you
are able to gather a variety of information as you make your decisions.
w.windSpeed(height).
b.getSandbags().
b.getInflation().
Math.random().
Read the sample files for other ideas.
Once you've implemented and successfully compiled your own strategist, add it to your race and run the race. You may find that you'll need to refine as you go.
import rebelsky.fun.balloons.Strategist;
import rebelsky.fun.balloons.Balloon;
import rebelsky.fun.balloons.BalloonRace;
import java.awt.Color;
/**
* Race some balloons.
*
* @author Samuel A. Rebelsky
* @author YOUR NAME HERE
* @version 1.0 of March 1998
*/
public class MyRace
{
/**
* Start and run the race.
*/
public static void main(String[] args)
{
BalloonRace race = new BalloonRace();
race.add("Yellow Popper", new Balloon(Color.yellow), new Popper());
race.add("Black Inflater", new Balloon(Color.black), new Inflater());
race.add("Green CoinFlipper", new Balloon(Color.green), new CoinFlipper());
race.add("Red Bagger", new Balloon(Color.red), new Bagger());
race.add("Grey Windy", new Balloon(Color.gray), new Windy());
race.start();
} // main(String[])
} // MyRace
import rebelsky.fun.balloons.Balloon;
import rebelsky.fun.balloons.Strategist;
import rebelsky.fun.balloons.Wind;
/**
* A simple strategy for winning balloon races. If you're still on the
* ground, drop a sandbag with a 20% probability.
*/
public class Bagger
extends Strategist
{
/**
* Decide what to do based on knowledge of the balloon status.
* Ignore the wind.
*/
public void decide(Balloon b, int maxheight, Wind w)
{
// If we're still on the ground, drop a sandbag
if (b.getHeight() == 0) {
if (b.getSandbags() > 0) {
b.drop();
}
else { // there are no sandbags left
b.inflate();
} // no sandbags left
} // if we're on the ground
// If we're more than half way up, deflate the balloon
if (b.getHeight() > (maxheight / 2)) {
b.deflate();
}
} // decide()
} // Bagger
import rebelsky.fun.balloons.Balloon;
import rebelsky.fun.balloons.Strategist;
import rebelsky.fun.balloons.Wind;
/**
* A completely random strategy for winning balloon races.
* Inflate the balloon with a 10% probability. Deflate the balloon
* with a 10% probability. Drop a sandbag with a 3% probability.
*/
public class CoinFlipper
extends Strategist
{
/**
* Decide what to do based on knowledge of the balloon status.
* Ignore the wind.
*/
public void decide(Balloon b, int maxheight, Wind w)
{
if (Math.random() < 0.10) {
b.inflate();
}
if (Math.random() < 0.10) {
b.deflate();
}
if (Math.random() < 0.02) {
b.drop();
}
} // decide()
} // CoinFlipper
import rebelsky.fun.balloons.Balloon;
import rebelsky.fun.balloons.Strategist;
import rebelsky.fun.balloons.Wind;
/**
* A simple strategy for winning balloon races. If you're below
* 1/4 of the maximum height, inflate the balloon with a 10%
* probability. If you're above 1/2 of the minimum height, deflate
* the balloon.
*/
public class Inflater
extends Strategist
{
/**
* Decide what to do based on knowledge of the balloon status.
* Ignore the wind.
*/
public void decide(Balloon b, int maxheight, Wind w)
{
// If we're on the ground, inflate
if (b.getHeight() == 0) {
b.inflate();
}
else if (b.getHeight() < (maxheight / 4)) {
if (Math.random() < 0.10) {
b.inflate();
}
}
else if (b.getHeight() > (maxheight / 2)) {
b.deflate();
}
} // decide()
} // Inflater
import rebelsky.fun.balloons.Balloon;
import rebelsky.fun.balloons.Strategist;
import rebelsky.fun.balloons.Wind;
/**
* A more complex strategy for winning balloon races. If you're on the
* ground, drop a sandbag. If you're over * 2/3 of the way up, deflate
* the balloon. If you're anywhere else check the wind speed ten feet
* above and ten feet below. If it's faster above, increase the inflation.
* If it's faster below, decrease the inflation.
*/
public class Windy
extends Strategist
{
/**
* Decide what to do based on knowledge of the balloon status.
* Ignore the wind.
*/
public void decide(Balloon b, int maxheight, Wind w)
{
// The height of the balloon
int height = b.getHeight();
// If the balloon popped, do nothing.
if (b.popped()) return;
// If we're still on the ground, drop a sandbag
if (height == 0) {
if (b.getSandbags() > 0) {
b.drop();
}
else {
b.inflate();
}
}
// If we're more than two thirds of the way up, deflate the balloon
else if (height > ((2 * maxheight) / 3)) {
b.deflate();
}
// Otherwise, compare higher speeds and lower speeds
else {
if (w.getSpeed(height + 10) > w.getSpeed(height - 10)) {
b.inflate();
}
else {
b.deflate();
}
}
} // decide()
} // Windy
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Thu Mar 12 15:52:00 1998.
This page generated on Thu Apr 2 13:50:08 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu