1/ Write the pseudocode of Method Luhn in the word document that you will submit
ID: 3779042 • Letter: 1
Question
1/ Write the pseudocode of Method Luhn in the word document that you will submit along with the java file of this lab;
2/ Implement Method Luhn in the file challenge2.java and add relevant code in the main method to test it; and
3/ Provide two examples of input and the corresponding output for Method Luhn–one valid and one invalid credit card number.
Activity 1. [60 points] Method Luhn. You will have to implement the Luhn Checksum Validation algorithm to check whether a credit card number is valid or fake. This is a widely used algorithm that checks that you enter a valid credit card number when you are trying to make a purchase. Short description of the algorithm [Think Like a Programmer an Introduction to creative Problem solving by V. A. Spraulj: Using the original number, double the value of every other digit, starting with the leftmost one, as shown below. Then add the values of the individual digits together (if a doubled value now has two digits, add the digits together see below) The identification number is valid if the sum is divisible by 10 (i.e., the sum has to be a multiple of 10 Example of execution of the algorithm Suppose you want to check that your credit card number is valid. You credit card number is 8273 1232 7351 0569. Let's see how to check it: 8 2 7 3 1 2 3 2 5 6 9 First, you are going to double every other number, starting with the first number (here, it is number 8) 8 x 2 2 7 x 2 3 1 x 2 2 3 x 2 2 7 x 2 3 5 x 2 1 0 x 2 5 6 x 2 9 And you obtain 16 2 14 3 2 2 6 2 14 3 10 1 0 12 9 But we do not want double digits, so for every number that now has double digits, we add these digits 1 6 2 1 t 4 3 6 1 4 3 1 0 1 0 1 2 9 And we obtain 7 2 5 2 2 6 2 5 3 1 1 0 5 3 9 Now we add all of these numbers: 56 is not a multiple of 10, so the credit card number was a fake.Explanation / Answer
/**
* The java test program that implements
* the Luhn method to check if given credit
* card number is valid or invalid.
* */
//challenge2.java
class challenge2
{
public static void main(String[] args)
{
long cardNumber = 0;
cardNumber = 8273123273510569l;
System.out.println("Credit Card : "+cardNumber);
if(isValid(cardNumber))
System.out.println("Valid card number.");
else
System.out.println("Invalid card number.");
cardNumber = 4388576018410707l;
System.out.println("Credit Card : "+cardNumber);
if(isValid(cardNumber))
System.out.println("Valid card number.");
else
System.out.println("Invalid card number.");
}
/** Return true if the card number is valid */
public static boolean isValid(long number)
{
if(((sumOfOddPlace(number) + sumOfDoubleEvenPlace(number))%10 == 0)
&& (getSize(number) <= 16 && getSize(number) >= 13)
&& (prefixMatched(number, 4)
|| prefixMatched(number, 5)
|| prefixMatched(number, 37)
|| prefixMatched(number, 6)) )
return true;
else
return false;
}
/**Returns the sum of the even places */
public static int sumOfDoubleEvenPlace(long number)
{
int numDigits = getSize(number)-1;
int sum = 0;
number/=10;
for(int i = 0; i < numDigits; i+=2)
{
sum+= getDigit((int) ( 2 * (number % 10)));
number /= 100;
}
return sum;
}
/**Return sum of digits */
public static int getDigit(int number)
{
return ((number - number % 10) / 10 ) + number % 10;
}
/** Return sum of odd place digits in number */
public static int sumOfOddPlace(long number)
{
int numberOfDigits = getSize(number);
int sum = 0;
for(int i = 0; i < numberOfDigits; i+=2)
{
sum+= number%10;
number /= 100;
}
return sum;
}
/** Return true if the digit'd' is a prefix for number */
public static boolean prefixMatched(long number,int d)
{
if( getPrefix(number, getSize(d)) == d )
return true;
else
return false;
}
/** Return the number of digits in d */
public static int getSize(long d)
{
int n = 0;
while(d != 0)
{
d /= 10;
n++;
}
return n;
}
/** 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)
{
int numberOfDigits = getSize(number);
if(numberOfDigits - k > 0)
{
for ( int i = 0; i < numberOfDigits - k; i++) {
number /= 10;
}
return number;
} else return number;
}
}
Sample Output:
Credit Card : 8273123273510569
Invalid card number.
Credit Card : 4388576018410707
Valid card number.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.