This is to be done using java and must meet all the program requirements in orde
ID: 3852938 • Letter: T
Question
This is to be done using java and must meet all the program requirements in order to be considered fully functional.
Credit card numbers follow certain patterns, which is why many web sites can determine what type of card you have as soon as you enter the card number. A credit card number must have between 13 and 16 digits and 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 for determining whether a card number has been entered correctly or 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 the sake of the illustration, consider the card number 4388576018402626):
1. Double every second digit from left to right. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
44*2=8
3
88*2=16 (1+6=7)
8
55*2=10(1+0=1)
7
66*2=12(1+2=3)
0
11*2=2
8
44*2=8
0
22*2=4
6
22*2=4
6
2. Now add all of the single-digit numbers from step 1. 4+4+8+2+3+1+7+8 = 37
3. Add all digits in the odd places from right to left in the original credit card number. 6+6+0+8+0+7+8+3=38
4. Add the results from step 2 and step 3. 37 + 38 = 75
5. If the result from step 4 is evenly divisible by 10 (is a multiple of 10), the card number is valid. Otherwise it is invalid. In the example given above, 75 is not a multiple of 10 so the number is invalid.
This exercise involves writing a program that prompts the user to enter a credit card number as a String of digits. As the digits are entered, the program will display asterisks in place of the digits. In order to do this, your program will use the JPasswordField class for input.
Display whether the card is valid or invalid and the name of the organization who issued the card.
Your validation code will be contained in a class named CcValidate and will use the following methods:
Return TRUE if the card number is valid
public static boolean isValid(String number)
Get the result from step 2
public static int sumOfEvenPlace(String 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 the sum of the odd-place digits in the number.
public static int sumOfOddPlace(String number)
Validate first digit of card number
public static Boolean isValidIssuer(String number)
Validate length of card number public static Boolean isValidLength(String number)
Explanation / Answer
From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 8 × 2 = 16), then sum the digits of the products (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9).
Take the sum of all the digits.
If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.
The check digit is obtained by computing the sum of digits then computing 9 times that value modulo 10. In algorithm form:
Compute the sum of the digits
Multiply by 9.
The last digit is the check digit.
Let us take it with example:
1. Number: 4 3 2 1 5 6 7 8 7 5 3 1 4 5 6 X
2. Double every second: 8 4 10 14 14 6 8 12
3. Sum digits >9: 8 3 4 1 1 6 5 8 5 5 6 1 8 5 3
4. Sum all digits: 8 + 3 + 4 + 1 + 1 + 6 + 5 + 8 + 5 + 5 + 6 + 1 + 8 + 5 + 3 = 69
5. Multiply sum by 9: 69 x 9 = 621
6. Take value mod 10: 621 mod 10 = 1 => x = 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.