# ICT131 Java Assignment

## Assignment Question 2 [ 25 marks ]

The objectives of this question are

• to practice the use of selection structure in simple Java program
• to practice the use of repetition structure in a Java program
• to practice the use of method in a Java program
• to manipulate string

The last digit of an identification number is a computer check digit which is used to detect errors arising when the number is transcribed manually.

The algorithm for computing the check digit for the identification number for an institution is as follows:

The identification number has a prefix of either ‘U’ or ‘A’, each has 7 digits from ‘0’ to ‘9’ and a check digit at the end. E.g. U0906931E, A0012345W.

For numbers with ‘U’ prefix, follow the algorithm below:

Step 1: Discard the third digit after the letter prefix thus giving you 6 digits.

E.g. U0906931 will be 096931

Step 2: Multiply each digit with the weight of 0, 1, 3, 1, 2, and 7 respectively and sum up all the products.

E.g. 0 x 0 + 9 x 1 + 6 x 3 + 9 x 1 + 3 x 2 + 1 x 7 = 49

Step 3: Get the remainder from dividing the sum from step 2 by 13.

E.g. remainder from 49 divide by 13 is 10

Step 4: Obtain the check digit from table 2 that corresponds to the remainder from step 3.

E.g. the check digit for U0906931 is ‘E’ as the remainder from step 3 is 10 For numbers with ‘A’ prefix, follow the algorithm below:

Step 1: Add the values of all the 7 digits.

E.g. A0012345 will be 0 + 0 + 1 + 2 + 3 + 4 + 5 = 15

Step 2: Get the remainder from dividing the sum from step 1 by 13.

E.g. remainder from 15 divide by 13 is 2

Step 3: Obtain the check digit from table 2 that corresponds to the remainder from step 2.

E.g. the check digit for A0012345 is ‘W’ as the remainder from step 2 is 2

 Remainder 0 1 2 3 4 5 6 7 8 9 10 11 12 Check Digit Y X W U R N M L J H E A B

Table 2

• Write a Java program that prompts and gets the complete identification number from the user (including the check digit). Finds the check digit and displays a message to indicate whether the check digit entered is correct. If it is incorrect, display the correct one. You are not required to perform data validation here as it is to be done in part (b). Your program output must show clearly the input entered, the sum and the remainder found as well as the final result.

Submit your program listing together with screenshot showing two runs of your program output. Your screenshot must show your name and student identification number as part of the program output.

(15 marks)

• Modify your program in part (a) to validate the input. Display an error message to indicate the type of error the input value has and repeat the input process until a valid input is entered. You should define a method to validate the input data.

Submit your program listing with the modification clearly highlighted. Submit also the screenshot of your program output that convince your tutor that your program is working. Your screenshot must show your name and student identification number as part of the program output.

Note: you should think carefully what is the possible type of errors for the input.

(10 marks)