Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Consider the following program that checks if a credit number is a valid card number. The last digit of a credit card number is the

  1. Consider the following program that checks if a credit number is a valid card number. The last digit of a credit card number is the check digit, which protects against transaction errors. The following method is used to verify credit card numbers. Following steps explains the algorithm in determining if a credit card number is a valid card.

Starting from the right most digit, form the sum of every other digit. For example, if the credit card is number is 3125145643589795 then you form the sum 5+7+8+3+6+4+5+1 = 39

Double each digit that we have not included in the preceding step. Add all digits of resulting numbers. For example, with the number given above, doubling the digits starting with next to last one, yields 18, 18, 10, 8, 10, 2, 4,6. Adding all digits in these values yield 1+8+1+8+1+0+8+1+0+2+4+6 = 40

Add sum of the two preceding steps. If the last digit of the result is zero, then the number is valid number

Credit card number cannot start with 0

The program asks the user 16-digit credit card number and the printout if the credit card is valid or invalid card.

  1. This java program has two defects. Identify defects in this program. You can assume that the user will enter a 16-digit number. No need to consider other types of inputs such as strings, characters etc.
  2. How do you fix the defects in the program? Submit the corrected program.

Below is the code for said program:

import java.util.Scanner;

public class Assignment {

public static void main(String[] args)

{

// since the card number is 8 digits, we need long integer

long cardNumber=0;

final int CARD_LENGHT = 16;

int length, sum1=0, sum2=0, finalSum=0;

int[] digitArray = new int[CARD_LENGHT];

Scanner scan = new Scanner(System.in);

// reads the eight digit card number

do{

System.out.println("Enter the 16 digit card number: " );

cardNumber = scan.nextLong();

// get the card length

length = (int) (Math.log10(cardNumber) + 1);

if (length != CARD_LENGHT)

{

System.out.println("Invalid card number, need to have 8

digits");

}

}while(length != CARD_LENGHT);

// get each digit from the card number and set the digitArray

int i = CARD_LENGHT - 1;

while(cardNumber > 0)

{

digitArray[i] = (int)(cardNumber%10);

cardNumber = cardNumber/10;

i = i - 1;

}

// starting from the right most digit add every other digit to sum 1

for(i= CARD_LENGHT - 1; i >=0 ; i= i -2)

{

sum1 = sum1 + digitArray[i];

}

// get each digit not counted in above, multiply by 2 and add each

digit of multiplied

// numbers to sum 2

for(i= CARD_LENGHT - 2; i >= 0; i= i -2)

{

int num = digitArray[i]*2;

sum2 = sum2 + num;

}

// find the final sum

finalSum = sum1 + sum2;

// check if the last digit of the final sum is 0

if(finalSum%10 == 0)

{

System.out.println("Valid Card");

}else

{

System.out.println("Invalid Card");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions