QuestionQuestion

PROBLEM DESCRIPTION:
Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with:
o 4 for Visa cards
o 5 for Master cards
o 37 for American Express cards
o 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.
o 2 * 2 = 4
o 2 * 2 = 4
o 4 * 2 = 8
o 1 * 2 = 2
o 6 * 2 = 12 (1 + 2 = 3)
o 5 * 2 = 10 (1 + 0 = 1)
o 8 * 2 = 16 (1 + 6 = 7)
o 4 * 2 = 8
2. Now add all single-digit numbers from Step 1.
o 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.
o 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
4. Sum the results from Step 2 and Step 3.
o 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:
o public static int AnaRL(String x){}
o Use a for loop to add the values of the odd numbers of the String x.
o 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:
o public static int AnaLR(String x){}
o Use a for loop to add the values of the even numbers of the String x.
o Make sure every number that is read is doubled.
o If a number after is doubled is >=10, make sure you add its digits only.
o 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:
o public static boolean Div10(int sum){}
o If the remainder of sum divided by 10 is 0, then return true.
o else return false.

Your class Val looks like:
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;
}

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

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);
       System.out.println("res1 = " + res1);
       int res2 = AnaRL(x);
       System.out.println("res2 = " + res2);
       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 ");
       }
    }

    private static int AnaLR(String x) {
       int val = 0;
       int num;
       // Use a for loop to add the values of the even numbers of the String x....

By purchasing this solution you'll be able to access the following files:
Val.java.

50% discount

Hours
Minutes
Seconds
$13.50 $6.75
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats