Search

Custom Search

Thursday, November 25, 2010

computer science assignment


Now write a second client program in a file named Collisions.java. This program should simulate two particles moving in space. Its goal is to determine the number of times the two particles collide (occupy exactly the same position after the same number of steps -- the steps could be thought of as simulating time). We'll assume the particles are in a very large space so use a large number for the boundary (such as 2,000,000). Use 100,000 for the maximum number of steps. (Don't enter the commas.) Start one particle at (-3, 0) and the other at (3, 0). (You can hardcode these values into the program; no need to enter them.) Your program should contain a loop that has each particle take a step as long as the particles have not exceeded the maximum number of steps. The program then determines how often the particles have collided. Note that in order for your program to know whether or not the two different RandomWalk objects are in the same place it needs to be able to find out the position. Hence, you need to add the following two methods to the RandomWalk class.

a. int getX() - returns the x coordinate of the current position

b. int getY() - returns the y coordinate of the current position

c. Compile and run your program to make sure it works. As before run it several times.

10. In your Collisions.java program the condition to determine if the points are at the same position is a bit cumbersome. This is something that would be best put in a separate method. Add a static method to Collisions.java (after the main method) with signature public static boolean samePosition (RandomWalk p1, RandomWalk p2)

a. The method should return true if p1 and p2 are at the same position and return false otherwise. Modify your main method so it calls samePosition rather than directly testing to see if the objects are at the same position. Test the program.

11. In using random walks to simulate behavior it is often of interest to know how far away from the origin the object gets as it moves.

a. Add an instance variable maxDistance (type int) to the RandomWalk class. This should be set to 0 in each constructor.

b. Now the takeStep method needs to update this maximum when a step is taken. We'll add a support method to the class to do this. Add a private method named max that takes two integer parameters (say num1 and

b. num2) and returns the largest of the two.

c. Add code to takeStep to update maxDistance. This can be done in a single statement using the max method -- the new value of maxDistance should be the maximum of 1) the old value of maxDistance, and 2) the current distance to the origin. Note that if the current point is (-3, 15) the distance to the origin is 15; if the current point is (-10, 7) the distance to the origin is 10. Remember that Math.abs returns the absolute value of a number.

d. Finally add an accessor method to return that distance so a client program can access it:

c. public int getMaxDistance()

e. Test the maximum by adding statements in TestWalk.java to get and print the maximum distance for each of the objects after the loop that had them take and print out 5 steps (this way you can see if the maximum is correct – each step is printed).

No comments:

Post a Comment