Java Reference
In-Depth Information
Customer 3 enters line at time 4. Transaction time is 1
Customer 2 begins service at time 4. Time waited is 2
Customer 4 enters line at time 6. Transaction time is 4
Customer 3 begins service at time 6. Time waited is 2
Customer 4 begins service at time 7. Time waited is 1
Customer 5 enters line at time 9. Transaction time is 1
Customer 6 enters line at time 10. Transaction time is 3
Customer 5 begins service at time 11. Time waited is 2
Customer 7 enters line at time 12. Transaction time is 4
Customer 6 begins service at time 12. Time waited is 2
Customer 8 enters line at time 15. Transaction time is 3
Customer 7 begins service at time 15. Time waited is 3
Customer 9 enters line at time 16. Transaction time is 3
Customer 10 enters line at time 19. Transaction time is 5
Customer 8 begins service at time 19. Time waited is 4
Number served = 8
Total time waited = 16
Average time waited = 2.0
Number left in line = 2
Since this example uses random numbers, another execution of the Java statements likely will
have different results.
Note: Pseudo-random numbers
Java's method Math.random generates numbers that are uniformly distributed over the interval
from 0 to 1. Actual times for processing customer transactions, however, are not uniformly dis-
tributed. They are close together, and few times are far from the average transaction time. One
such distribution is called a Poisson distribution . Ideally, this simulation should use a different
pseudo-random number generator. Since our maximum transaction time is small, however,
using Math.random probably has little effect on the average wait time.
A Problem Solved: Computing the Capital Gain in a Sale of Stock
Suppose that you buy n shares of a stock or mutual fund for d dollars each. Later you sell some
of these shares. If the sale price exceeds the purchase price, you have made a profit—a capital
gain . On the other hand, if the sale price is lower than the purchase price, you experience a loss.
We will designate a loss as a negative capital gain.
Typically, investors buy shares in a particular company or fund over a period of time. For exam-
ple, suppose that last year you bought 20 shares of Presto Pizza at $45 per share. Last month, you
bought 20 additional shares at $75 per share, and today you sold 30 shares at $65 per share. What is
your capital gain? Well, which of your 40 shares did you actually sell? Unfortunately, you cannot
pick and choose. When computing capital gains, you must assume that you sell shares in the order in
which you purchased them (meaning that stock sales are a first-in, first-out application). So in our
example, you sold the 20 shares that you bought at $45 each and 10 of the shares that you bought at
$75 each. Your cost for the 30 shares is $1650. You sold them for $1950, a profit of $300.
Design a way to record your investment transactions chronologically and to compute the
capital gain of any stock sale.
10.10
Solution design. To simplify the example, we assume that all transactions are for stocks of a single
company and that there is no commission charge for the transactions. The class StockPurchase
records the cost of a single share of stock.
 
 
Search WWH ::




Custom Search