/* * Balloon Race Program * * Class: Balloon * * Author: Alyce Brady * * Modified: * Your Name Date Reason * * License: * This program is free software; you can redistribute it * and/or modify it under the terms of the GNU General Public * License as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ import java.awt.Color; import java.util.Random; /** Balloon Race Program: * The Balloon class defines a balloon in a balloon race. * * @author Alyce Brady (modified by Your Name) * @version 2 March 2003 **/ public class Balloon { // Instance Variables: Encapsulated data for EACH balloon private Color color; // this balloon's color private int targetAltitude; // height at which this balloon floats private int altitude; // this balloon's altitude private int distanceTraveled; // how far this balloon has traveled private int minMoveAmount; // min. distance can move at one time private int maxMoveAmount; // max. distance can move at one time private int maxAltitude; // max. altitude for this balloon /** Constructs a Balloon instance and sets its properties, including the * balloon's color, the altitude to which this balloon will rise before * moving forward, and the minimum and maximum distances this balloon * will move up or forward in any given time step. * @param balloonColor color for this balloon * @param targetAltitude altitude at which this balloon needs to reach * @param minMoveAmount minimum distance to move in one time step * @param maxMoveAmount maximum distance to move in one time step * @param maxAltitude max. altitude at which this balloon can fly **/ public Balloon(Color balloonColor, int targetAltitude, int minMoveAmount, int maxMoveAmount, int maxAltitude) { // Initialize balloon's color, target altitude, and minimum and // maximum movement distances. Place balloon at the starting line. this.color = balloonColor; this.targetAltitude = targetAltitude; this.minMoveAmount = minMoveAmount; this.maxMoveAmount = maxMoveAmount; this.maxAltitude = maxAltitude; this.altitude = 0; this.distanceTraveled = 0; } /** Gets the balloon's altitude. **/ public int altitude() { return this.altitude; } /** Gets the distance this balloon has traveled from the starting line. **/ public int distanceFromStart() { return this.distanceTraveled; } /** Gets balloon's color. **/ public Color color() { return this.color; } /** Moves this balloon. **/ public void move() { // If the balloon is below its target altitude, rise. Otherwise, move // forward. } /** Returns distance to move this balloon. **/ protected int getMoveAmount() { // First get random number in range [0, maxMoveAmount-minMoveAmount], // then shift to [minMoveAmount, maxMoveAmount]. Random generator = RandNumGenerator.getInstance(); int moveAmt = generator.nextInt(this.maxMoveAmount - this.minMoveAmount + 1); return moveAmt + this.minMoveAmount; } /** Rises this balloon by the specified distance. * @param distance how far to rise **/ protected void rise(int distance) { this.altitude += distance; if ( this.altitude > this.maxAltitude ) this.altitude = this.maxAltitude; } /** Moves this balloon forward by the specified distance. * @param distance how far to move forward **/ protected void moveForward(int distance) { this.distanceTraveled += distance; } }