public class Ball {
    private double px, py, vx, vy, gravity, radius;
    private int r, g, b;

    public Ball() {
        px = Math.random();
        py = Math.random();
        vx = -0.005 + (Math.random() * 0.01); // [-0.005, 0.005]
        vy = -0.005 + (Math.random() * 0.01);
        gravity = -0.0001;
        radius = 0.02 + Math.random() * 0.04; // [0.02, 0.06]
        r = (int) (Math.random() * 256);
        g = (int) (Math.random() * 256);
        b = (int) (Math.random() * 256);
    }

    public void draw() {
        PennDraw.setPenColor(r, g, b);
        PennDraw.filledCircle(px, py, radius);
    }

    public void update() {
        px = px + vx;
        py = py + vy;
        vy = vy + gravity;

        if (vy < 0 && py - radius <= 0) {
            vy = -0.9 * vy;
        }

        if ((vx < 0 && px - radius <= 0) ||
                (vx > 0 && px + radius >= 1)) {
            vx = -0.9 * vx;
        }
    }
}