Sei sulla pagina 1di 3

Fatih University Department of Computer Engineering Ceng 101: Introduction to Programming Fall 2012

Homework 2 Due 20.12.2012 Subject: Methods Lecturer: Migena Ceyhan Advisor: Harun Reit Zafer, hrzafer@fatih.edu.tr Problem Description: Credit card numbers follow certain patterns. A credit card number must be between 13 and 16 digits. Major credit cards starts with the following numbers: 4 for Visa cards 5 for Master cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm (called as the Luhn check or the Mod 10 check) to be used as a validity criterion for a given set of numbers. Almost all credit card numbers are generated following this validity criterion. It goes without saying that the Luhn check is also used to verify a given existing card number. If a credit card number does not satisfy this check, it is not a valid number. For illustration, consider the card number 4388576018402626): 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.
2*2=4 2*2=4 4*2=8 1*2=2 6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4*2=8

2. Now add all single-digit numbers from Step 1. 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37 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 4. Sum the results from Step 2 and Step 3. 37 + 38 = 75 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. Main Implementation (25 pts.) In this project you are expected to write a Java program that validates and generates 16-digit credit card numbers. The main method of the program should be like this:

Fatih University Department of Computer Engineering Ceng 101: Introduction to Programming Fall 2012

public enum CardType { VISA, MASTER, AMERICAN_EXPRESS, DISCOVERY } public static void main(String[] args) { CardType type = CardType.VISA; String cardNumber = generateCardNumber(type); System.out.println("Generated " + type + " number " + cardNumber + " validity check: " + isValid(cardNumber)); type = CardType.MASTER; cardNumber = generateCardNumber(type); System.out.println("Generated " + type + " number " + cardNumber + " validity check: " + isValid(cardNumber)); }

And the output should look like this:

Generated VISA number 4851936596972692 validity check: true Generated MASTER number 5746910961957642 validity check: true

Note: To learn about enumerated types in Java Google for Java enum Method Implementation You must write and use following functions in your code: static boolean isValid(String cardNumber): returns whether a 16 digit card number is valid or not (10 pts) static String generateCardNumber(CardType cardType): generates a 16 character String that represents a random generated valid credit card number (10 pts) static int getCheckSum(String cardNumber): returns the check sum of card number (the result from Step-4) according to the Luhn method. (10 pts) static int getSumOfOddDigits(String number): returns the sum of the digits in the odd places from right to left in the card number. (returns the result from Step-3) (5 pts) static int getSumOfDoubledDigits(String number): returns the sum of the single digits shown in the Step-1 above. (10 pts) (returns the result from Step-2) static String getRandomDigits(int digitCount): returns a String that its length is equal to digitCount and includes random generated digits. For example getRandomDigits(4); returns a random String like 3529 (10 pts) static int getLastDigit(String firstFifteenDigits): given the first fifteen digit of a credit card number, returns the last digit that satisfies the Luhn check and makes the card number valid. (10 pts) Other Methods: Write and use any other method which is necessary (10 pts) Note: It is recommended to start coding the validator first.

Fatih University Department of Computer Engineering Ceng 101: Introduction to Programming Fall 2012

Testing You can test your validator with the following valid card numbers
American Express 378282246310005 3111111111111117 343434343434343 Visa 4111111111111111 4007000000027 4012888888881881 Master 5105105105105100 5111111111111118 5500000000000004 Discovery 6011111111111117 6011000000000004 6011601160116611

You can test your generator by using the following web site: http://www.brainjar.com/js/validation/default2.asp

Submission and Organization (-25) Save your source file as cNumber_Name.java. Not cName_Number.java! Its just not the same thing. o Ex: c20022365_harun_zafer.java Dont use Turkish characters or save your Java file in UTF-8 encoding. Submit just one Java file not two! All the codes will be tested and evaluated on Netbeans 7.2. Before submitting your code, also test it on Netbeans 7,2. Write clean code with reasonable variable names, small methods that doing just one thing and properly indented. Otherwise youll be penalized up to -15 points. Inappropriate submissions will be penalized by -10 points.

Kolay Gelsin Harun Reit Zafer

Potrebbero piacerti anche