Question: Java: make sure program compiles before posting. The last digit of a credit card number is the check digit, which protects against transcription errors such

Java: make sure program compiles before posting.

The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following algorithm is used to verify the actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16:

all digits at odd index should be added to form the sum.

For example, if the credit card number is 43589795, then you form the sum 3 + 8 + 7 + 5 is 23

double each of the digits that are at even index and add all digits of the resulting numbers to the sum.

For example, with the number given above, doubling the digits yields 8 10 18 18. Adding all digits in these values yields 8 + 1 + 0 + 1 + 8 + 1 + 8 is 27, so the final sum total is 23 + 27 which is 50

if the last digit of the final sum is 0, the credit card number is valid. In our case the last digit of 50 is 0, so the number is valid.

Write a program that implements this algorithm. The user should supply an 8-digit number, and the program should print the calculated sum and the message if the given card is valid or not. The program must allow user to check another card if the user wishes to do so.

Use the following flow of the program:

do {  do  {  prompt the user for credit card number  }while (the entered card does not match the specified pattern)  Using a for loop go over the input card number, one character at the time starting with the first character (see Lecture Notes Chapter 6 "Processing a String" slide)  {  - utilize Character.digit method to convert the char to and int  - if the current character is at odd index add the int to sum total  - if the current character is at even index double the int  and then utilize division by 10 and modulus by 10 to get   the first and last digit respectively, add the digits to the sum total  }  Check if the calculated sum ends with zero (again use % operator)  Display the results  Ask the user if (s)he wants to convert another card  }while (user wants to convert another card) 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!