Question: how to solve this instance of class method? /** * Updates the state of this Paddle object to correspond to a new x-position for *

how to solve this instance of class method?

/**

* Updates the state of this Paddle object to correspond to a new x-position for

* its left-side. This method has no impact on the y positioning of the Paddle.

*

* @param newLeft - The new x coordinate for the Paddle's left side. A logical

* minimum of 0 is enforced on the Paddle, so negative values

* will result in a new position of 0.

*/

public void moveTo(int newLeft)

{

if (newLeft > 0)

{

x = newLeft;

}

else

{

x = 0;

}

}

/**

* This method implements a collision detection algorithm to identify whether

* this Paddle is currently being hit by a given Ball object. It will produce a

* return value to signal which side, if any, is currently being hit.

*

* @param theBall - The Ball to examine for collision with this Paddle.

* @return - A valid TouchPosition state representing where theBall is

* intersecting this Paddle. When no collision is detected at all NONE

* should be returned. Otherwise, TOP, LEFT, or RIGHT will be returned

* corresponding to which side of this Paddle is currently being hit by

* theBall. Note, a value of BOTTOM is not possible under standard

* physics rules for brick breaker and thus is not expected.

*/

public TouchPosition isTouching(Ball theBall)

{

TouchPosition tp = TouchPosition.NONE;

int xBall = theBall.getX();

int yBall = theBall.getY();

int X = getLeft();

int Y = getTop();

int X_right = X + getWidth();

int Y_right = Y + getHeight();

//checking if the ball hits the particular brick or not

if (xBall >= X && xBall <= X_right && yBall >= Y_right && yBall <= Y) {

//if hits then returning the new TouchPosition

if (theBall.getY() == Y_right)

{

tp = TouchPosition.BOTTOM;

}

else if (theBall.getY() == y)

{

tp = TouchPosition.TOP;

}

else if (theBall.getX() == x)

{

tp = TouchPosition.LEFT;

}

else if (theBall.getX() == X_right)

{

tp = TouchPosition.RIGHT;

}

else

{

tp = TouchPosition.NONE;

}

}

//Please change it accordingly ( NONE ) because TouchPosition class is not available

return tp;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!