/*************************************************************
Create an object-oriented version of BouncingBall.java that
is capable of simulating any number of Ball instances. The first
program should define the following API:
public class Ball
-----------------------------------------------------------------
Ball() create a ball at (0,0), random velocity, random size
void draw() draw ball at current position
void move() move using velocity and unit time increment
The second program (shown separately) will be a client
BouncingBalls that takes a command-line argument N and
creates/animates N bouncing balls.
*************************************************************
* Compilation: javac Ball.java
* Execution: java Ball
* Dependencies: StdDraw.java
*
* Object oriented implementation of a 2-d Ball
* Implementation of a 2-d Ball moving in square with coordinates
* between -1 and 1. Bounces off the walls upon collision.
* Booksite 3.4
*************************************************************/
public class Ball {
// declare instance variables
_____________________________________________ // position
_____________________________________________ // velocity
_____________________________________________ // radius
// other instance variables? up to you
// constructor
public Ball() {
// always start ball position at (0, 0)
// initial velocity and size generated randomly
}
// draw the ball, but not the background
public void draw() {
}
// bounce off vertical wall by reflecting x-velocity
private void bounceOffVerticalWall() {
}
// bounce off horizontal wall by reflecting y-velocity
private void bounceOffHorizontalWall() {
}
// move the ball one step, but don't draw it
public void move() {
// bounce off wall(s) if you are near the border
// update position using unit change in time
}
// test client to create and animate just 2 balls.
// this part is already complete.
public static void main(String[] args) {
// create and initialize two balls
Ball b1 = new Ball();
Ball b2 = new Ball();
// animate them
StdDraw.setXscale(-1.0, +1.0);
StdDraw.setYscale(-1.0, +1.0);
while (true) {
StdDraw.setPenColor(StdDraw.GRAY);
StdDraw.filledSquare(0.0, 0.0, 1.0);
StdDraw.setPenColor(StdDraw.BLACK);
b1.move();
b2.move();
b1.draw();
b2.draw();
StdDraw.show(20);
}
}
}