Java Reference
In-Depth Information
Write a program that computes e x using this formula. Of course, you can't
compute an infinite sum. Just keep adding values until an individual
summand (term) is less than a certain threshold. At each step, you need to
compute the new term and add it to the total. Update these terms as follows:
term = term * x / n;
Follow the approach of the preceding two exercises, by implementing a
class ExpApproximator . Its first guess should be 1.
΢ Exercise P6.11. Write a program RandomDataAnalyzer that generates 100
random numbers between 0 and 1000 and adds them to a DataSet. Print out
the average and the maximum.
΢΢ Exercise P6.12. Program the following simulation: Darts are thrown at
random points o nto the square with corners (1,1) and (ɨ1,ɨ1). If the dart
lands inside the unit circle (that is, the circle with center (0,0) and radius
1), it is a hit. Otherwise it is a miss. Run this simulation and use it to
determine an approximate value for ƚ. Extra credit if you explain why this
is a better method for estimating ƚ than the Buffon needle program.
282
283
΢΢΢G Exercise P6.13. Random walk. Simulate the wandering of an
intoxicated person in a square street grid. Draw a grid of 20 streets
horizontally and 20 streets vertically. Represent the simulated drunkard
by a dot, placed in the middle of the grid to start. For 100 times, have
the simulated drunkard randomly pick a direction (east, west, north,
south), move one block in the chosen direction, and draw the dot. (One
might expect that on average the person might not get anywhere
because the moves to different directions cancel one another out in the
long run, but in fact it can be shown with probability 1 that the person
eventually moves outside any finite region. See, for example, [ 6 ,
Chapter 8 ] for more details.) Use classes for the grid and the drunkard.
΢΢΢G Exercise P6.14. This exercise is a continuation of Exercise P6.2. Most
cannonballs are not shot upright but at an angle. If the starting velocity
has magnitude v and the starting angle is Ƌ, then the velocity is a vector
with components v x = v – cos(Ƌ), v y = v – sin(Ƌ). In the x-direction the
Search WWH ::




Custom Search