ACS-1903 Lab 3 Fall 2009 Check Digits Techniques: Scanner, charAt(), modulo arithmetic, if-else statement Hand in all pages to your lab demonstrator Student Identification Student number ____________________________________________ Last name ____________________________________________ First name ____________________________________________ Workstation id ____________________________________________ PASS id ____________________________________________ Calculating check digit for ISBN-10 numbers The final character of a ten digit International Standard Book Number is a check digit computed so that multiplying each digit by its position in the number (counting from the right) and taking the sum of these products modulo 11 is 0. The last digit (which is multiplied by 1) is the check digit, chosen to make the sum correct. For instance, your text Starting Out with Java: From Control Structures through Data Structures has the ISBN-10 number: 0321421027. Consider 0*10 + 3*9 + 2*8 + 1*7 + 4*6 + 2*5 + 1*4 + 0*3 + 2*2 + 7*1 = 99 and since 99 modulo 11 is 0 the above is a valid ISBN-10 number. Note the 10th digit can be an “X” which represents 10. For example the book Learning to program with Alice, 2nd edition, has the 10 digit ISBN of 013208516X and 0*10 + 1*9 + 3*8 + 2*7 + 0*6 + 8*5 + 5*4 + 1*3 + 6*2 + 10*1 = 132 and 132 mod 11 = 0 … i.e. it’s a valid ISBN. Note that a check digit for an ISBN-10 is generated from the first 9 digits in such a way to ensure the weighted sum modulo 11 is equal to zero. This is why some ISBNs have a last digit of “X”. 1 ACS-1903 Lab 3 Fall 2009 1. For each of the following show the exact line of output produced System.out.println(0*10+3*9+2*8+1*7+4*6+2*5+1*4+0*3+2*2+7*1); System.out.println(99/11); System.out.println(99%11); System.out.println(99-99/11); System.out.println(122 % 11); System.out.println(013208516 % 10); System.out.println(013208516 - 013208516 % 100 ); System.out.println(013208516 - 013208516 % 10 ); System.out.println ( 99%11 == 0 ); System.out.println ( 122%11 == 1 ); 2 ACS-1903 Lab 3 Fall 2009 2. What output is produced by the following. // Note that charAt() is a method of the String class // that is used to obtain or extract a single character // out of a String. String isbn; // calculate the weighted sum of first 9 digits isbn = "013208516"; // the individual characters System.out.println((isbn.charAt(0))); System.out.println((isbn.charAt(1))); System.out.println((isbn.charAt(2))); System.out.println((isbn.charAt(3))); System.out.println((isbn.charAt(4))); System.out.println((isbn.charAt(5))); System.out.println((isbn.charAt(6))); System.out.println((isbn.charAt(7))); System.out.println((isbn.charAt(8))); // the values of the characters as digits System.out.println((isbn.charAt(0)-48)); System.out.println((isbn.charAt(1)-48)); System.out.println((isbn.charAt(2)-48)); System.out.println((isbn.charAt(3)-48)); System.out.println((isbn.charAt(4)-48)); System.out.println((isbn.charAt(5)-48)); System.out.println((isbn.charAt(6)-48)); System.out.println((isbn.charAt(7)-48)); System.out.println((isbn.charAt(8)-48)); // calculate the weighted sum of the first 9 digits int sum = (isbn.charAt(0)-48)*10+ (isbn.charAt(1)-48)*9+ (isbn.charAt(2)-48)*8+ (isbn.charAt(3)-48)*7+ (isbn.charAt(4)-48)*6+ (isbn.charAt(5)-48)*5+ (isbn.charAt(6)-48)*4+ (isbn.charAt(7)-48)*3+ (isbn.charAt(8)-48)*2; System.out.println(sum); 3 ACS-1903 Lab 3 Fall 2009 3. Calculating a check digit Write a program named Generate.java that accepts the first 9 digits from the end user displays the first 9 digits of the ISBN-10 calculates and displays the weighted sum determines and displays the check digit for the ISBN-10 Show a working program to your lab demonstrator; run it for 032142102 (check digit is 7) and for 013208516 (check digit is X). Use the following code to get the first 9 digits of the ISBN from the user // ask user for the first 9 digits of an ISBN-10 String isbn; Scanner kb = new Scanner(System.in); System.out.println("enter the first 9 digits of the isbn:"); isbn=kb.next(); // calculate the weighted sum … // Calculate and display the check digit using an if-else ... Submit this program to PASS as a first submission for Assignment 3. 4. Get Assignment 3 Log on to PASS https://venus.acs.uwinnipeg.ca Navigate to 1903 and view assignment 3. 4