12.11 Credit Card Check Digit Intro – Lab

Banks issue credit cards with 16 digit numbers. If you’ve never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is – all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company to contact without having to “look” at the card itself and see what the bank is on it.

Another key part of the credit card number standard is the check digit. The last digit of every credit card number is determined by the previous 15 digits through a simple mathematical formula known the Luhn Algorithm. The Lhun Algorithm can be used to check if a credit card number has been corrupted in its transmission between the vendor reading the card, and the bank which has the account. It can also be used to check to see if a credit card number is valid before transmitting it to the bank.

The Luhn Algorithm is described at the link above, and will be described in detail in your next project. For this lab all you need to know is that in a 16 digit credit card number, the last digit of the card is known as the check digit, and that the check digit is derived from a formula that is based on the sum of the other digits in the card, with odd and even digits being treated differently.

For this lab you will write a Java program that prompts the user for a credit card number continuously until the user enters a blank line. If they enter a blank line, the loop ends and a goodbye message is displayed. If they enter a number that doesn’t have exactly 16 digits, you should display an error message. Otherwise, if they enter a value that has a length of 16, you will need to print the check digit (the last digit) and a DEBUG message that is the sum of the first 15 digits. (In your project you will see the rules for verifying an actual card number and will be asked to implement those there – for this lab just provide the sum of the digits).

NOTE: You do NOT need to use arrays to solve this problem – this problem can be solved just with nested loops. Solutions that use an array where it isn’t needed will be penalized in two ways: First, you’re making the problem much harder than it needs to be, and second there will be a point deduction for use of an unnecessary array in the solution.

Note: You will need to convert characters in your string into integer values in order to compute the sum. There are two ways to do this. The first is to use Integer.parseInt() and substring() to get a character value. The following lines of code would give you the integer value of the first character of the string input:

String s = input.substring(0,1);
int val = Integer.parseInt(s);

Another way to get a numeric value from a character is to use the method Character.getNumericValue(). This takes a char value (not a String) and converts it to its correct integer value. The following lines of code would give the integer value of the first character of the String input:

char c = input.charAt(0);
int val = Character.getNumericValue(c);

Sample Output: This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.

Enter a credit card number (enter a blank line to quit): 5457623898234112
DEBUG: Sum is  68
Check digit is: 2

Enter a credit card number (enter a blank line to quit): 5457623898234113
DEBUG: Sum is 68
Check digit is: 3

Enter a credit card number (enter a blank line to quit): 5555555555554
ERROR! Number MUST have exactly 16 digits.

Enter a credit card number (enter a blank line to quit): 55555555555544456789
ERROR! Number MUST have exactly 16 digits.

Enter a credit card number (enter a blank line to quit): 5555555555554445
DEBUG: Sum is 72
Check digit is: 5

Enter a credit card number (enter a blank line to quit): 
Goodbye!
Calculate your order
Pages (275 words)
Standard price: $0.00
Client Reviews
4.9
Sitejabber
4.6
Trustpilot
4.8
Our Guarantees
100% Confidentiality
Information about customers is confidential and never disclosed to third parties.
Original Writing
We complete all papers from scratch. You can get a plagiarism report.
Timely Delivery
No missed deadlines – 97% of assignments are completed in time.
Money Back
If you're confident that a writer didn't follow your order details, ask for a refund.

Calculate the price of your order

You will get a personal manager and a discount.
We'll send you the first draft for approval by at
Total price:
$0.00
Power up Your Academic Success with the
Team of Professionals. We’ve Got Your Back.
Power up Your Study Success with Experts We’ve Got Your Back.