All pastes #736434 Raw Edit

CheckDigit.java

public java v1 · immutable
·published 2007-10-14 15:12 UTC
rendered paste body
/**  * Calculate the check digit of HKID * - Zhang Haoran, 20070912 * */import java.util.Scanner;public class CheckDigit {  // Define aliases for ASCII character code  private static final int ASC_0=48, ASC_A=65;  // Return the checkDigit produced from hkid  // hkid is a string that looks like A123456  private static char check( String hkid ){    // Step 1: Map the leading alphabet to number: A=1, B=2, ... Z=26    // e.g. A is 65 in ASCII, so 65-65+1 gives 1    int s=((int)hkid.charAt(0)-ASC_A+1)*8;    // Step 2: Add the other digits like A*8 + 1*7 + 2*6 + ... + 6*2    for(int i=1; i<7; i++){ s += ((int)hkid.charAt(i)-ASC_0) * (8-i); }    // Step 3: s = 11 - (s mod 11)    s = 11-s%11; // ans can be 1, 2, 3, ..., 10, 11.    // Step 4: If s is 10, return A. If 11, return 0. Else return s.    s = s%11; return (s==10)?'A':(char)(s+ASC_0);  }  // Main method begins execution of Java application  public static void main( String [] args )  {    System.out.print( "Enter the first 7 digits of your HKID (e.g. A123456): " );    Scanner input = new Scanner( System.in );    String hkid = input.nextLine(); // read in a whole line, assuming correct format    System.out.printf( "Your ID number is %s(%c).\n", hkid, CheckDigit.check(hkid));  } //end method main} // end class ID