package ballworld; import cs101.lang.Animate; /** * The Exploder class represents a Ball which maintains its own position * and velocity, with a growing radius. When the Ball reaches its * maximum radius, it "explodes", producing two new Balls. This class * also maintains the generation of each Ball. After a set number of * generations, this Exploder will simply remove itself from the * world in which it resides. */ public class Exploder implements Ball, Animate { // Constant: the maximum radius of a Ball before it explodes private static final double maxRadius = 5; // Constant: the maximum number of generations a Ball can attain // before it removes itself from its World private static final int maxLevel = 5; private double x; // The current X-coordinate of this Ball private double y; // The current Y-coordinate of this Ball private double radius; // The current radius of this Ball private double xVel; // The current X-velocity of this Ball private double yVel; // The current Y-velocity of this Ball private World myWorld; // The world in which this Ball resides private int level; // The current generation of this Ball /** * Class constructor. * Initialize this instance of the Exploder class. */ public Exploder() { // Initialize the x and y velocities of this Bouncer object this.xVel = Math.random()-0.5; this.yVel = Math.random()-0.5; // Initialize the x and y position of this Bouncer object this.x = 0; this.y = 0; // Initialize the radius of this Bouncer object this.radius = 2; // Explicitly set myWorld to null this.myWorld = null; // Level initially == 0. this.level = 0; } /** * Class constructor. * Initialize this instance of the Exploder class, * using the specified parameters. */ public Exploder(double startx, double starty, int level) { this.x = startx; this.y = starty; this.xVel = Math.random()-0.5; this.yVel = Math.random()-0.5; this.radius = 2; this.level= level; } /** * Returns current X-coordinate of this Ball. */ public double getX() { return this.x; } /** * Returns current Y-coordinate of this Ball. */ public double getY() { return this.y; } /** * Returns current radius of this Ball. */ public double getRadius() { return this.radius; } /** * Sets the world in which this ball resides to be "theWorld". */ public void setWorld(World theWorld) { this.myWorld = theWorld; } /** * Moves this Ball to the location at which the user clicked. */ public void userClicked(double atX, double atY) { this.x = atX; this.y = atY; } /** * If the user typed a 'd', removes this Ball from myWorld. Otherwise * ignores the keypress. */ public void userTyped(char key) { if (key == 'd') { this.myWorld.removeBall(this); } else { // Ignore keypress } } /** * This Ball's implementation of the Animate interface. Causes this Ball * to move in a direction until its radius reaches a certain size. * At this point, if the Ball's generation is less than the maximum * number of generations, it explodes, producing two new Balls. * Each new Ball will have a generation equal to this Ball's generation, * plus 1. Finally, this Ball is removed from the world in which it * resides. * * NOTE: Many students included their bouncer code in the Exploder * class. This was cool, but not necessary for full credit on this * problem. */ public void act() { this.radius += 0.02; this.x += this.xVel; this.y += this.yVel; if (this.r > maxRadius) { if (this.level <= maxLevel) { this.myWorld.addBall(new Exploder(this.x, this.y, this.level+1)); this.myWorld.addBall(new Exploder(this.x, this.y, this.level+1)); } this.myWorld.removeBall(this); } } }