I am try to solve OOP Java problems and I reached a class named "Ball", The class is easy to design but I have problem that is how to get xDelta and yDelta so I can add them to initial x and y to get the position of the ball after moving it?
Now I wrote this code but the xDelta and yDelta are wrong. What is the need for speed and direction in the constructor?
This is the class diagram:
This is the class code:
public class Ball {
private float x;
private float y;
private int radius;
private float xDelta;
private float yDelta;
public Ball(float x,float y,int radius,int speed,int direction){
this.x=x;
this.y=y;
this.radius=radius;
}
float getX() {
return x;
}
void setX(float x) {
this.x=x;
}
float getY() {
return y;
}
void setY(float y) {
this.y=y;
}
int getRadius() {
return radius;
}
void setRadius(int radius) {
this.radius = radius;
}
float getXDelta() {
return xDelta;
}
void setXDelat(float xDelta) {
this.xDelta=xDelta;
}
float getYDelta() {
return yDelta;
}
void setYDelat(float yDelta) {
this.yDelta=yDelta;
}
void move() {
x += xDelta ;
y += yDelta ;
}
void reflectHorizontal() {
xDelta = -xDelta ;
}
void reflectVertical() {
yDelta = -yDelta ;
}
public String toString () {
return "Ball[("+x+","+y+"),speed=("+xDelta+","+yDelta+")]";
}
}