home—info—lects—labs—exams—hws
D2Ltutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
Credit Card validation
hw03
Due Oct.02 (Wed) in class,
hardcopy and D2L.
You will submit two files:
CCard.java and TestCCard.java.
This homework is based
on this similar assignment
by S. Brenneman.
You will develop three and test three top-level methods which determine wither a given string is a valid credit-card number.
-
isValidVisa returns true iff it is passed a valid Visa card-number.
-
isValidVisa returns true iff it is passed a valid Visa card-number.
-
isValidVisa returns true iff it is passed a valid Visa card-number.
-
Helper methods as appropriate.
Details are in the provided file (see below).
Test for isValidVisa have been written for you; you will write the tests for the other methods.
In addition to
having one file for your functions
and a different file for your tests,
you will also write an interactive function,
CCard#main(String[]).
Running that function would print (depending on the exact user input):
Please enter a credit-card number, or the word "quit": 4111-1111-1111-1111
4111-1111-1111-1111 is a valid Visa card number.
Please enter a credit-card number, or the word "quit": hrmmm
hrmmm is NOT a valid credit-card number.
Please enter a credit-card number, or the word "quit": 6011-0000-0000-0004
4111-1111-1111-1111 is a valid Discover card number.
Please enter a credit-card number, or the word "quit": quit
Good bye.
You may use next (from Object120) to read in the next word of input.
To get " inside a string, you can "use this --> \" <-- character escape sequence.".
Note: This interactive version might only recognize credit-card numbers typed in without spaces;
that's fine (due to how
Object120#next() only returns the one next word, up to a space).
This function is the least points out of any on this page.
Download these files to start
To develop this program, you will write methods in a service class, called CardMethods, and you will write tests in a driver class called HW4Driver. These files have been created for you.
Download:
CCard.java,
TestCCard.java,
and
Object120.java.
In class CCard you will find method headers and instructions
for the methods you need to write. Do not change these method headers or the name of the class.
Your program will be graded using a test driver which will be testing these methods.
If you change any method signature or the classname,
the grading program will not be able to test your work.
In class TestCCard
you will find pass fail tests which also output the return value of the isValidVisa method.
This code is being given to you to give you an example of one way to write a test,
and make it easy for you to add more tests.
In a similar fashion, you will write tests for isValidAmEx and isValidDiscover.
Feel free to copy these tests,
but you will need to make adjustments to the input as well as the conditions for which the test passes or fails.
Both the above files extends Object120,
so you'll need to also include that file in your project.
Helper methods
There are two method headers for helper methods:
stripDashesAndSpaces (or “withoutDashesAndSpaces” if you prefer)
and allDigits.
You will call these methods from your isValid… methods.
You should test these methods as well.
To check if a character is more than the char '0'
but less than '9', you can use <=.
(Recall from class/reading: 'a' < 'z'.)
Do’s and Don’ts
You may (nay, should) use the methods
Object120#charAt(String,int)
and
Object120#length(String) for this assignment.
You should use while loop(s) as appropriate.
You may not use for-loops, arrays,
non-static methods,
class Pattern/regex (regular-expression) methods,
nor methods from class Character.
The purpose of this assignment is for you to write any methods you need.
Extra Credit
Actually, a 16-digit-long credit card number has only 15 digit's worth of information;
the last digit is a “check digit” -- chosen so that the sum of all the digits will be 0.
This allows us to detect if somebody mis-typed one digit of a credit-card number.
…Well, it's not quite that simple; a simple sum wouldn't catch
somebody who accidentally swapped two digits (“transposed” them); so actually
we'll double every other digit before summing them.
Read about the Luhn Algorithm,
and incorporate that into your checks.
(You'll wan't to add tests which fail. Note that all the provided Visa tests that pass
really do pass the Luhn Algorithm, too.)
Hint:
The code given on the Wikipedia page
is definitely pseudo-code, and requires changes to use Java and class Object120.
You can try translating it, or (even better) understand that code and write the steps
more clearly from knowing what they are doing.
Of course, as per class policy, you must understand the code you write.
You may re-type (not paste) code from other sources, but you must cite them.
And for this extra-credit portion, you will not get credit for an entirely-copied function.
Hint:
If you have a digit-character in '0'…'9',
you can subtract '0' from it
to get the corresponding int in 0…9.
home—info—lects—labs—exams—hws
D2Ltutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |