Fundamentals of Computer Science II (CSC-152 98S)

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]


Lab 11: A Balloon Race

Held: Friday, March 13, 1998

Overview: In this lab, 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: The primary goal of this assignment is to have fun. At the same time, you will be enhancing your understanding of Java's object model while developing reactive algorithms.

Exercise

Getting Started

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.

Observation

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.

Your Own Strategy

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.

Read the sample files or the documentation 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.

Your Own Simulation

It is likely that you won't get to this part of the lab.

Read the documentation for Wind or Balloon and create your own subclass that overrides some or all of the methods.

Program Code

MyRace.java



import rebelsky.fun.balloons.Strategist;
import rebelsky.fun.balloons.Balloon;
import rebelsky.fun.balloons.BalloonRace;
import rebelsky.fun.balloons.Wind;
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(new Wind(BalloonRace.HEIGHT));
    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



Bagger.java



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



CoinFlipper.java



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



Inflater.java



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



Popper.java



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 above ground
 * level, inflate the balloon with a 25% probability. 
 */
public class Popper
  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 ((b.getHeight() == 0) || (Math.random() < 0.25)) {
      b.inflate();
    }
  } // decide(Balloon, int, Wind)
} // Popper




Windy.java



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




[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]

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 Fri Mar 13 07:56:28 1998.

This page generated on Fri Mar 13 08:04:44 1998 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu