Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Q2) Design a class named Ball that models a moving ball in two-dimensional space. The class can - staml uuudie lastr sture tre iasi r-mxis
Q2) Design a class named Ball that models a moving ball in two-dimensional space. The class can - staml uuudie lastr sture tre iasi r-mxis pusitiun ur me mustrecent vain mat nas muveu. Methods: - A default constructor that sets initial position of the ball to (0,0). - A constructor that receives two arguments newX and newY and sets initial position of the ball to them. - getX() a getter that returns the value of x. - getY() a getter that returns the value of y. - move(double xDisp, double yDisp):void, which changes x and y by the given x Disp and yDisp, respectively. A ball must not move if it is going to finish its movement in a position occupied by another ball (Hint: use last X and lastY to determine this. If movement is allowed, then change the values of last X and lastY). - getDistTraveledX():double. Returns the distance traveled by the current ball throughout all of its moves on X-Axis. - getDistTraveledY():double. Returns the distance traveled by the current ball throughout all its moves on Y-Axis. - public static double getTotDistXAllBalls() that return the distance traveled by all balls throughout all of the moves on X-Axis. - public static double getTotDistYAllBalls() that return the distance traveled by all balls throughout all of the moves on Y-Axis. - tostring () , which returns the string "The ball is at (x,y) ". Create a class testBall to test our Ball class. This class will do the following: - It creates a ball b1 with a position (2,2). - Then it moves b1 by (3, -2). (After you move a ball, you must print its location) - Then it moves b1 by (2,7). - Then it creates a new ball b2 with a position (0,0). - Then it moves b2 by (7,7). Notice that b2 must not move since it will otherwise occupy the same place occupied by the b1. - Then it moves b2 by (2,4). Finally, it prints the last position of the two balls using toString method and the total distance traveled on both X-Axis and Y-Axis by each ball and by all balls. Sample Run B1 Ball is at (2.0,2.0) B1 Bal1 is at (5.0,0.0) B1 Ball is at (7.0,7.0) B2 Ball is at (0.0,0.0) The ball in position (0.0,.) cannot be moved to position (7.0,7.0) B2 Ball is at (0.0,0.0) B2 Ball is at (2.0,4.0) Last position for ball B1 Ball is at (7.0,7.0) Last position for ball B2 Ball is at (2.0,4.0) Distance travelled on X for b1 5.0 Distance travelled on y for b1 9.0 Distance travelled on X for b2 2.0 Distance travelled on y for b2 4.0 Distance travelled on X for all balls 7.0 Distance travelled on Y for all balls 13.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started