Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

check digit (aka checksum digit) is added to a number, such as book ISBN number

ID: 3693874 • Letter: C

Question

check digit (aka checksum digit) is added to a number, such as book ISBN number or bank routing number, to verify the correctness of the number. The check digit is usually added as the last digit in the number. A common usage of a check digit is to detect errors made while entering the number into a system. For this assignment, you will write a C++ program to generate check digits for ISBN-13 numbers.

The ISBN-13 is a standard, introduced in 2007, for identifying books. It uses 13 digits d0d1d2d3d4d5d6d7d8d9d10d11d12. The last digit d12 (i.e., the 13th digit) is check digit that is calculated from the other digits using the following formula:

10 - (d0 + d1 * 3 + d2 + d3 * 3 + d4 + d5 * 3 + d6 + d7 * 3 + d8 + d9 * 3 + d10 + d11 * 3) % 10

If the checksum is 10, replace it with 0.

Your code must have a function that accepts a string parameter of the first 12-digits in an ISBN-13 number and returns the checksum digit (i.e., the 13th digit). You may use other functions if you like. For example, a helpful function takes a character containing a digit as parameter and returns that digit as an integer, which you can define as follows:

The program must read input from a file, named isbn.txt, that is located in the same folder as the program. Each line on the file contains the first 12-digits of a valid ISBN-13 number. The program outputs to the computer's screen the checksum digit for each number in the file. For example, using the following input file:

your program should produce the following output:

Explanation / Answer

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; class Test{ public static void main(String[] args) { BufferedReader br = null; try { String line; br = new BufferedReader(new FileReader("isbn.txt")); int[] nums = new int[12]; while ((line = br.readLine()) != null) { int i=0; for(char c : line.toCharArray()){ nums[i] = toDigit(c); i++; } System.out.println(getCheckSum(nums[0],nums[1],nums[2],nums[3], nums[4],nums[5],nums[6],nums[7],nums[8],nums[9],nums[10],nums[11])); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } } } static int getCheckSum(int d0,int d1,int d2,int d3,int d4,int d5,int d6,int d7,int d8,int d9,int d10,int d11){ return 10 - (d0 + d1 * 3 + d2 + d3 * 3 + d4 + d5 * 3 + d6 + d7 * 3 + d8 + d9 * 3 + d10 + d11 * 3) % 10; } static int toDigit(char ch){ return ch - '0'; } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote