Question

Need in C# with pseudocode comments please! A Ball has an X and Y position. Equally...

Need in C# with pseudocode comments please!

A Ball has an X and Y position. Equally important, a ball has an x velocity and a y velocity. Every time the ball moves (in one unit of time), it changes its X and Y position by its x and y velocity. However, before moving it, you need to check to see if it’s touching a wall. If so, you need to reverse either its x or y velocity depending on whether or not its touching a wall. What is the ball’s location if its touching a wall? For simplicity, we’re going to assume that the ball is on a 10x10 field and that the x and y velocities can only be -1, 0, or +1.


Your task is to 1) write class Ball that has the variables/attributes above, 2) has a constructor that takes in the starting X and Y position as well as the starting X and Y velocity, 3) has a method called “move” that takes no parameters and updates the position of the ball and 4) has a print statement called “print” that takes no parameters and prints out the ball’s current position.

You must call the class “Ball” and put it in a file called Ball(.java, .cs, .cpp, .h). To test your ball, you should create a file called Assignment6A(.java, .cs, .cpp) that creates a ball based off of user input and calls the “move” and “print” methods of the ball the number of times the user wants. It should behave like the sample output below.

Sample Output #1:

x:

7

y:

4

x velocity:

1

y velocity:

1

Number of moves:

20

X:7 Y:4

X:8 Y:5

X:9 Y:6

X:8 Y:7

X:7 Y:8

X:6 Y:9

X:5 Y:8

X:4 Y:7

X:3 Y:6

X:2 Y:5

X:1 Y:4

X:0 Y:3

X:1 Y:2

X:2 Y:1

X:3 Y:0

X:4 Y:1

X:5 Y:2

X:6 Y:3

X:7 Y:4

X:8 Y:5

X:9 Y:6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

//Ball.cs file

using System;

class Ball{
    //attributes
    private int x;
    private int y;
    private int x_vel;
    private int y_vel;
    
    //static variables indicating field size
    private static int MAX_X=10;
    private static int MAX_Y=10;
    
    //constructor taking all values
    public Ball(int X, int Y, int xvel, int yvel){
        x=X;
        y=Y;
        x_vel=xvel;
        y_vel=yvel;
    }
    
    //method to move the ball
    public void move(){
        //if x is at the edge, reversing x_vel
        if(x==MAX_X-1 || x==0){
            x_vel=-x_vel;
        }
        //adding x_vel to x
        x=x+x_vel;
        
        //repeating the same for y
        if(y==MAX_Y-1 || y==0){
            y_vel=-y_vel;
        }
        y=y+y_vel;    
    }
    
    //method to print current coordinates of the Ball
    public void print(){
        Console.WriteLine("X:"+x+" Y:"+y);
    }
}

//Assignment6A.cs file


using System;


class Assignment6A{
    
    
    public static void Main(string[] args)
    {
        //asking and reading each required input. assuming all inputs are valid
        Console.WriteLine("x:");
        int x=int.Parse(Console.ReadLine());
        
        Console.WriteLine("y:");
        int y=int.Parse(Console.ReadLine());
        
        Console.WriteLine("x velocity:");
        int xvel=int.Parse(Console.ReadLine());
        
        Console.WriteLine("y velocity:");
        int yvel=int.Parse(Console.ReadLine());
        
        Console.WriteLine("Number of moves:");
        int n=int.Parse(Console.ReadLine());
        
        //creating a Ball and printing initial position
        Ball b=new Ball(x,y,xvel,yvel);
        b.print();
        
        //looping for n times
        for(int i=0;i<n;i++){
            //moving and printing new position
            b.move();
            b.print();
        }
        
        //wait for user input to quit, remove if you don't need this
        Console.Write("\n\nPress any key to continue . . . ");
        Console.ReadKey(true);
    }
}

/*OUTPUT*/

x:

7

y:

4

x velocity:

1

y velocity:

1

Number of moves:

20

X:7 Y:4

X:8 Y:5

X:9 Y:6

X:8 Y:7

X:7 Y:8

X:6 Y:9

X:5 Y:8

X:4 Y:7

X:3 Y:6

X:2 Y:5

X:1 Y:4

X:0 Y:3

X:1 Y:2

X:2 Y:1

X:3 Y:0

X:4 Y:1

X:5 Y:2

X:6 Y:3

X:7 Y:4

X:8 Y:5

X:9 Y:6

Press any key to continue . . .

Add a comment
Know the answer?
Add Answer to:
Need in C# with pseudocode comments please! A Ball has an X and Y position. Equally...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q: Python ball in billiards board (Bouncing ball) Ball class contains two essential components: position (ball.x. ball....

    Q: Python ball in billiards board (Bouncing ball) Ball class contains two essential components: position (ball.x. ball.v. ball.pos) and velocity wwww ww (ball.xvel ball.yvel, ball.vel). What you must design is two functions, one of which is update ball It is a function which accepts a ball as an argument, and outputs a new ball representing the state of the ball in the instant after it next bounces off of a wall As an illustration of the expected behaviour, and how...

  • At time t = 1, a particle is located at position (x, y) = (4, 5)....

    At time t = 1, a particle is located at position (x, y) = (4, 5). If it moves in the velocity field F(x, y) = xy − 3, y2 − 8 find its approximate location at time t = 1.03.

  • I need this the Pseudocode and also in C# Stay on the Screen! Animation in video...

    I need this the Pseudocode and also in C# Stay on the Screen! Animation in video games is just like animation in movies – it’s drawn image by image (called “frames”). Before the game can draw a frame, it needs to update the position of the objects based on their velocities (among other things). To do that is relatively simple: add the velocity to the position of the object each frame. For this program, imagine we want to track an...

  • 1. If a ball is thrown vertically upward with a certain velocity, its height (in feet)...

    1. If a ball is thrown vertically upward with a certain velocity, its height (in feet) after t seconds is s(t) = 28t - 4tº. Answer the following questions with correct units. (a) Find the velocity and acceleration of the ball after 2 sec? (b) What is the maximum height reached by the ball? (c) Determine the velocity of the ball when it hits the ground. 4 6 6 6 6 2. Find the derivatives of the following functions. You...

  • Two girls playing catch Julie throws a ball to her friend Sarah. The ball leaves Julie's...

    Two girls playing catch Julie throws a ball to her friend Sarah. The ball leaves Julie's hand a distance 1.5 meters above the ground with an initial speed of 17 m/s at an angle 37 degrees; with respect to the horizontal. Sarah catches the ball 1.5 meters above the ground. 1) What is the horizontal component of the ball’s velocity right before Sarah catches it? m/s 2) What is the vertical component of the ball’s velocity right before Sarah catches...

  • ***Program must compile under Ubuntu! (35 pts) Write a C++ program A4p3.cpp with a class that...

    ***Program must compile under Ubuntu! (35 pts) Write a C++ program A4p3.cpp with a class that is a derived class of your class from problem 2. Add a private intmember variable var2 to this class. Initialize the member variables var and var2 with values between 1 and 50 using a constructor that takes two integer parameters. Add a public member function called getsp that should print out the sum and product of var and var2. In your main function, create...

  • PLEASE ANSWER BOTH PROBLEM SETS PLEASE ANSWER BOTH (1 point) A steel ball weighing 128 pounds...

    PLEASE ANSWER BOTH PROBLEM SETS PLEASE ANSWER BOTH (1 point) A steel ball weighing 128 pounds is suspended from a spring. This stretches the spring 528 feet. The ball is started in motion from the equilibrium position with a downward velocity of 7 feet per second. The air resistance (in pounds) of the moving ball numerically equals 4 times its velocity (in feet per second). Suppose that after t seconds the ball is y feet below its rest position. Find...

  • 6. An object moves along the x-axis. The graph shows its position x as a function...

    6. An object moves along the x-axis. The graph shows its position x as a function of time t. Find ( point) the average velocity of the object from points B to C x (m) 10 8 6 2 t (s) 0 1 23 4 5 6 1.8 m/s 1.0 m/s 1.5 m/s 0.67 m/s 10. A person is walking briskly in a straight line. The figure shows a graph of the person's position (1 point) x as a function...

  • At time t = 1, a particle is located at position (x,y) = (3,1). If it...

    At time t = 1, a particle is located at position (x,y) = (3,1). If it moves in the velocity field F(x,y) = <xy - 3, y2 - 9> find its approximate location at time t = 1.02. (x,y) = (_____)

  • by netBeans C++ java by netBeans C++ java by netBeans C++ java 1. Answer the following...

    by netBeans C++ java by netBeans C++ java by netBeans C++ java 1. Answer the following question a) (4 marks) Write a Java method, which takes an integer array and two integers x and y as input parameters. The method should exchange the value in position x with the value in the position y in the array. Example: If x = 1, y=2 Input Array Output Array b) (4 marks) Write the main method to do the following: 1. Define...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT