Java Reference
In-Depth Information
2.
In the game of craps, a pass line bet proceeds as follows. Two six-sided dice are
rolled; the first roll of the dice in a craps round is called the “come out roll.” A
come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12
automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that
number becomes “the point.” The player keeps rolling the dice until either 7 or
the point is rolled. If the point is rolled first, then the player wins the bet. If a 7 is
rolled first, then the player loses.
Write a program that simulates a game of craps using these rules without human
input. Instead of asking for a wager, the program should calculate whether the
player would win or lose. The program should simulate rolling the two dice and
calculate the sum. Add a loop so that the program plays 10,000 games. Add
counters that count how many times the player wins, and how many times the
player loses. At the end of the 10,000 games, compute the probability of winning
[i.e., Wins / (Wins + Losses) ] and output this value. Over the long run, who is
going to win the most games, you or the house?
Note: To generate a random number x , where 0 < x <= 1 , use x =
Math.random( ); . For example, multiplying Math.random( ) by 6 and
converting to an integer results in a random integer that is between 0 and 5.
3.
One way to estimate the adult height of a child is to use the following formula,
which uses the height of the parents:
H male_child = ((H mother × 13/12) + H father )/2
H female_child = ((H father × 12/13) + H mother )/2
All heights are in inches. Write a program that takes as input the gender of the child,
the height of the mother in inches, and the height of the father in inches, and
outputs the estimated adult height of the child in inches. The program should allow
the user to enter a new set of values and output the predicted height until the user
decides to exit. The user should be able to input the heights in feet and inches, and
the program should output the estimated height of the child in feet and inches. Use
the int data type to store the heights.
4.
It is difficult to make a budget that spans several years, because prices are
not stable. If your company needs 200 pencils per year, you cannot simply use
this year's price as the cost of pencils two years from now. Because of inflation,
the cost is likely to be higher than it is today. Write a program to gauge the
expected cost of an item in a specified number of years. The program asks for
the cost of the item, the number of years from now that the item will be pur-
chased, and the rate of inflation. The program then outputs the estimated cost
of the item after the specified period. Have the user enter the inflation rate as a
percentage, such as 5.6 (percent). Your program should then convert the per-
cent to a fraction, such as 0.056, and should use a loop to estimate the price
adjusted for inflation.
Search WWH ::




Custom Search