Posted: Thu Oct 29
Due: Monday 11/2 10:00am
Submission name: w18_bounce
My First Class
You will make a Ball class that should match the following UML diagram:
classDiagram
class Ball {
-int cx
-int cy
-int csize
-int xspeed
-int yspeed
+Ball(int x, int y, int xs, int ys, int cs)
+Ball(int s, int maxX, int maxY)
+void display()
-boolean hitHorizonatalWall(int left, int right)
-boolean hitVerticalWall(int top, int bottom)
+move(int left, int right, int top, int bottom)
}
Important notes:
- Fields or methods with a
-should beprivate, and those with a+should bepublic. - The 3 argument constructor should set
cxandcyvalues such that the entireBallobject will be within the window bonded by[0, maxX)and[0, maxY). - The
let, right, top, bottomparameters in some of the functions represent the maximum area in which the ball can move before bouncing. - If working, the following driver file should test your code:
Ball b0;
Ball b1;
boolean moving;
void setup() {
size(600, 400);
moving = true;
b0 = new Ball(width/2, height/2, 1, 1, 40);
b1 = new Ball(40, width, height);
}//setup
void draw() {
background(0);
b0.display();
if (moving) {
b0.move(0, width, 0, height);
}
}
void keyPressed() {
if (key == ' ') {
moving = !moving;
}
if (key == 'r') {
b0 = new Ball(width/2, height/2, 1, 1, 40);
b1 = new Ball(40, width, height);
}
}//keyPressed