Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 6, Problem 6.31PE

(Financial: credit card number validation) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with

  • for Visa cards
  • for Master cards
  • for American Express cards
  • 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 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):

  1. 1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.

Chapter 6, Problem 6.31PE, (Financial: credit card number validation) Credit card numbers follow certain patterns. A credit , example  1

  1. 2. Now add all single-digit numbers from Step 1.

    4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

  2. 3. Add all digits in the odd places from right to left in the card number.

    6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38

  3. 4. Sum the results from Step 2 and Step 3.

    37+38=75

  4. 5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid.

Write a program that prompts the user to enter a credit card number as a 1ong integer. Display whether the number is valid or invalid. Design your program to use the following methods:

/** Return true if the card number is valid */

public static boolean isValid(long number)

/** Get the result from Step 2 */

public static int sumOfDoubleEvenPlace(long number)

/** Return this number if it is a single digit, otherwise, • return the sum of the two digits */

public static int getDigit(int number)

/** Return sum of odd-place digits in number */

public static int sumOfOddPlace(long number)

/** Return true if the number dis a prefix for number *I

public static boolean prefixMatched(long number, int d)

/** Return the number of digits in d */

public static int getSize(long d)

/ ** 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)

Here are sample runs of the program: (You may also implement this program by reading the input as a string and processing the string to validate the credit card.)

Chapter 6, Problem 6.31PE, (Financial: credit card number validation) Credit card numbers follow certain patterns. A credit , example  2

Blurred answer
Students have asked these similar questions
Find the error in the following code
What is the output from each of the following pseudocode statements?
What does the following code mean?: if r[i+1]

Chapter 6 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)

Ch. 6.5 - Prob. 6.5.1CPCh. 6.5 - Identify and correct the errors in the following...Ch. 6.5 - Prob. 6.5.3CPCh. 6.5 - Prob. 6.5.4CPCh. 6.6 - Prob. 6.6.1CPCh. 6.6 - Prob. 6.6.2CPCh. 6.7 - What is hexCharToDecimal ( B)) ? What is...Ch. 6.8 - What is method overloading? Is it permissible to...Ch. 6.8 - What is wrong in the following program? public...Ch. 6.8 - Given two method definitions, public static double...Ch. 6.9 - Prob. 6.9.1CPCh. 6.9 - What is the scope of a local variable?Ch. 6 - (Math: pentagonal numbers) A pentagonal number is...Ch. 6 - (Sum the digits in an integer) Write a method that...Ch. 6 - (Palindrome integer) Write the methods with the...Ch. 6 - (Display an integer reversed) Write a method with...Ch. 6 - (Sort three numbers) Write a method with the...Ch. 6 - (Display patterns) Write a method to display a...Ch. 6 - (Financial application: compute the future...Ch. 6 - (Conversions between Celsius and Fahrenheit) Write...Ch. 6 - Prob. 6.9PECh. 6 - (Use the isPrime Method) Listing 6.7,...Ch. 6 - (Financial application: compute commissions) Write...Ch. 6 - (Display characters) Write a method that prints...Ch. 6 - (Sum series) Write a method to compute the...Ch. 6 - (Estimate ) can be computed using the following...Ch. 6 - (Financial application: print a tax table) Listing...Ch. 6 - Prob. 6.16PECh. 6 - Sections 6.10 and 6.11 6.17 (Display matrix of 0s...Ch. 6 - (Check password) Some Websites impose certain...Ch. 6 - (Triangles) Implement the following two methods: /...Ch. 6 - (Count the letters in a string) Write a method...Ch. 6 - (Phone keypads) The international standard...Ch. 6 - (Math: approximate the square root) There are...Ch. 6 - (Occurrences of a specified character) Write a...Ch. 6 - (Display current date and time) Listing 2.7,...Ch. 6 - Prob. 6.25PECh. 6 - (Palindromic prime) A palindromic prime is a prime...Ch. 6 - (Emirp) An emirp (prime spelled backward) is a...Ch. 6 - (Mersenne prime) A prime number is called a...Ch. 6 - (Twin primes) Twin primes are a pair of prime...Ch. 6 - (Game: craps) Craps is a popular dice game played...Ch. 6 - (Financial: credit card number validation) Credit...Ch. 6 - (Game: chance of winning at craps) Revise...Ch. 6 - (Current date and time) Invoking System....Ch. 6 - (Print calendar) Programming Exercise 3.21 uses...Ch. 6 - (Geometry: area of a pentagon) The area of a...Ch. 6 - (Geometry: area of a regular polygon) A regular...Ch. 6 - (Format an integer) Write a method with the...Ch. 6 - (Generate random characters) Use the methods in...Ch. 6 - (Geomentry: point position) Programming Exercise...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Identity the body of the following loop structure and count the number of times it will be executed. What happe...

Computer Science: An Overview (13th Edition) (What's New in Computer Science)

List the five major components of a computer system.

Starting Out with Programming Logic and Design (4th Edition)

T F Arguments are passed to the base class constructor by the derived class constructor.

Starting Out with C++ from Control Structures to Objects (8th Edition)

What is the difference between main memory and secondary storage?

Starting Out With Visual Basic (7th Edition)

When the value of an item is dependent on other data, and that item is not updated when the other data is chang...

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
What Are Data Types?; Author: Jabrils;https://www.youtube.com/watch?v=A37-3lflh8I;License: Standard YouTube License, CC-BY
Data Types; Author: CS50;https://www.youtube.com/watch?v=Fc9htmvVZ9U;License: Standard Youtube License