Question

Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with:

  • 4 for Visa cards
  • 5 for Master cards
  • 37 for American Express cards
  • 6 for Discover cards

IBM proposed an algorithm for validating credit card numbers. The algorithm is used to determine if a card number is entered correctly or if a credit card is scanned correctly by a scanner. Almost all credit card numbers are generated following this validity check, commonly known as the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388576018402626):

  1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
    • 2 * 2 = 4
    • 2 * 2 = 4
    • 4 * 2 = 8
    • 1 * 2 = 2
    • 6 * 2 = 12 (1 + 2 = 3)
    • 5 * 2 = 10 (1 + 0 = 1)
    • 8 * 2 = 16 (1 + 6 = 7)
    • 4 * 2 = 8
  2. Now add all single-digit numbers from Step 1.
    • 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
  3. Add all digits in the odd places from right to left in the card number.
    • 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
  4. Sum the results from Step 2 and Step 3.
    • 37 + 38 = 75
  5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid.

Write a program that prompts the user to enter a credit card number as a long integer. Display whether the number is valid or invalid. Here are the three functions that you are required to write:

  1. A method AnaRL() which has a String parameter x which is the credit card number read and returns an int and is implemented as follows:
    • public static int AnaRL(String x){}
    • Use a for loop to add the values of the odd numbers of the String x.
    • In the case of 4388576018402626, the value returned is 38. (30 points)
  2. A method AnaLR() which has a String parameter x which is the credit card number read and returns an int and is implemented as follows:
    • public static int AnaLR(String x){}
    • Use a for loop to add the values of the even numbers of the String x.
    • Make sure every number that is read is doubled.
    • If a number after is doubled is >=10, make sure you add its digits only.
    • In the case of 4388576018402626, the value returned is 37. (30 points)
  3. A method Div10 which has an int sum which is the sum of both int returned from 1. and 2. and returns a boolean:
    • public static boolean Div10(int sum){}
    • If the remainder of sum divided by 10 is 0, then return true.
    • else return false.(30 points)

Your class Val looks like (10 points):

import java.util.Scanner;
public
class Val {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String x= sc.next();
int res1=AnaLR(x);
int res2=AnaRL(x);
int sum=res1+res2;
System.out.println(sum);
boolean bool=Div10(sum);
if(bool)
System.out.println("Credit Card " + x + " " + " is valid ");
else
System.out.println("Credit Card " + x + " " + " is not valid ");
}

Here are sample runs of the program:

SAMPLE 1:

Enter a credit card number as a long integer: 4246345689049834

4246345689049834 is invalid

SAMPLE 2:

Enter a credit card number as a long integer: 4388576018410707

4388576018410707 is valid

HINT: code for AnaLR():

public static int AnaLR(String num){
int total = 0;
for(int i = num.length() - 1; i >= 0; i -= 2){
total += Integer.parseInt(num.charAt(i) +"");}
return total;
}

Here is my code so far:

import java.util.Scanner;
public class Document8 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String x= sc.next();
int res1=AnaLR(x);
int res2=AnaRL(x);
int sum=res1+res2;

//System.out.println(sum);
//boolean bool=Div10(sum);
boolean bool=false;
if(bool)
System.out.println("Credit Card " + x + " " + " is valid ");
else
System.out.println("Credit Card " + x + " " + " is not valid ");
}


public static int AnaRL(String x){
int total=0;
for(int i = x.length() - 2; i >= 0; i -= 2){
      total += Integer.parseInt(x.charAt(i) +"");}
   return total;
}

public static int AnaLR(String num){
   int total = 0;
   for(int i = num.length() - 1; i >= 0; i -= 2){
   total += Integer.parseInt(num.charAt(i) +"");}
   return total;

}
}

To finish AnaRL() I was told to Extract every odd value from the string. Multiply by 2. If its doubled value <= to 9 add it. If it is >= 10 then subtract 9 from the final value but I have no idea how to do that or finish the program for that matter. I am new to programming. If you could help me finish my program in the simplest way would be very much appreciated.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package luhncheck;

import java.util.Scanner;


public class LuhnCheck {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
  
System.out.print("Enter a credit card number as a long integer: ");
Scanner scanner = new Scanner(System.in);
checkLuhn(scanner.nextLine().trim());
}

private static void checkLuhn(String CardNumber) {
int[] ints = new int[CardNumber.length()];
for (int i = 0; i < CardNumber.length(); i++) {
ints[i] = Integer.parseInt(CardNumber.substring(i, i + 1));
}

for (int i = ints.length - 2; i >= 0; i = i - 2) {
int j = ints[i];// take second digit
j = j * 2; // double it
if (j > 9) { // if it is in double digit then take reminder and add 1 to get single digit number
j = j % 10 + 1;
}
ints[i] = j;
}
int sum = 0;
// add all odd digits and doubled values of each second digit
for (int i = 0; i < ints.length; i++) {
sum += ints[i];
}
if (sum % 10 == 0) {
System.out.println(CardNumber + " is a valid ");
} else {
System.out.println(CardNumber + " is an invalid");
}
}

}

Add a comment
Answer #2
package HomeworkLib1;
import java.util.Scanner;
public class Val{
        public static void main(String[] args) {
                int n=2;
                while(n>0) {
                        Scanner sc= new Scanner(System.in);
                        System.out.println("Enter card no: ");
                        String x= sc.next();
                        int res1=AnaLR(x);
                        int res2=AnaRL(x);
                        int sum=res1+res2;
                        System.out.println(sum);
                        boolean bool=Div10(sum);
                        if(bool)
                                System.out.println("Credit Card " + x + " " + " is valid ");
                        else
                                System.out.println("Credit Card " + x + " " + " is not valid ");
                        n--;
                }
        }       //End of main
        
        public static int AnaLR(String num){
                int total = 0;
                for(int i = num.length() - 2; i >= 0; i -= 2){
                        int x = Integer.parseInt(num.charAt(i) +"");
                        x *= 2;
                        if(x<10)                     //if x is single digit number
                                total += x;             
                        else {                          //if x is double digit number
                                x = x/10 + x%10;        //add digit at tens place and digit at unit place
                                total += x;                     //Add to total
                        }
                }
                return total;
        }

        public static int AnaRL(String x){              //sum of odd places
                int total=0;
                
                for(int i = x.length() - 1; i >= 0; i -= 2){
                        total += Integer.parseInt(x.charAt(i) +"");}
                return total;
        }
        
        public static  boolean Div10(int x) {
                boolean f = false;
                if(x%10 == 0)           //If divisible
                        f = true;
                return f;
        }
}
Add a comment
Know the answer?
Add Answer to:
Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or is scanned correctly by a scanner. Almost all credit card numbers...

  • Credit card numbers adhere to certain constraints. First, a valid credit card number must have between...

    Credit card numbers adhere to certain constraints. First, a valid credit card number must have between 13 and 16 digits. Second, it must start with one of a fixed number of valid prefixes : 1 • 4 for Visa • 5 for MasterCard • 37 for American Express • 6 for Discover cards In 1954, Hans Peter Luhn of IBM proposed a “checksum” algorithm for validating credit card numbers . 2 The algorithm is useful to determine whether a card...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...

  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of...

    Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of N-squared. I.E. No nested for loops in rowIsLatin and colIsLatin. Only nested loop allowed in goodSubsquare. public class Sudoku {               public String[][] makeSudoku(String s) {              int SIZE = 9;              int k = 0;              String[][] x = new String[SIZE][SIZE];              for (int i = 0; i < SIZE; i++) {                     for (int j = 0; j < SIZE; j++)...

  • I am trying to run a couple of functions in my main and It won't output...

    I am trying to run a couple of functions in my main and It won't output anything if there is anything that you see wrong please help me out. I have to use simple, sometimes slower ways because we can use what we haven't learned yet. Here is my code. package a3; import java.util.Scanner; public class LoopsAndImages { public static void main(String[] args){ String test = "A rabbit has a carrot"; Boolean numbers = hasMoreEvenThanOdd("12344"); System.out.println(test); System.out.println(hideLetterA(test)+" This is the...

  • import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc...

    import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number");       int num = sc.nextInt();       boolean[] bool = new boolean[num];            for (int i = 0; i< bool.length; i++) {          bool[i] = true;       }       for (int i = 2; i< Math.sqrt(num); i++) {          if(bool[i] == true) {             for(int j = (i*i); j<num; j = j+i) {                bool[j] = false;...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT