Credit card numbers contain a check digit that is used to help detect errors and
ID: 3680724 • Letter: C
Question
Credit card numbers contain a check digit that is used to help detect errors and verify that the card number is valid. (You can read about the Luhn algorithm at https://en.wikipedia.org/wiki/Luhn_algorithm.) The check digit can help detect all single-digit errors and almost all transpositions of adjacent digits. In this problem we write some methods that will allow us to quickly check whether a card number is invalid. We will limit our numbers to seven digits and the rightmost digit will be the check digit. For example, if the credit card number is 2315778, the check digit is 8. We number the digit positions starting at the check digit, moving left. Here's the numbering for credit card number 2315778: In order to verify that the card number is correct we will need to "decode" every digit. The decoding process depends on the position of the digit within the credit card number: a) If the digit is in an odd-numbered position, simply return the digit, b) If the digit is in an even-numbered position, double it. If the result is a single digit, return it; otherwise, add the two digits in the number and return the sum. For example, if we decode 8 and it is in an odd position, we return 8. On the other hand, if 8 is in an even position, we double it to get 16, and then return 1 + 6 = 7. Decoding 4 in an odd position would return 4, and decoding it an even position would return 8. As a first step to being able to being able to detect invalid numbers: 1. create a new NetBeans project 2. download the source file CPS150_Lab17_1.java 3. import the source file into your project (see the online instructions on importing the source file into your project) 4. complete the Java functionExplanation / Answer
CPS150_Lab17_1.java
public class CPS150_Lab17_1
{
public static void main(String args[])
{
boolean even=false;
int counter=1;
while(counter<=9)
{
System.out.printf("decode(%d,false) -> ",counter);
System.out.println(decode(counter,even));
counter++;
}
even=true;
counter=1;
while(counter<=9)
{
System.out.printf("decode(%d,false) -> ",counter);
System.out.println(decode(counter,even));
counter++;
}
}
public static int decode(int digit,boolean position)
{
if(digit>=10)
{
return -1;
}
if(position)
{
int x=0;
x=2*digit;
//x%10 will give the last digit of a two digit number and x/10 will give the first one
return (x/10+x%10);
}
else
{
return digit;
}
}
}
output:
decode(1,false) -> 1
decode(2,false) -> 2
decode(3,false) -> 3
decode(4,false) -> 4
decode(5,false) -> 5
decode(6,false) -> 6
decode(7,false) -> 7
decode(8,false) -> 8
decode(9,false) -> 9
decode(1,false) -> 2
decode(2,false) -> 4
decode(3,false) -> 6
decode(4,false) -> 8
decode(5,false) -> 1
decode(6,false) -> 3
decode(7,false) -> 5
decode(8,false) -> 7
decode(9,false) -> 9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.