Java Reference
In-Depth Information
**6.30
(
Game: craps
) Craps is a popular dice game played in casinos. Write a program
to play a variation of the game, as follows:
Roll two dice. Each die has six faces representing values 1, 2, …, and 6, respec-
tively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called
craps
), you
lose; if the sum is 7 or 11 (called
natural
), you win; if the sum is another value
(i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either
a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win.
Your program acts as a single player. Here are some sample runs.
You rolled 5 + 6 = 11
You win
You rolled 1 + 2 = 3
You lose
You rolled 4 + 4 = 8
point is 8
You rolled 6 + 2 = 8
You win
You rolled 3 + 2 = 5
point is 5
You rolled 2 + 5 = 7
You lose
**6.31
(
Financial: credit card number validation
) Credit card numbers follow certain pat-
terns. A credit card number must have between 13 and 16 digits. It must start with:
4 for Visa cards
■
5 for Master cards
■
37 for American Express cards
■
6 for Discover cards
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card
numbers. The algorithm is useful to determine whether a card number is entered
correctly or whether a credit card is scanned correctly by a scanner. Credit card
numbers are generated following this validity check, commonly known as the
Luhn check
or the
Mod 10 check,
which can be described as follows (for illustra-
tion, consider the card number 4388576018402626):
1. Double every second digit from right to left. If doubling of a digit results in a
two-digit number, add up the two digits to get a single-digit number.
■
4388576018402626
2
*
2 = 4
2
*
2 = 4
4
*
2 = 8
1
*
2 = 2
6
*
2 = 12 (1 + 2 = 3)
5
*
2 = 10 (1 + 0 = 1)
8
*
2 = 16 (1 + 6 = 7)
4
*
2 = 8
Search WWH ::
Custom Search