Write a program ISBNChecksum.java that takes a 9-digit int as a command-line argument and displays the checksum digit according to the following rules: (IN JAVA PLEASE)

Multiply the first digit by 10, the second by 9, the third by 8, and so on, down to the ninth digit by 2.
Add the values together.
Divide the sum by 11. What is the remainder?
If the remainder is 0, the checksum is 0; otherwise, subtract the remainder from 11 to get the checksum digit.
If the checksum that you come up with is 10, then X is used as the checksum digit!

As an example, the checksum digit corresponding to 020131452 is 5 since 5 is the only value of x between 0 and 10 for which10*0 + 9*2 + 8*0 + 7*1 + 6*3 + 5*1 + 4*4 + 3*5 + 2*2 + 1*x is a multiple of 11.

Hint 1: to extract the rightmost digit of a number use the modulus operator.
Hint 2: to remove the rightmost digit of a number use the integer division by 10
Hint 3: for full credit use a loop to compute the sum

Assume the input value used to test your program is a 9-digit positive integer. Also assume that the input is a valid ISBN number without the checksum digit.

Respuesta :

Answer:

Here is the JAVA program:

import java.util.Scanner; // gets input from user

public class ISBNChecksum

{

      public static void main(String[] args) { //start of the main() function body

      Scanner s = new Scanner(System.in); // creates Scanner class object

      System.out.print("Enter a 9 digit integer:");

//prompts the user to enter a 9 digit integer

      int num = s.nextInt(); // takes input integer values digits from the user

       int a[] = new int[num]; //stores the value of integer into array a

       int sum = 0; // for adding the values

       for (int i = 2; i <= 10; i++) // loop starts from 2 and ends when i>10

       {

           int digit = num % 10; // extract right most digit      

/* Multiply the first digit of input integer number by 10, the second by 9, the third by 8, so on, down to the ninth digit by 2 and add the values together */    

           sum = sum + i * digit;

           num = num / 10; // remove the rightmost digit

       }

       System.out.print("The checksum digit is " + num);

       int remainder = sum % 11; //computes the remainder

       if(remainder == 1) //if remainder value is equal to 1

       System.out.println("X"); // used as a checksum digit

       else if (remainder == 0) // if remainder is 0

       System.out.println("checksum is 0"); //checksum is 0

       else //subtract the remainder from 11 to get the checksum digit.

       System.out.println(11 - (remainder));}  

}              

Explanation:

The for loop basically computes the sum of each digit of the input number from right to left. First the right most digit is extracted using modulus operator and stores it into digit variable i.e. digit = num % 10;

Then the digit is multiplied by the value of i and added to the value in sum. The weighted sum of each digit is calculated this way. From right to left means that the first right most digit is multiplied by i = 2, the second rightmost digit is multiplied by 3 (i is incremented at every iteration) and so on and the loop terminates as the value of i gets greater of equal to 10. Then the sum is calculated for each result of product so the complete statement is sum = sum + i * digit; To remove the rightmost digit of a number at each iteration num = num / 10; is used which divides the number by 10. For example if the number is 1234 then 1234/10 gives 4 which is the rightmost digit.

Next the remainder of the computed sum is calculated by dividing the value stored in sum by 11 in order to find the checksum. If the remainder is 0, the message "checksum sum is 0" is displayed. If not then the remainder value is subtracted from 11 to compute the checksum. If the checksum computed is 10 then X is used as checksum digit.

ACCESS MORE
EDU ACCESS
Universidad de Mexico