Answer:
// Scanner class is imported to allow program
// read input from user
import java.util.Scanner;
// Solution class is created
public class Solution {
// main method that begin program execution
public static void main(String[] strings) {
// Scanner object scan is created
// it receive input via the keyboard
Scanner scan = new Scanner(System.in);
// a prompt is displayed for user to enter 12 digits of ISBN
System.out.print("Enter the first 12 digits of an ISBN as string: ");
// user input is assigned to input
String input = scan.next();
//if length of input is not 12, program display error message and quit
if (input.length() != 12) {
System.out.println(input + " is Invalid input");
System.exit(0);
}
// 0 is assigned to checkSum
int checkSum = 0;
// for loop that goes through the string and does the computation
for (int i = 0; i < input.length(); i++) {
// if the index is even
if ((i + 1) % 2 == 0) {
checkSum += (input.charAt(i) - '0') * 3;
}
// if the index is odd
else
{
checkSum += input.charAt(i) - '0';
}
}
// modulo 10 of checkSum is assigned to checkSum
checkSum %= 10;
// checkSum is substracted from 10
checkSum = 10 - checkSum;
// if checkSum equals 10, 0 is concatenated with input
// else input is concatenated with checkSum
if (checkSum == 10) input += "0";
else input += checkSum;
// the new value of the isbn-13 is displayed
System.out.println("The ISBN-13 number is " + input);
}
}
Explanation:
The program is written in Java and well commented.
A sample of program output during execution is also attached.