Question

In: Computer Science

I need java code for this ..thx Question 2 Credit card numbers are not completely random...

I need java code for this ..thx

Question 2

Credit card numbers are not completely random sequences; they follow certain rules depending on the card issuer. A MasterCard number must meet these criteria:

• Begin with 51, 52, 53, 54, 55, or something in the range 222100-272099
• 16 digits in length
• Satisfy the Luhn formula, created by IBM scientist Hans Peter Luhn in the 1950s

Here’s how the Luhn formula works:

  • Double every other digit going backwards, starting from the next-to-last digit.

  • For each of the doubled values that exceed 9, subtract 9.

  • Add up all the doubled values, along with the digits that were not doubled.

  • If the result is a multiple of 10, the number satisfies the Luhn formula. If the result is not a multiple of 10, the number does not satisfy the Luhn formula.

    For example, 2221008763790559 is a valid MasterCard number. (Don’t worry, this was randomly gener- ated and most likely doesn’t actually belong to anyone :) You can easily verify that the number begins with 222100 and is 16 digits long. To check whether it satisfies the Luhn formula:

    Original number:

    2221008763790559

    Double every other digit going left, starting from the next-to-last digit:

      4  2  4  1  0  0 16  7 12  3 14  9  0  5 10  9
    

    For every doubled value that exceeds 9, subtract 9:

    4241007733590519

    Finally,addupallthemodifieddigits: 4+2+4+1+0+0+7+7+3+3+5+9+0+5+1+9=60, which is indeed a multiple of 10.

    Within your Lab4HW folder, write a program named MasterCardValidator.java that allows the user to enter a credit card number. The program should then determine and print whether that number is a valid MasterCard number according to the criteria above. You can use the randomly generated MasterCard numbers from https://www.freeformatter.com/credit-card-number-generator-validator.html to help you test.

  • Hint: There are several ways you can read the number from the user, but I recommend reading it as a string. You can then use strName.charAt(i) to get the individual digits. However, these are treated as char values rather than int values. To convert to int, you can use one of the following:

    • For a single digit: Integer.parseInt("" + strName.charAt(i)), or strName.charAt(i) - ’0’

    • For multiple digits: Integer.parseInt(strName.substring(startIndex, endIndex))
    Here are some examples of what your completed program might look like when you run it. Underlined

    parts indicate what you type in as the program is running.

    Example 1

    Enter a card number for validation: 2221008763790559
    Valid
    

    Example 2

    Enter a card number for validation: 2221018763790559
    Invalid
    

    Example 3

    Enter a card number for validation: 55
    Invalid

Solutions

Expert Solution

MasterCardValidator.java

import java.util.Scanner;

public class MasterCardValidator {
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a card number for validation: ");
String card = sc.nextLine().trim();
  
if(validateCard(card))
System.out.println("Valid");
else
System.out.println("Invalid");
}
  
private static boolean validateCard(String card)
{
boolean isValid = false;
  
// check whether the card number length is 16
boolean isLengthChecked;
if(card.length() == 16)
isLengthChecked = true;
else
isLengthChecked = false;
  
// check whether the card number starts with a 51, 52, 53, 54 or 55
boolean isStartingWith5152535455;
boolean isStartingWith222100To272099 = false;
String firstTwo = card.substring(0, 2);
if(firstTwo.equals("51") || firstTwo.equals("52") || firstTwo.equals("53")
|| firstTwo.equals("54") || firstTwo.equals("55"))
isStartingWith5152535455 = true;
else
{
// if not, check whether it starts with a number that falls within the range 222100 - 272099
isStartingWith5152535455 = false;
String firstSix = card.substring(0, 6);
int firstSixVal = Integer.parseInt(firstSix);
if(firstSixVal >= 222100 && firstSixVal <= 272099)
isStartingWith222100To272099 = true;
else
isStartingWith222100To272099 = false;
}
  
/*
The card number string index starts from 0 to 15.
We need to start from the next-to-last digit, means from index 14 and continue till index 0.
But we also need all the digits of the card number for adding up.
  
So, if the digit is at an even index, it will be doubled, if it is > 9, 9 will be subtracted from it
and added to the total.
And, if the digit is at an odd index, it will simply be added to the total.
*/
boolean isSumMultipleOf60;
int total = 0;
for(int i = card.length() - 1; i >= 0; i --)
{
int doubleVals;
  
if(i % 2 == 0)
{
// double each digit
doubleVals = 2 * Integer.parseInt("" + card.charAt(i));
  
// if the double value is greater than 9, subtract 9
if(doubleVals > 9)
doubleVals -= 9;
}
else
doubleVals = Integer.parseInt("" + card.charAt(i));
  
total += doubleVals;
}
// check whether the sum is a multiple of 10
if(total % 10 == 0)
isSumMultipleOf60 = true;
else
isSumMultipleOf60 = false;
  
// finally if all the booleans at all the phases are true, it returns a true, else returns a false
if(isLengthChecked && (isStartingWith5152535455 || isStartingWith222100To272099) && isSumMultipleOf60)
isValid = true;
else
isValid = false;
  
return isValid;
}
}

******************************************************************** SCREENSHOT *******************************************************


Related Solutions

I need to show this using SQL: Credit card numbers should include asterisks in place of...
I need to show this using SQL: Credit card numbers should include asterisks in place of all digits preceding the last four digits, which will be left visible, regardless of credit card length
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers and then outputs the largest number.
i need code in javascript or htmlt convert 0 to 999 numbers into word
i need code in javascript or htmlt convert 0 to 999 numbers into word
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT