You are to do Machine Project 5.31 from the text, where you are to use a for loop to test your logic

on 11 different sample card numbers. The code for the main loop in this application is as follows:

for (count = 0; count < TOTAL_TRIALS; count++)

{

System.out.print("Enter a card number: ");

cardNumber = keyboard.nextLong( );

keyboard.nextLine( );

if (isValid(cardNumber))

System.out.println("the number is valid.");

else

System.out.println("the number in not valid.");

where the 11 sample runs (Note: TOTAL_TRIALS is a global constant, a final value) are as follows:

Enter a card number: 431189543102

There are too few digits in the card, so the number is not valid.

Enter a card number: 59327698001231327

There are too many digits in the card, so the number is not valid.

Enter a card number: 8431998650521231

The card is not corporate, so the number is not valid.

Enter a card number: 3719282493103238

The card is American Express, and the number is valid.

Enter a card number: 3763098416524

The card is American Express, and the number is not valid.

Enter a card number: 4388576018410707

The card is Visa, and the number is valid.

Enter a card number: 4388576018402625

The card is Visa, and the number is not valid.

Enter a card number: 51873622090437

The card is MasterCard, and tisValid’he number is valid.

Enter a card number: 51872364091945

The card is MasterCard, and the number is not valid.

Enter a card number: 6031451987218208

The card is Discover, and the number is valid.

Enter a card number: 6224501893125

The card is Discover, and the number is not valid.

You are to use the same data I did for the sample runs in order to test the logic of your project.

Here is is how the three tests for invalidity are done:

the invalid size test result is gotten with a call to the boolean function sizeIsInvalid. The single

argument required of this function is the value of the int variable size (it doesn’t need the value

of cardNumber!). If an invalid size is found, the function should display an appropriate output

message using a System.out.print (not println, please!) statement.

the invalid prefix test result is gotten with a call to the boolean method prefixIsInvalid. This

method requires the cardNumber and size. It will display text about the card’s company when

false is returned (the card is valid) or that the card has no company (when true is returned)

If the card has the proper number of digits and belongs to one of the four corporations, the

method digitsAreValid is called. This method checks whether or not the sum of the odd and

even digit sums passes the Luhn algorithm, as described in the text. There are no display

responsibilities required of the method digitsAreValid

import java.util.Scanner; /** This Program checks the validity of a credit card number. * Its main purpose is to prompt the user to enter a Credit card * number and display whether the number is valid or invalid*/ public class Project1a { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int TOTAL_TRIALS = 11; int count; for (count = 0;count<TOTAL_TRIALS; count++) { System.out.print("Enter a Card Number : "); long cardNumber = keyboard.nextLong(); keyboard.nextLine(); if (isValid(cardNumber) == true) { System.out.println("the number is Valid."); } if (getSize(cardNumber)> 16) { System.out.println("There are to many digits in the card, so the number is not valid. "); } else if (getSize(cardNumber)<16) { System.out.println("There are too few digits in the card, so the number is not valid."); } else { System.out.println("the number is not valid."); } } keyboard.close(); } /** Return true if the card number is valid*/ public static boolean isValid(long number) { int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number); if ((total % 10 == 0) && (prefixMatched(number, 1) == true)) { return true; } else { return false; } } /** Get the result Step 2*/ public static int getDigit(int number) { if (number <= 9) { return number; } else { int firstDigit = number % 10; int secondDigit = (int) (number / 10); return firstDigit + secondDigit; } } /** Return this number if it is a single digit, otherwise, * return the sum of the two digits*/ public static int sumOfOddPlace(long number) { int result = 0; while (number > 0) { result += (int) (number % 10); number = number / 100; } return result; } /** Return of sum of odd-place digits in number*/ public static int sumOfDoubleEvenPlace(long number) { int result = 0; long temp = 0; while (number > 0) { temp = number % 100; result += getDigit((int) (temp / 10) * 2); number = number / 100; } return result; } /** Return true if the digit d is the prefix for the number*/ public static boolean prefixMatched(long number, int d) { if ((getPrefix(number, d) == 37) || (getPrefix(number, d) == 4) || (getPrefix(number, d) == 5) || (getPrefix(number, d) == 6)) { if (getPrefix(number, d) == 37 ) { System.out.print("The Card is American Express, and "); } else if (getPrefix(number, d) == 4) { System.out.print("The Card is Visa, and "); } else if (getPrefix(number, d) == 5) { System.out.print("The Card is Master, and "); } else if (getPrefix(number, d) == 6) { System.out.print("The Card is Discover Card, and "); } return true; } else { System.out.print("The Number is not corporate, so "); } return false; } /**Return the number of digits in d*/ public static int getSize(long d) { int count = 0; while (d > 0) { d = d / 10; count++; } return count; } /**Return the first k number of digits from number. If the * number of digits in number is less than k, return number.*/ public static long getPrefix(long number, int k) { if (getSize(number) < k) { return number; } else { int size = (int) getSize(number); for (int i = 0; i < (size - k); i++) { number = number / 10; } return number; } } }