Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this lab you will write a Java program that checks credit card strings to see if they are valid. Your program should first prompt

For this lab you will write a Java program that checks credit card strings to see if they are valid. Your program should first prompt the user to enter a string of numbers as a credit card number, or enter a blank line to quit the program. If the user doesn't quit, your program should ensure that this string is exactly 16 characters in length. If the user enters a string that is not 16 characters in length, your program should print an error message and ask again for a valid string. Your program should use the Luhn Algorithm above to compute what the check digit should be and then compare it to the actual value in the provided string and report whether the credit card number is valid or wrong. If it is wrong, your program should report what the correct check digit should be for the input value. Your program should keep asking for new values until the user enters a blank line to quit the program.

Create a new project named LuhnAlgorithm and a new Java program in that project folder named LuhnAlgorithm.java for this project. You can find a selection of valid-but-fake credit card numbers courtesy of PayPal at this link. Change the check digit on any of them to get an invalid number (note that your code should only use the 16 digit numbers and does not have to account for any of the card numbers that have any number of digits other than 16).

NOTE: You do NOT need to use arrays to solve this problem - this problem can be solved just with nested loops, try not to use methods. 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. Your code must also have a close statement for Scanner excample: Scanner keyboard = new Scanner (System.in);image text in transcribed should have a keyboard.close(); statement

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 Check digit should be: 3 Check digit is: 2 Number is not valid. Enter a credit card number (enter a blank line to quit): 5457623898234113 Check digit should be: 3 Check digit is: 3 Number is valid. 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): 5555555555554445 Check digit should be: 4 Check digit is: 5 Number is not valid. Enter a credit card number (enter a blank line to quit): Goodbye! 

Note: You will need to convert characters in your string into integer values to make this project work. 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);

The Luhn Algorithm is described at the link above, but the basic idea is:

  1. From the right-to-left, double the value of each digit that is in an even-numbered position (with the check-digit at position 1). If this doubling gives you a two-digit value for any of the numbers, then subtract 9 from the value (which is easier than adding the digits together but gets you the same result). Leave the odd-valued positions as is.
  2. Sum together all of the values except the check digit.
  3. Take the digit in the one's position of the sum. If the value of that digit is 0, then it stays as 0. If the value is greater than zero, subtract that value from 10. That value should be the check digit (note that the special case for 0 is required since "10" is not a single digit).
16 1514 13 12 11 10 8 Z 6 5 4 3 2 1 5 4 5 762 3 8 98 23 411 3 10 Position Original Value Doubled value Doubled-value adjusted Sum of values 67 Check digit is 3 (10-7-3) 8 8 1 41 732 6 89 843 8 10 12 18 2 2 2 4 You can read more about credit card numbers, as well as how the Luhn Algorithm is used in other areas of Computer Science, in this article from the Data Genetics blog (where the above example was taken from) or this lab you will write a Java program that checks credit card strings to see if they are valid. Your program should first prompt the user o enter a string of numbers as a credit card number, or enter a blank line to quit the program. If the user doesn't quit, your program should ensure that this string is exactly 16 characters in length. If the user enters a string that is not 16 characters in length, your program should print an error message and ask again for a valid string. Your program should use the Luhn Algorithm above to compute what the check dig should be and then compare it to the actual value in the provided string and report whether the credit card number is valid or wrong. If it wrong, your program should report what the correct check digit should be for the input value. Your program should keep asking for new alues until the user enters a blank line to quit the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

1. What are the major sources of stress in your life?

Answered: 1 week ago