Question

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 number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the “Mod 10” check, which can be described as follows (for illustration, consider the card number 4388576018402626):

I need help with these two classes

/**
   * Returns true if the supplied string
   * begins with a valid prefix
   *
   * @param s credit card number (assumed to be comprised only of digits)
   * @return true if begins with a valid CC prefix
   */
   public static boolean validPrefix(String s) {

return false; //replace with your own code

}

public static int numDigits(int num) {

return 0;//replace with your own code

}

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

As the question lacks many relevant information, I have only completed the two methods you asked for, according to my understandings. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

/**

* Returns true if the supplied string begins with a valid prefix

*

* @param s

*            credit card number (assumed to be comprised only of digits)

* @return true if begins with a valid CC prefix

*/

public static boolean validPrefix(String s) {

              // creating a String array containing valid prefixes

              String validPrefixes[] = new String[] { "4", "5", "37", "6" };

              // looping through each element in the above array

              for (int i = 0; i < validPrefixes.length; i++) {

                           // checking if String s starts with current prefix

                           if (s.startsWith(validPrefixes[i])) {

                                         // valid

                                         return true;

                           }

              }

              return false; // invalid

}

/**

* returns the number of digits in a number

*

* @param num

*            input number

* @return number of digits

*/

public static int numDigits(int num) {

              int c = 0; // initializing count of digits to 0

              // looping until all digits are counted

              do {

                           // removing last digit

                           num = num / 10;

                           // incrementing count

                           c++;

              } while (num > 0); // loop ends when num becomes 0

              return c;// returning count

}

Add a comment
Know the answer?
Add Answer to:
Credit card numbers adhere to certain constraints. First, a valid credit card number must have between...
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...

  • 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...

  • 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...

  • Consider the following credit card numbers and tell me if they are valid or not. If...

    Consider the following credit card numbers and tell me if they are valid or not. If a number is invalid, write the valid credit number by changing the “check” digit. Show your work for full marks. a. 4634 5163 4122 7983 b. 5321 4142 6379 4771 c. How many valid (according to the Luhn check) 16-digit credit card numbers are there that start with these 14 digits 4444 4444 4444 44? Give one of the valid credit card numbers.

  • 2.Consider the following credit card numbers and tell me if they are valid or not. If...

    2.Consider the following credit card numbers and tell me if they are valid or not. If a number is invalid, write the valid credit number by changing the “check” digit. Show your work for full marks. a. 4519 6731 6055 4166 b. 4515 1663 4722 3333 c. How many valid (according to the Luhn check) 16-digit credit card numbers are there that start with these 14 digits 4444 4444 4444 45? Give one of the valid credit card numbers. 3....

  • How many valid 16-digit credit card numbers are there that start with these 14 digits 4444...

    How many valid 16-digit credit card numbers are there that start with these 14 digits 4444 4444 4444 45 (according to the Luhn check)? Give one of the valid credit card numbers.

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

  • The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card...

    The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card number: 3056 9309 0259 04\n Checksum: 50\n Card status: VALID\n Credit card number: 3852 0000 0232 37\n Checksum: 40\n Card status: VALID\n Credit card number: 6011 1111 1111 1117\n Checksum: 30\n Card status: VALID\n Credit card number: 6011 0009 9013 9424\n Checksum: 50\n Card status: VALID\n Credit card number: 3530 1113 3330 0000\n Checksum: 40\n Card status: VALID\n Credit card number: 3566 0020 2036...

  • Write in java . I started it can you finish it. I will rate if code...

    Write in java . I started it can you finish it. I will rate if code works What is this lab about and what will you be working on? In this lab, you will practice new tools and concepts you've learned in class and in lab. Namely this lab will be on methods, arrays (1D and 2D), and on repetition (using loops) In this lab, you will have to complete two activities. Here is what you have to do: Activity...

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
ADVERTISEMENT