Java Reference
In-Depth Information
5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise,
it is invalid. For example, the number 4388576018402626 is invalid, but the
number 4388576018410707 is valid.
Write a program that prompts the user to enter a credit card number as a
long
integer. Display whether the number is valid or invalid. Design your program to
use the following methods:
/** Return true if the card number is valid */
public static boolean
isValid(
long
number)
/** Get the result from Step 2 */
public static int
sumOfDoubleEvenPlace(
long
number)
/** Return this number if it is a single digit, otherwise,
* return the sum of the two digits */
public static int
getDigit(
int
number)
/** Return sum of odd-place digits in number */
public static int
sumOfOddPlace(
long
number)
/** Return true if the digit d is a prefix for number */
public static boolean
prefixMatched(
long
number,
int
d)
/** Return the number of digits in d */
public static int
getSize(
long
d)
/** Return the first k number of digits from number. If the
* number of digits in number is less than k, return number. */
public static long
getPrefix(
long
number,
int
k)
Here are sample runs of the program:
Enter a credit card number as a long integer:
4388576018410707
4388576018410707 is valid
Enter a credit card number as a long integer:
4388576018402626
4388576018402626 is invalid
**5.32
(
Game: chance of winning at craps
) Revise Exercise 5.30 to run it 10,000 times
and display the number of winning games.
**5.33
(
Current date and time
) Invoking
System.currentTimeMillis()
returns the
elapsed time in milliseconds since midnight of January 1, 1970. Write a program
that displays the date and time. Here is a sample run:
Current date and time is May 16, 2012 10:34:23