Question

In: Computer Science

Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375...

Credit card numbers typically consist of 13, 15, or 16 digits. For example,

4690 3582 1375 4657

is a hypothetical credit card number. The first digit designates the system. In (3.1.1), the first digit, 4, shows that the card would be a Visa card. The following digits specify other information such as the account number and bank number. (The precise meaning depends on the type of card.) The last digit it special; it is computed from the previous digits and is called the check digit. In (3.1.1), the check digit is 7 and is computed from the previous digits 4690 3582 1375 465. Credit card check digits are used to identify certain erroneous card numbers. It is not a security measure, but rather it is used to detect errors such as giving a credit card number over the phone and having it transcribed improperly or detecting an error in entering a credit card number while ordering a product online.

The check digit is computed as follows. Start from the right and skipping the check digit, double every other number. If the result of doubling is a two-digit number, addthe digits; otherwise, use the original digit. The other digits are not modified.

4 6 9 0 3 5 8 2 1 3 7 5 4 6 5

8 6 18 0 6 5 16 2 2 3 14 5 8 6 10 >>>>>>>>Double every other digit.

8 6 9 0 6 5 7 2 2 3 5 5 8 6 1 >>>>>>>>Add digits of two-digit numbers.

Sum the resulting digits

8+6+9+0+6+5+7+2+2+3+5+5+8+6+1=73.

If the last digit of the sum is 0, the check digit is 0. Otherwise, subtract the last digit of the sum from 10 to get the check digit, 10-3=7. Verify the check digit on your favorite Visa, Mastercard, AMEX, or Diners Club card. This method of calculating a check digit is called the Luhn algorithm. It is named after Hans Peter Luhn (1896-1964), who invented it while at IBM. Although originally patented, it is now in the public domain and is widely used.

For this project you will write a program that implements the Luhn algorithm given above (use the above method not another variation) for computing a credit card check digit. There will be three parts to this project. It has to in java code.

Part-1

Work the algorithm by hand. Read the description of the algorithm above and work it by hand for a credit card or debit card number you might have. You can use the hypothetical credit card number in the discussion above. Compute the credit card check digit by hand. Write out the steps neatly. Do it several times until you understand how it works. Write it up neatly, scan it and upload to Canvas. This will be the first of three items to turn in this project.

Part-2 Write up a program algorithm (pseudo code).

          Once you understand the algorithm write pseudocode that will describe how you will write you program. The conditions of the program are as follows.

  1. The program should accept as input the a credit card number.   Since the credit card numbers are typically 16 digits you will have to load it into a string since a 16 digit card number is too big for an integer.
  2. The program should check the number digits in the card numbed and verify the number is an even number. If the number of digits is even then continue processing. If the number of digits is odd issue a message to that affect and exit the program.
  3. Process the credit card number and compute the check digit using the Luhn algorithm.
  4. Display output as follows.

Enter a credit card number with an even number of digits:

4690358213754657

CSC-201 Computer Science I

Programming Project 2

Name:

Credit Card Number Entered 4690 3582 1375 4657

The check digit is 7

  1. Once you pseudo code is complete. Upload it to Canvas. This is the second item you will turn in.

Part-3

          Use you pseudo code to help you code the program in Java.   Test your program using credit card number above (4690358213754657) Test it with other credit card numbers you might have. Once you have the code completed and tested upload the source .java file to canvas. This is the third of three items you will turn in.

Solutions

Expert Solution

// LuhnAlgorithm.java: Java program to implement Luhn Algorithm to calculate the check digit of a credit card number entered

import java.util.Scanner;

public class LuhnAlgorithm {

   public static void main(String[] args) {

       Scanner keyboard = new Scanner(System.in);
       String cardNumber;
       // Input the credit card number
       System.out.println("Enter a credit card number with an even number of digits:");
       cardNumber = keyboard.next();
       System.out.println("CSC-201 Computer Science I");
       System.out.println("Programming Project 2");
       System.out.println("Name:");
       System.out.print("Credit Card Number Entered: ");
      
       // loop over the cardNumber to display card number with sections consisting of 4 digits
       for(int i=0;i<cardNumber.length();i++)
       {
           System.out.print(cardNumber.charAt(i));
           if((i+1)%4 == 0)
               System.out.print(" ");
       }
      
       // initialize totalSum to 0
       int totalSum = 0, digit;

       // loop over cardNumber from second last digit to first digit
       for(int i=cardNumber.length()-2;i>=0;i--)
       {
           digit = (int)(cardNumber.charAt(i) - '0'); // extract the ith digit and convert it to integer
           // if i is even
           if(i%2 == 0)
           {
               digit = 2*digit; // double the digit extracted
              
               // if double is greater than 9
               if(digit > 9)
               {
                   // calculate sum of digits
                   int sum = 0;
                   while(digit > 0)
                   {
                       sum += digit%10;
                       digit = digit/10;
                   }
                  
                   digit = sum; // set the digit to the sum
               }
           }
          
           // add the digit to the totalSum
           totalSum += digit;
       }
      
       // get the last digit of the totalSum
       int checkDigit = totalSum%10;
      
       // if last digit is not 0, subtract it from 10 to get the check digit
       if(checkDigit != 0)
           checkDigit = 10-checkDigit;
      
       // display the check digit
       System.out.println("\nThe check digit is "+checkDigit);

   }

}

//end of LuhnAlgorithm.java

Output:


Related Solutions

Credit Cards Credit card numbers follow patterns. Cards must have 13 - 16 digits (inclusive) Card...
Credit Cards Credit card numbers follow patterns. Cards must have 13 - 16 digits (inclusive) Card numbers must start with the number(s): 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards Given that a number has satisfied the above criteria A and B the following steps are taken to validate the number: Double every second digit from right to left.  If doubling the digit results in a two-digit number, add the two digits...
Example 1: A credit card company claims that the mean credit card debt for individuals is...
Example 1: A credit card company claims that the mean credit card debt for individuals is greater than $5,300. You want to test this claim. You find that a random sample of 27 cardholders has a mean credit card balance of $5,560 and a standard deviation of $575. At α = 0.05​, can you support the​ claim? Assume the population is normally distributed. Write out the hypothesis statements below and identify the parameter of interest. Ho: _________________________         Ha: _________________________         Which...
Date Debit Card and Credit Card Cash Sale Deposited cash in Bank 15/9/2020 $2,956.00 0 16/9/2020...
Date Debit Card and Credit Card Cash Sale Deposited cash in Bank 15/9/2020 $2,956.00 0 16/9/2020 $1,848.00 $477.50 17/9/2020 17/9/2020 $3,240.00 $350.50 18/9/2020 18/9/2020 $1,259.50 0 19/9/2020 $1,729.50 0 20/9/2020 $1,404.50 0 22/9/2020 $2,327.00 $140.50 23/9/2020 23/9/2020 $3,140.50 $330.50 24/9/2020 24/9/2020 $2,890.00 0 25/9/2020 $1,448.00 0 26/9/2020 $3,240.00 $347.00 28/9/2020 27/9/2020 $1,269.50 $332.00 28/9/2020 29/9/2020 $1,829.50 0 30/9/2020 $3,104.50 $277.00 Not yet deposited Credit Card Transactions: From Payee 10/9/2020 Dandenong City Council Council Rates $880.00 15/9/2020 India Bazar Rice, Wheat,...
Simon recently received a credit card with an 13% nominal interest rate. With the card, he...
Simon recently received a credit card with an 13% nominal interest rate. With the card, he purchased an Apple iPhone 5 for $460. The minimum payment on the card is only $20 per month. If Simon makes the minimum monthly payment and makes no other charges, how many months will it be before he pays off the card? Do not round intermediate calculations. Round your answer to the nearest month.   month(s) If Simon makes monthly payments of $60, how many...
37. Simon recently received a credit card with a 13% nominal interest rate. With the card,...
37. Simon recently received a credit card with a 13% nominal interest rate. With the card, he purchased an Apple iPhone 7 for $430.00. The minimum payment on the card is only $20 per month. If Simon makes the minimum monthly payment and makes no other charges, how many months will it be before he pays off the card? Do not round intermediate calculations. Round your answer to the nearest whole number. How many month(s)? ____________ If Simon makes monthly...
Simon recently received a credit card with a 15% nominal interest rate. With the card, he...
Simon recently received a credit card with a 15% nominal interest rate. With the card, he purchased an Apple iPhone 7 for $363.75. The minimum payment on the card is only $10 per month. If Simon makes the minimum monthly payment and makes no other charges, how many months will it be before he pays off the card? Do not round intermediate calculations. Round your answer to the nearest whole number. ______month(s) If Simon makes monthly payments of $35, how...
Jim recently received a credit card with a 15% nominal interest rate. With the card, he...
Jim recently received a credit card with a 15% nominal interest rate. With the card, he purchased a new computer for $2,000.00. The minimum monthly payment on the card is $70 per month. If he makes the minimum monthly payment and makes no other charges, how many months will it take him to pay off the card? 28.57 months 35.57 months 50.25 months 67.20 months 70.57 months
Simon recently received a credit card with an 15% nominal interest rate. With the card, he...
Simon recently received a credit card with an 15% nominal interest rate. With the card, he purchased an Apple iPhone 5 for $320. The minimum payment on the card is only $10 per month. If Simon makes the minimum monthly payment and makes no other charges, how many months will it be before he pays off the card? Do not round intermediate calculations. Round your answer to the nearest month.   month(s) If Simon makes monthly payments of $30, how many...
Simon recently received a credit card with a 15% nominal interest rate. With the card, he...
Simon recently received a credit card with a 15% nominal interest rate. With the card, he purchased an Apple iPhone 7 for $360.53. The minimum payment on the card is only $10 per month. If Simon makes the minimum monthly payment and makes no other charges, how many months will it be before he pays off the card? Do not round intermediate calculations. Round your answer to the nearest whole number.   month(s) If Simon makes monthly payments of $35, how...
Many types of identification numbers, including credit card numbers, must satisfy the Luhn Algorithm in order...
Many types of identification numbers, including credit card numbers, must satisfy the Luhn Algorithm in order to be considered "valid". This algorithm verifies the number by performing the following operation: starting from the right-most digit, double every 2nd digit. If this doubling causes that digit to be greater than 9, subtract 9 from it. Now add up all the new digits of the number (including the digits that weren't doubled). If the sum is evenly divisible by 10, then the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT