0

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: 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+")]";
    }
}
howie
  • 2,587
  • 3
  • 27
  • 43
  • the problem is with the " move " method , I don't know how to get the xDelta and yDelta – Mahmoud.Sh Jul 18 '20 at 17:28
  • 1
    What is the problem? That is the correct way to get `xDelta` and `yDelta` in `move()`. Is there an actual error? – Elliott Frisch Jul 18 '20 at 17:29
  • What do you mean by "the xDelta and yDelta are wrong"? – Unmitigated Jul 18 '20 at 17:30
  • @ElliottFrisch there is no value for xDelta and yDelta , I must get their values maby with a certain calculations but I can't find anyone I've searched but didn't find anything close – Mahmoud.Sh Jul 18 '20 at 17:32
  • 1
    @Mahmoud.Sh What do you mean by that? Your implementation of it looks fine according to the description. – Unmitigated Jul 18 '20 at 17:33
  • I think the first problem is you need to **set** their values. Because they have value. It's just `0`. As for *what is the need for speed and direction in the constructor ?* - it's **your** code. – Elliott Frisch Jul 18 '20 at 17:34
  • if someone can solve this it would be great help – Mahmoud.Sh Jul 18 '20 at 17:39
  • I'm not understanding why you are tying to "get" the deltaX/Y? Normally you have a current x/y location for the ball. Your class then has a property deltaX/Y which is used to move() the ball when you do animation. So you don't calculate the deltaX/Y, you make it a property of your class. See [Motion Using the Keyboard](https://tips4java.wordpress.com/2013/06/09/motion-using-the-keyboard/) for working examples. Or you can play with: https://stackoverflow.com/a/54028681/131872 which uses a Swing Timer to generate constant animation of "balls" on a panel. – camickr Jul 18 '20 at 18:19

1 Answers1

0

Let direction be the direction of the ball flight in degrees (0 to 360). Add this to your constructor.

double radian = Math.toRadians(direction);
this.xDelta = (float)Math.cos(radian) * speed;
this.yDelta = (float)Math.sin(radian) * speed;