ISBN Number in Java
An ISBN (International Standard Book Number) is a unique identifier assigned to books and other publications. In Java, you can create a program to generate and validate ISBN numbers using a few simple steps.
ALGORITHM to generate an ISBN number:
Step 1:
Choose the prefix: The prefix is the first three digits of the ISBN number. The most common prefix is 978, but there are other prefixes as well. You can choose a prefix based on your needs.
Step 2:
Choose the registration group identifier: The registration group identifier is the next one to five digits of the ISBN number. It identifies the country, geographic region, or language area in which the book was published. You can find a list of registration group identifiers on the official ISBN website.
Step 3:
Choose the registrant element: The registrant element is the next one to seven digits of the ISBN number. It identifies the publisher or the publisher's imprint. You can obtain a registrant element from the official ISBN agency for your country.
Step 4:
Choose the publication element: The publication element is the next one to six digits of the ISBN number. It identifies the specific edition, format, or version of the book. You can choose a publication element based on your needs.
Step 5:
Calculate the check digit: The check digit is the final digit of the ISBN number. It is calculated using a formula that involves the first 12 digits of the ISBN number. You can use a Java program to calculate the check digit automatically.
ALGORITHM to validate an ISBN number:
Step 1:
Check the length: An ISBN number must be either 10 or 13 digits long. If it is not, it is invalid.
Step 2:
Check the prefix: An ISBN-10 number must start with the prefix 0 or 1, and an ISBN-13 number must start with the prefix 978 or 979. If it does not, it is invalid.
Step 3:
Calculate the check digit: Use the same formula as in the generation algorithm to calculate the check digit. If the calculated check digit does not match the last digit of the ISBN number, it is invalid.
Step 4:
Check for errors: Check for common errors, such as transposed digits or missing digits.
Implementataion:
File Name:ISBNGenerator
import java.util.Random;
public class ISBNGenerator {
public static void main(String[] args) {
// Generate a sample ISBN number
String prefix = "978";
String registrantElement = "12345";
String publicationElement = "6789";
String isbn = generateISBN(prefix, registrantElement, publicationElement);
System.out.println("Generated ISBN: " + isbn);
// Validate the generated ISBN number
boolean isValid = validateISBN(isbn);
if (isValid) {
System.out.println("Valid ISBN");
} else {
System.out.println("Invalid ISBN");
}
}
// Generate an ISBN-13 number
public static String generateISBN(String prefix, String registrantElement, String publicationElement) {
// Concatenate the prefix, registrant element, and publication element
String isbn = prefix + registrantElement + publicationElement;
// Calculate the check digit
int checkDigit = calculateCheckDigit(isbn);
// Append the check digit to the end of the ISBN
isbn += checkDigit;
return isbn;
}
// Validate an ISBN-13 number
public static boolean validateISBN(String isbn) {
// An ISBN-13 number must be 13 digits long and start with either 978 or 979
if (isbn.length() != 13 || (!isbn.startsWith("978") && !isbn.startsWith("979"))) {
return false;
}
// Extract the first 12 digits of the ISBN
String digits = isbn.substring(0, 12);
// Calculate the check digit
int calculatedCheckDigit = calculateCheckDigit(digits);
// Get the last digit of the ISBN
int checkDigit = Character.getNumericValue(isbn.charAt(12));
// Compare the calculated check digit with the actual check digit
return checkDigit == calculatedCheckDigit;
}
// Calculate the check digit of an ISBN-13 number
public static int calculateCheckDigit(String digits) {
// Multiply each digit by a weight and sum the products
int sum = 0;
for (int i = 0; i < digits.length(); i++) {
int digit = Character.getNumericValue(digits.charAt(i));
int weight = (i % 2 == 0) ? 1 : 3;
sum += digit * weight;
}
// Subtract the sum from the nearest multiple of 10
int remainder = sum % 10;
int checkDigit = (10 - remainder) % 10;
return checkDigit;
}
}
Output:
Generated ISBN: 9781234567899
Valid ISBN
Complexity Analysis:
The time complexity of the generateISBN
method in the example program is O(1) because it simply concatenates the input strings, calculates the check digit using a constant number of operations, and appends the check digit to the end of the string.
The time complexity of the validateISBN
method is O(n) because it needs to iterate over the input string to extract the first 12 digits and calculate the check digit using a constant number of operations, and then it needs to extract the last digit of the input string and compare it to the calculated check digit.
The overall time complexity of the program is O(n + n + n), which simplifies to O(n).
The space complexity of both methods is O(1) because they do not use any additional data structures that depend on the input size.