COMS S1004

Homework #3

Due 5:30pm Mon Jun 15


Do the following questions from Schneider & Gersting Third Edition:
Chapter 4: 1, 8, 11, 15, 16


Credit card companies use built-in "checksums" as added security measures when creating the account numbers on credit cards. This means that there are only certain valid credit card numbers, and validity can instantly be detected by using an algorithm that may involve adding up parts of the numbers or performing other checks.

In this assignment, you will implement a security algorithm on a credit card number, and your program will indicate whether or not the card number is valid, according to the security check. Note that this algorithm is purely made-up; don't try to use it to create fake credit card numbers! :-)

First, implement a class called CreditCard. This class will have attributes "name", "cardNumber", and "expirationDate"; all three are Strings. Include accessors and constructors as necessary. The credit card number must be represented the format ####-####-####, including the dashes. The expiration date must be in the format "MM/YY". There is also to be a method called "isExpired" which takes a String in "MM/YY" format to represent the current date) and returns true if the card has expired (i.e., if the expiration date of the card is before the date specified as the argument).

Then, implement a class called CreditCardVerifier. It should contain a static method called "verifyCreditCard" that takes a CreditCard as a parameter and then returns true if the credit card number and expiration date conform to the following rules:

  1. The first digit must be a 4.
  2. The fourth digit must be one greater than the fifth digit.
  3. The product of the first, fifth, and ninth digits must be 24.
  4. The sum of all digits must be evenly divisible by 4.
  5. The sum of the four digits in the expiration date must be less than the product of the last two digits of the card number.
  6. The sum of the first four digits must be one less than the sum of the last four digits.
  7. If you treat the first two digits as a two-digit number, and the seventh and eight digits as a two digit number, their sum must be 100.

Hint: valid card numbers according to this set of rules include 4807-6052-1766 and 4094-3460-2754, both with expiration dates of 11/10. You should use these for testing out your program.

To do this, you will need to use various methods of the String class, described in Big Java section 4.6 and in the Java online documentation for String.

Last, create a class called CreditCardTest that prompts the user to enter a credit card number and expiration date, creates a CreditCard object, and then uses the verifyCreditCard method to report whether or not the card number is valid. It should NOT explain why in plain English. However, for testing purposes you can report an "error code" that corresponds to the numbers of the validity checks above (this will affect how you implement the return value of the "verifyCreditCard" method).

In your program, you can assume that the credit card number and expiration date are correctly entered by the user in the form ####-####-####, including the dashes, and "MM/YY", including the slash, respectively. However, if your "verifyCreditCard" method checks for correct formatting and returns an error if the card number or expiration date is not correctly formatted, you will receive 5 points extra credit (note: TAs will not be able to help you on the extra credit portions of our homework assignments, and no partial credit will be given).

Want more extra credit? For 10 points extra credit, create a class called CreditCardHacker that has a "main" method that randomly generates 10,000,000 credit card numbers and reports which ones pass the test in the CreditCardTest's verifyCreditCard method. To "cheat" a little, you can set the first digit to 4 and use 11/10 as the expiration date (otherwise it's somewhat unlikely you will find any valid card numbers).

Please be sure to include a "README" file with your submission. For this assignment, it should be about two or three paragraphs long. You should explain how your program works, how to run it, what the output means, and how you performed all of the checks.


The Theory assignment will be worth a total of 40 points. Questions 1 and 16 are worth five points each; the others are worth 10.

The Programming assignment will be worth a total of 60 points, distributed as follows:

  • Your program must compile with no errors (5 points)
  • Your program must implement the three classes and the corresponding methods described above (5 points)
  • Your program must correctly determine whether the credit card number is valid (45 points; 5 points each for the first five security checks, 10 points each for the last two)
  • Your program should include a README file, be well-commented, use good variable names, be easy to read, etc. (5 points)

  • Submitting your assignment

    For the Theory assignment, print a paper copy and submit it at the beginning of class. You do not need to submit it electronically.

    For the Programming assignment, submit a paper copy of all your source code and submit it electronically by the due date. Put all your .java files and your README file in a .zip or .tar file, then follow these instructions:

    1. Make sure the zip/tar file name clearly indicates your UNI and the name of the assignment, e.g.
    2. Log onto CourseWorks and go to the page for COMS S1004
    3. Click on CLASS FILES in the left hand side menu
    4. Click on POST FILE in the top menu, and fill the form to submit your zip/tar file
    5. Make sure you choose Homework #3 (under "Shared Files") as the "Post file To" destination (the contents of this folder should only be visible to the teaching staff)
    6. Click the "Submit" button
    7. All done!