Most books now published are assigned a code which uniquely identifies the book. The International Standard Book Number, or ISBN, is normally a sequence of 10 decimal digits, but in some cases, the capital letter X may also appear as the tenth digit. Hyphens are included at various places in the ISBN to make them easier to read, but have no other significance. The sample input and expected output shown below illustrate many valid, and a few invalid, forms for ISBNs.
Actually, only the first nine digits in an ISBN are used to identify a book. The tenth character serves as a check digit to verify that the preceding 9 digits are correctly formed. This check digit is selected so that the value computed as shown in the following algorithm is evenly divisible by 11. Since the check digit may sometimes need to be as large as 10 to guarantee divisibility by 11, a special symbol was selected by the ISBN designers to represent 10, and that is the role played by X.
The algorithm used to check an ISBN is relatively simple. Two sums, s1 and s2, are computed over the digits of the ISBN, with s2 being the sum of the partial sums in s1 after each digit of the ISBN is added to it. The ISBN is correct if the final value of s2 is evenly divisible by 11.
An example will clarify the procedure. Consider the (correct) ISBN
0-13-162959-X. First look at the calculation of s1:
digits in the ISBN | 0 | 1 | 3 | 1 | 6 | 2 | 9 | 5 | 9 | 10 (X) |
s1 (partial sums) | 0 | 1 | 4 | 5 | 11 | 13 | 22 | 27 | 36 | 46 |
The calculation of s2 is done by computing the total of the partial sums in
the calculation of s1:
s2 (partial sums of s1) | 0 | 1 | 5 | 10 | 21 | 34 | 56 | 83 | 119 | 165 |
You will prompt the user to enter an ISBN and return if it is valid or not:
Enter ISBN: 0-1315-2447-X 0-1315-2447-X is valid ISBNOR
Enter ISBN: 0-89237-010-9 0-89237-010-9 is invalid ISBN
For this program you need to read in the input as a string, then parse the string character by character and store the digits and the character X into an array. Remember the hypen ('-') character can occur anywhere in the string. There are several tests that you will have to perform to insure that you have a valid ISBN.
The file that you will be turning in will be called TestISBN.java. You will follow the standard Java coding convention that I have appended below. The file will have a header of the following form:
/* File: TestISBN.java Description: Student Name: Student UT EID: Course Name: CS 303E Unique Number: Date Created: Date Last Modified: */
You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code. For example:
Do this: if ( x > 5 ) { a = b + c; } Not this: if ( x > 5 ) { a = b + c; }
Use the turnin program to submit your TestISBN.java file. The TAs should receive your work by 5 PM on Saturday, 01 August 2009. You have until 5 PM Monday, 03 August 2009 to submit your work without late penalty. There will be substantial penalties if you do not adhere to the following guidelines.