Question

What is this lab about and what will you be working on? In this lab, you will practice new tools and concepts youve 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 1. [60 points] Method Lubn, You will have to implement the Luhn Checksum Validation algorithm to check whether a credit card number is valid or fake. This is a widely used algorithm that checks that you enter a valid credit card number when you are trying to make a purchase. Short description of the algorithm [Think Like a Programmer an Introduction to Creative Problem Solving by V. A. Spraull: Using the original number, double the value of every other digit, starting with the leftmost one, as shown below. Then add the values of the individual digits together (if a doubled value now has two digits, add the digits together - see below). The identification number is valid if the sum is divisible by 10 〔i.e., the sum has to be a multiple of 10 Example of execution of the algorithm Suppose you want to check that your credit card number is valid. You credit card number is 8273 1232 7351 0569. Lets see how to check it: 9 First, you are going to double every other number, starting with the first number (here, it is number 8) And you obtain 9 But we do not want double digits, so for every number that now has double digits, we add these digits And we obtain: 9 w we add all of these numbers: 7 2 5322 6+ 2 5 3 +1 +1053 +9 - 56 56 is not a multiple of 10, so the credit card number was a fake...

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

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

Please find the code below::

package classes1;

import java.util.Scanner;

public class compLab1 {

public static boolean Luhn(long creditCard) {

if((sumOfDoubleEvenPlace(creditCard) +

sumOfOddPlace(creditCard)) % 10 == 0){

return true;

}else{

return false;

}

}

// Return sum of odd-place digits in number

public static int sumOfOddPlace(long number)

{

int sum = 0;

String num = number + "";

for (int i = getSize(number) - 1; i >= 0; i -= 2)

sum += Integer.parseInt(num.charAt(i) + "");   

return sum;

}

// Get the result from Step 2

public static int sumOfDoubleEvenPlace(long number)

{

int sum = 0;

String num = number + "";

for (int i = getSize(number) - 2; i >= 0; i -= 2)

sum += getDigit(Integer.parseInt(num.charAt(i) + "") * 2);

return sum;

}

// Return the number of digits in d

public static int getSize(long d)

{

String num = d + "";

return num.length();

}

// Return this number if it is a single digit, otherwise,

// return the sum of the two digits

public static int getDigit(int number)

{

if (number < 9)

return number;

//if number if of two digit

return number / 10 + number % 10;

}

public static void main(String[] args) {

//scanner to get user input

Scanner sc = new Scanner(System.in);

System.out.print("Enter credit card number as long integer : ");

//read long from stdin

long number = sc.nextLong();

//call function to check credit card validity

System.out.println(number + " is " +

(Luhn(number) ? "valid" : "invalid"));

sc.close();

}

}

output:

Console 3 terinated> compLab1 Java Application] CAProgram FilesJavalire7\bin javaw.exe (Oct 23, 2018, 12:00:46 A Enter credit card number as long integer: 8273123273510569 8273123273510569 is invalid

Add a comment
Know the answer?
Add Answer to:
Write in java . I started it can you finish it. I will rate if code...
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
  • Please do it in c++ please show steps as much as possible to help me understand...

    Please do it in c++ please show steps as much as possible to help me understand the code thank you Credit Card error detection check -> Luhn Algorithm, (http://www.freeformatter.com/credit-card-number-generator-validator.html#cardFormats) From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1...

  • Write java program to check that a (16-digit) credit card number is valid. A valid credit...

    Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you: Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10. For example, consider the number 4012 8888 8888 1881. The...

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

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

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

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

  • coding luhnDouble equations in haskell here is the question: DEFINING FUNCTIONS: Assignment Description: The Luhn algorithm...

    coding luhnDouble equations in haskell here is the question: DEFINING FUNCTIONS: Assignment Description: The Luhn algorithm is used to check bank card numbers for simple errors such as mistyping a digit, and proceeds as follows: consider each digit as a separate number; moving left, double every other number from the second last; subtract 9 from each number that is now greater than 9; add all the resulting numbers together; if the total is divisible by 10, the card number is...

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

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

  • Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

    Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type of credit card he/she would like to find the checksum for. 2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.] 3. Using the user's input of the n digits, finds the last digit of...

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