|
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
/** Return how many occurrences of `target` occur in `s`. * @param s the string to search through * @param target the char to count. * @return how many occurrences of `target` occur in `s`. */ int countOccurences( String s, char target ) { int seenSoFar = 0; int i=0; while (i < s.length()) { if (s.charAt(i) == target) { seenSoFar = seenSoFar + 1; } // else do nothing. i = i + 1; } return seenSoFar; } |
Ten-digit book numbers
— ISBN-10s
—
have the following “checksum” property:
the digits
x1x2x3…x9x10
have the property that
(1·x1 + 2·x2 + 3·x3 + … + 9·x9 + 10·x10) % 11 = 0.
This scheme means1that if somebody mis-types a digit, or transposes two digits,
a computer can recognize that an error has been made.
For example, for the ten digits "0306406152",
( | 1·0 | + | 2·3 | + | 3·0 | + | 4·6 | + | 5·4 | + | 6·0 | + | 7·6 | + | 8·1 | + | 9·5 | + | 10·2 | ) | % 11 | |
= | ( | 0 | + | 6 | + | 0 | + | 24 | + | 20 | + | 0 | + | 42 | + | 8 | + | 45 | + | 20 | ) | % 11 |
= | 165 % 11 | |||||||||||||||||||||
= | 0 (since 11 goes into 165 fifteen times with remainder 0) |
Test case for you to try by hand,
to understand what our code will have to do:
Which of these is a valid ISBN-10?
By the way, mathemeticians have a concise way of writing this sum:
10 | ||||
( | Σ | i·xi | ) | mod 11 = 0 |
i=1 |
class ISBNChecker { boolean isValidISBN10( String isbn ) { return false; // stub. } } |
int charToInt( char digit ) { return Integer.parseInt( Character.toString(digit) ); } |
class ISBNCheckerTester { public static void main( String[] args ) { ISBNChecker looper = new ISBNChecker(); System.out.println( "\nTest some okay (but contrived) ISBNs: " ); checkExpect( looper.isValidISBN10("0000000000"), true ); checkExpect( looper.isValidISBN10("1000000001"), true ); checkExpect( looper.isValidISBN10("0100000002"), true ); checkExpect( looper.isValidISBN10("0010000003"), true ); checkExpect( looper.isValidISBN10("0001000004"), true ); checkExpect( looper.isValidISBN10("0000000019"), true ); checkExpect( looper.isValidISBN10("0000000027"), true ); checkExpect( looper.isValidISBN10("2000000010"), true ); checkExpect( looper.isValidISBN10("4000000020"), true ); checkExpect( looper.isValidISBN10("5000000080"), true ); System.out.println( "\nTest some real and incorrect ISBNs: " ); checkExpect( looper.isValidISBN10("0306406152"), true ); checkExpect( looper.isValidISBN10("0306406162"), false ); checkExpect( looper.isValidISBN10("0306406154"), false ); checkExpect( looper.isValidISBN10("0553565699"), true ); checkExpect( looper.isValidISBN10("1553565699"), false ); } static void checkExpect( boolean actual, boolean expected ) { if (actual == expected) { System.out.print("."); } else { System.out.println("failed test: expected " + expected + ", got " + actual ); } } } |
Extra challenge: Actually, sometimes the check-digit works out to be 10. Since that won't fit into a single digit, in that case the letter 'X' is used (reminiscent of the roman numeral).
Fix your code above so that ISBN-10s ending in the letter 'X' are also recognized. (Hint: put your change inside charToInt.)
System.out.println( "\nTest some X-digit ISBNs: " ); checkExpect( looper.isValidISBN10("000000000X"), false ); checkExpect( looper.isValidISBN10("050000000X"), true ); checkExpect( looper.isValidISBN10("000020000X"), true ); checkExpect( looper.isValidISBN10("300000002X"), true ); |
1 In ITEC122, you'll learn why this is true. ↩
2 You might wonder why this formula works; from what we saw above, shouldn't x10 be the negative of that sum, so that ten times it will have the last digit cancel out the first nine? Yes! But in mod 11 (where we can subract a multiple of 11 without changing anything), ten times a number is equivalent to negative-one times that number. More on modular arithmetic in ITEC122. ↩
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
©2009, Ian Barland, Radford University Last modified 2009.Apr.07 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |