Page 192 - Introduction to Programming with Java: A Problem Solving Approach
P. 192

                158 Chapter 5 Using Pre-Built Methods
Note the round method in Figure 5.2. How is it different from using an (int) type cast operator on a double value? The (int) operator truncates the fraction, whereas the round method rounds up if the fraction is 􏰃 0.5.
As shown in Figure 5.2, Math’s random method returns a uniformly distributed value between 0.0 and 1.0, not including 1.0. “Uniformly distributed” means that there’s the same chance of getting any value within the specified range. In other words, if you have a program that calls random, the chances are the same for random returning 0.317, 0.87, 0.02, or any value between 0.0 and 1.0, not including 1.0.
Why would you want to call the random method? If you need to analyze a real-world situation that involves random events, you should consider writing a program that uses the random method to model the random events. For example, if you work for a city transportation department, and you’re in charge of im- proving traffic flow at traffic light intersections, you could write a program that uses the random method to model the arrival of automobiles at the traffic lights. For each traffic light that you’re interested in, you’d set the traffic light’s cycle time (e.g., two minutes between each new green signal) and then simulate auto- mobiles arriving at the traffic light at random intervals. You’d run the program so that it simulates one week of traffic flow, and you’d keep track of average wait time for all vehicles. You’d then adjust the traffic light’s cycle time (e.g., one minute and forty-five seconds between each new green signal), run the simulation again, and determine which traffic light cycle time produces shorter average wait times.
Let’s wrap up the discussion of Figure 5.2’s Math methods with a complete program example. Sup- pose you want to calculate the length of the hypotenuse of a right triangle, given the lengths of its base and height, as shown in this picture:
Apago PDF Enhancer
      height
hypotenuse
base
hypotenuse = height2 + base2
      Figure 5.3 contains a simple program that asks the user to provide base and height values. Then it uses Math’s sqrt method to calculate and print the square root of the sum of the squares. Notice that we did not use the Math.pow method to square the base and square the height. For small powers, it’s more efficient just to multiply them out.
Trigonometric Math Methods
Figure 5.4 contains API headings and descriptions for some of the methods in the Math class that can help you solve problems in trigonometry. The sin, cos, and tan methods implement the sine, cosine, and tangent functions, respectively. The asin, acos, and atan methods implement the arcsine, arccosine, and arctangent functions, respectively. The trigonometric and inverse trigonometric functions all use or return angle values as radians, not degrees. Using or assuming degrees is a common programming error. Be careful!






















































































   190   191   192   193   194