In: Computer Science
I need java code for this ..thx
Question 2
Credit card numbers are not completely random sequences; they follow certain rules depending on the card issuer. A MasterCard number must meet these criteria:
• Begin with 51, 52, 53, 54, 55, or something in the range
222100-272099
• 16 digits in length
• Satisfy the Luhn formula, created by IBM scientist Hans Peter
Luhn in the 1950s
Here’s how the Luhn formula works:
Double every other digit going backwards, starting from the next-to-last digit.
For each of the doubled values that exceed 9, subtract 9.
Add up all the doubled values, along with the digits that were not doubled.
If the result is a multiple of 10, the number satisfies the Luhn formula. If the result is not a multiple of 10, the number does not satisfy the Luhn formula.
For example, 2221008763790559 is a valid MasterCard number. (Don’t worry, this was randomly gener- ated and most likely doesn’t actually belong to anyone :) You can easily verify that the number begins with 222100 and is 16 digits long. To check whether it satisfies the Luhn formula:
Original number:
2221008763790559
Double every other digit going left, starting from the next-to-last digit:
4 2 4 1 0 0 16 7 12 3 14 9 0 5 10 9
For every doubled value that exceeds 9, subtract 9:
4241007733590519
Finally,addupallthemodifieddigits: 4+2+4+1+0+0+7+7+3+3+5+9+0+5+1+9=60, which is indeed a multiple of 10.
Within your Lab4HW folder, write a program named MasterCardValidator.java that allows the user to enter a credit card number. The program should then determine and print whether that number is a valid MasterCard number according to the criteria above. You can use the randomly generated MasterCard numbers from https://www.freeformatter.com/credit-card-number-generator-validator.html to help you test.
Hint: There are several ways you can read the number from the user, but I recommend reading it as a string. You can then use strName.charAt(i) to get the individual digits. However, these are treated as char values rather than int values. To convert to int, you can use one of the following:
• For a single digit: Integer.parseInt("" + strName.charAt(i)), or strName.charAt(i) - ’0’
• For multiple digits:
Integer.parseInt(strName.substring(startIndex, endIndex))
Here are some examples of what your completed program might look
like when you run it. Underlined
parts indicate what you type in as the program is running.
Example 1
Enter a card number for validation: 2221008763790559 Valid
Example 2
Enter a card number for validation: 2221018763790559 Invalid
Example 3
Enter a card number for validation: 55 Invalid
MasterCardValidator.java
import java.util.Scanner;
public class MasterCardValidator {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a card number for validation: ");
String card = sc.nextLine().trim();
if(validateCard(card))
System.out.println("Valid");
else
System.out.println("Invalid");
}
private static boolean validateCard(String card)
{
boolean isValid = false;
// check whether the card number length is 16
boolean isLengthChecked;
if(card.length() == 16)
isLengthChecked = true;
else
isLengthChecked = false;
// check whether the card number starts with a 51, 52, 53, 54 or
55
boolean isStartingWith5152535455;
boolean isStartingWith222100To272099 = false;
String firstTwo = card.substring(0, 2);
if(firstTwo.equals("51") || firstTwo.equals("52") ||
firstTwo.equals("53")
|| firstTwo.equals("54") || firstTwo.equals("55"))
isStartingWith5152535455 = true;
else
{
// if not, check whether it starts with a number that falls within
the range 222100 - 272099
isStartingWith5152535455 = false;
String firstSix = card.substring(0, 6);
int firstSixVal = Integer.parseInt(firstSix);
if(firstSixVal >= 222100 && firstSixVal <=
272099)
isStartingWith222100To272099 = true;
else
isStartingWith222100To272099 = false;
}
/*
The card number string index starts from 0 to 15.
We need to start from the next-to-last digit, means from index 14
and continue till index 0.
But we also need all the digits of the card number for adding
up.
So, if the digit is at an even index, it will be doubled, if it is
> 9, 9 will be subtracted from it
and added to the total.
And, if the digit is at an odd index, it will simply be added to
the total.
*/
boolean isSumMultipleOf60;
int total = 0;
for(int i = card.length() - 1; i >= 0; i --)
{
int doubleVals;
if(i % 2 == 0)
{
// double each digit
doubleVals = 2 * Integer.parseInt("" + card.charAt(i));
// if the double value is greater than 9, subtract 9
if(doubleVals > 9)
doubleVals -= 9;
}
else
doubleVals = Integer.parseInt("" + card.charAt(i));
total += doubleVals;
}
// check whether the sum is a multiple of 10
if(total % 10 == 0)
isSumMultipleOf60 = true;
else
isSumMultipleOf60 = false;
// finally if all the booleans at all the phases are true, it
returns a true, else returns a false
if(isLengthChecked && (isStartingWith5152535455 ||
isStartingWith222100To272099) && isSumMultipleOf60)
isValid = true;
else
isValid = false;
return isValid;
}
}
******************************************************************** SCREENSHOT *******************************************************