/*********************************************** Libby is the librarian at OHS. She must type ISBN book numbers into her database, and she occasionally makes mistakes. She asked programmer Pete if he could write a program to check the ISBN numbers and reject obvious mistakes, like too few digits or a mismatched check digit. Pete read the following web-page http://isbn-information.com/10-digit-isbn.html and then wrote the following test program. It expects the user to type 10 digits (the last might be X), without any dashes or spaces. This is only a first attempt - it won't really solve Libby's problem, as this needs to be incorporated into Libby's database program. ************************************************/ void setup() { String code = input("Type an ISBN number as 10 digits - no dashes or spaces"); code = code.toUpperCase(); System.out.println(code); if(code.length()!=10) // check length = 10 { System.out.println("Not valid - need exactly 10 characters"); System.exit(0); } int sum = 0; for(int p = 0; p < 10; p = p+1) // count through characters { char c = code.charAt(p); if( c < '0' || (c > '9' && c != 'X') ) // check if not digit or X { System.out.println("Invalid character : " + c); System.exit(0); } int val; // value of the character if(c == 'X') { val = 10; } else { val = (int)c - 48;} // convert ASCII code to value sum = sum + val * (10-p); } if(sum % 11 != 0) // check-sum must be divisible by 11 { System.out.println("Check-sum is not divisible by 11, so incorrect"); System.exit(0); } System.out.println("Valid"); } public String input(String prompt) { return javax.swing.JOptionPane.showInputDialog(null,prompt); } public void output(String message) { javax.swing.JOptionPane.showMessageDialog(null,message); } |
Java can input and store Strings,
which are sequences of keyboard characters - letters and numbers and
punctuation. Each character in the String is stored in two
Bytes as a number. That's right, the letters are not
actually stored in the memory, but rather a number
is stored that represents the letter (or digit or punctuation mark).
A Little History
Around 1960-1965, there were not very many computers in the world.
But some companies did have computers and they wanted to exchange data
with other companies or other offices in the same company. That was
relatively simple with numbers that were stored in binary (base
two). However, text data was stored in a variety of different ways,
with no standard. IBM created a system called EBCDIC (Extended
Binary Coded Decimal Interchange Code), but this only worked on the
hardware (card punch machines and printers) that IBM built. Around
the same time, a group of companies formed a committee of engineers to
standardize the numeric codes used to represent text data. The
result was 7-bit ASCII - the
American Standard Code for Information Interchange.
For example : 'A' = 65 'B' = 66 '*' =
42 'd' = 100
The 7-bit code could store 128 different numbers, representing 128
different characters. That was enough for all English letters,
digits and punctuation, plus extra codes for control signals like #7 to
ring the bell on a teletype machine (very important in The
Andromeda Strain!)
After the President of the United States mandated in 1968 that every
computer used by the US government must support the ASCII standard, this
became a de facto standard for all computer systems and it is still
in use. You might wish to read
more about ASCII here.
In subsequent years various small changes and additions were made to
ASCII, and ASCII became an 8-bit system. That was very convenient as 1
character occupied 1 Byte, and allowed 256 characters to be defined,
including a few extra characters for German, French, Spanish and a couple
other languages, plus special symbols for mathematics (everyone's favorite
subject!) Around 1990, the need for more international
characters for other languages prompted the expansion to 16-bit Unicode. This 2 Byte
code supports 65536 different characters, which is enough for virtually
every language on the planet.
Characters and Codes
Java uses 16-bit
UniCode (actually UTF-16,
that's almost the same) to store
each character. If you need
to change ASCII codes (or Unicodes) to characters, or vice versa, use the
following commands:
char C = 'ã';
int A = (int)C;
println(A);
==> 227
int A = 88;
char C = (char)A;
println(C);
==> X
Check-Sums
A check-sum for a String is calculated by converting the characters to int codes, then doing a calculation
with those codes to produce a result. Now the check-sum should be
transmitted along with the String. Then the String can be checked
against the check-sum. If it does not match correctly, we know the
String has been corrupted during transmission. Then the receiver
should ask the sender to "retransmit".
The check-sum for an ISBN code is calculated as follows:
ISBN Calculation
The program above checks whether an ISBN code is valid by calculating the
check-sum. The for loop goes through each character, converts it to the
corresponding code, multiplies by 10, then 9, ... and adds up all
the results. After the loop finishes, if checks divisibility by 11:
The % sign calculates : sum mod 11
. That is the remainder
when sum is divided by 11. For example:
79 % 11 = 79 mod 11 ==>
79/11 = 7 remainder 2 ==> result is 2
If the result of the mod operation
is 0 (zero), then the number is evenly divisible by 11.
Practice Questions
Programming Practice