//Java applet programmed by ... on the 11 June 2002, At ... //This applet is to check whether an isbn number is valid. import java.awt.*; import java.applet.*; import java.util.*; public class isbn extends Applet { public String ValidationVerdict; public String HelpfulComment; public void init() { //Initial Displays ValidationVerdict = "Plase type in the ISBN number you wish to check in the box below."; } public void paint(Graphics g) { //Output the string to the screen g.drawString(ValidationVerdict,10,10); g.drawString(HelpfulComment,10,20); } public void CheckIsbn(String ISBN_Number) { //This counter counts through the string backward. int digit_number; //This counter stores the weight value of the string number int weight_of_digit; //th stores the integer value of the digit to be analysed int digit_value; //Total is where the total of all weights of digits end up int Total = 0; //Remainder is to determine whether the ISBN is correct remainder = Total mod 11 int Remainder = 0; //First digit is where the first numeric value of the ISBN number will be stored. int first_digit = 0; //Initialise weight_of_digit weight_of_digit=1; //Step through the string backwards. for(digit_number = (ISBN_Number.length()-1);digit_number >=0;digit_number--) { //Make sure the current character isn't a -, these are separaters, dont pay attention to them. if(ISBN_Number.charAt(digit_number)!='-') { //is there a Roman character X in the ISBN if((ISBN_Number.charAt(digit_number) == 'X') || (ISBN_Number.charAt(digit_number) == 'x')) //X = 10 digit_value = 10; else //Get the integer value of the character. digit_value = (int)ISBN_Number.charAt(digit_number) - 48; //make sure we store the first numeric value. if(weight_of_digit == 1) first_digit = digit_value; //Add digit_value*weight_of_digit (number * wieght) to the cumulative total. Total += digit_value * weight_of_digit; //increase the wieght. weight_of_digit++; } } //find out whether the ISBN is corrent. Remainder = Total % 11; //Display outputs. if(Remainder == 0) { ValidationVerdict = "Yes, This is a valid ISBN Number."; HelpfulComment = ""; } else { ValidationVerdict = "No, This is an invalid ISBN number."; if((first_digit - Remainder) > 0) HelpfulComment = "The correct ISBN number would end with the digit " + (first_digit - Remainder); if((first_digit - Remainder) < 0) { int i=0; for (i = (first_digit*1+1); i < 11; i++) { Remainder = (Total - first_digit + i) % 11; if (Remainder == 0) break; } HelpfulComment = "The correct ISBN number would end with the digit " + i; } } repaint(); } }