-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.java
More file actions
56 lines (46 loc) · 1.48 KB
/
Ball.java
File metadata and controls
56 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package Week5;
public class Ball {
private float x, y, r, xD, yD;
public Ball(float x, float y, float radius, float xDelta, float yDelta) {
this.x = x;
this.y = y;
this.r = radius;
this.xD = xDelta;
this.yD = yDelta;
}
public float getX() { return x;}
public float getY() {return y;}
public float getRadius() {return r;}
public float getXDelta() {return xD;}
public float getYDelta() {return yD;}
public void move() {
x += xD;
y += yD;
}
public void reflectHorizontal() {
xD = -xD;
}
@Override
public String toString() {
return String.format("Ball[(%.1f,%.1f),speed=(%.1f,%.1f)]", x, y, xD, yD);
}
public static void main(String[] args) {
Ball b2 = new Ball(80.0f, 35.0f, 5.0f, 4.0f, 6.0f);
System.out.println(b2);
System.out.println("x is: " + b2.getX());
System.out.println("y is: " + b2.getY());
System.out.println("radius is: " + b2.getRadius());
System.out.println("xDelta is: " + b2.getXDelta());
System.out.println("yDelta is: " + b2.getYDelta());
for (int i = 0; i < 15; i++) {
b2.move();
System.out.println(b2);
}
// Reflecting horizontally
// b2.reflectHorizontal();
// for (int i = 0; i < 5; i++) {
// b2.move();
// System.out.println(b2);
// }
}
}