A Java program has been started for you. The goal of the program is to prompt the user to enter a credit card number and then display whether the card is valid/invalid. Several method headers have already been defined in the code. Your task is to complete the body of each of these methods (6 in total) so that the program accomplishes the stated goal above. Below I have started the code for you as practice and I even included hints as comments to help you out. import java.util.Scanner; public class CreditCard {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a credit card number: ");long number = input.nextLong(); if (isValid(number))System.out.println("valid");elseSystem.out.println("invalid");} /** Return true if the card number is validWhat makes a card number valid?1. 13-16 digits2. Prefix of 4, 5, 6, or 373. Result of Luhn check must be divisible by 10*/public static boolean isValid(long number) {// Enter your code here} /** Return sum of doubled even place digits in number */public static int sumOfDoubleEvenPlace(long number) {// Enter your code here} /** Return the input number if it is a single digit,otherwise, return the sum of the two digits For example, getDigit(4) = 4getDigit(18) = 9*/public static int getDigit(int number) {// Enter your code here} /** Return sum of odd place digits in number */public static int sumOfOddPlace(long number) {// Enter your code here} /** Return true if d is a prefix for number For example, prefixMatched(3784, 37) is trueprefixMatched(12345, 4) is false*/public static boolean prefixMatched(long number, int d) {return getPrefix(number, getSize(d)) == d;} /** Return the number of digits in d */public static int getSize(long d) {// Enter your code here} /** Return the first k number of digits from number. If the numberof digits in number is less than k, return number. */public static long getPrefix(long number, int k) {// Enter your code here}}
A Java
Below I have started the code for you as practice and I even included hints as comments to help you out.
import java.util.Scanner;
public class CreditCard {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a credit card number: ");
long number = input.nextLong();
if (isValid(number))
System.out.println("valid");
else
System.out.println("invalid");
}
/** Return true if the card number is valid
What makes a card number valid?
1. 13-16 digits
2. Prefix of 4, 5, 6, or 37
3. Result of Luhn check must be divisible by 10*/
public static boolean isValid(long number) {
// Enter your code here
}
/** Return sum of doubled even place digits in number */
public static int sumOfDoubleEvenPlace(long number) {
// Enter your code here
}
/** Return the input number if it is a single digit,
otherwise, return the sum of the two digits
For example, getDigit(4) = 4
getDigit(18) = 9
*/
public static int getDigit(int number) {
// Enter your code here
}
/** Return sum of odd place digits in number */
public static int sumOfOddPlace(long number) {
// Enter your code here
}
/** Return true if d is a prefix for number
For example, prefixMatched(3784, 37) is true
prefixMatched(12345, 4) is false
*/
public static boolean prefixMatched(long number, int d) {
return getPrefix(number, getSize(d)) == d;
}
/** Return the number of digits in d */
public static int getSize(long d) {
// Enter your code here
}
/** 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) {
// Enter your code here
}
}
Trending now
This is a popular solution!
Step by step
Solved in 7 steps with 7 images