Skip to content

Commit bcdea42

Browse files
committed
isbn code
1 parent 14c90f3 commit bcdea42

File tree

8 files changed

+130
-16
lines changed

8 files changed

+130
-16
lines changed

FAT-Practice/.idea/workspace.xml

Lines changed: 70 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

FAT-Practice/src/ISBN.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Package1.NumberManipulation;
2+
3+
import java.util.Scanner;
4+
5+
class InvalidInputException extends Exception{
6+
InvalidInputException(String msg){
7+
super(msg);
8+
}
9+
}
10+
11+
public class ISBN {
12+
public static void main(String[] args) {
13+
Scanner sc = new Scanner(System.in);
14+
String is = sc.nextLine();
15+
try{
16+
if(is.length() != 9){
17+
throw new InvalidInputException("ISBN must be exactly 9 digits");
18+
}
19+
else{
20+
NumberManipulation n = new NumberManipulation();
21+
Integer i = Integer.parseInt(is);
22+
n.extractDigits(i);
23+
System.out.println("Last digit = " + n.findLastDigit());
24+
System.out.println("New ISBN = " + is + n.findLastDigit());
25+
}
26+
}
27+
catch(Exception e){
28+
System.out.println(e);
29+
}
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Package1;
2+
3+
public class NumberManipulation {
4+
int i;
5+
int arr[] = new int[9];
6+
7+
public void extractDigits(int num){
8+
9+
for(i = 8; i >= 0; i--){
10+
arr[i] = num%10;
11+
num = num/10;
12+
}
13+
}
14+
15+
public int findLastDigit(){
16+
int n = 1, sum = 0;
17+
18+
for(i = 0; i < 9; i++) {
19+
sum += arr[i]*n;
20+
n++;
21+
}
22+
23+
return sum;
24+
}
25+
}

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ previous numbers, i.e., `T(n)=T(n-1)+T(n-2)+T(n-3), T(1)=T(2)=1, and T(3)=2`). [
206206
207207
17. Student volunteers from three batches of B.Tech are selected to ensure the smooth conduct of a Technical event. Assume that only 4 volunteers from batch1, 3 volunteers from batch2, 1 volunteer from batch3 are selected. Create a 2-D ragged array of strings to store their register numbers batch wise. The first row stores register numbers of the volunteers in batch1, second row – batch2 and so forth. Use for-each loop to traverse the array elements.[Ans](https://github.com/jacobjohn2016/Java-Programming/blob/master/FAT-Practice/src/volunteers.java)
208208
209-
18.
209+
18. An ISBN (International Standard Book Number) consists of 10 digits d1d2d3d4d5d6d7d8d9d10. The last digit d10 is a checksum, which is calculated from the other nine digits using the following formula: `(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9)% 11`.
210+
* If the checksum is 10, the last digit is denoted X according to the ISBN convention. For the input 013601267, the program should display 0136012671.
211+
* Define a package ‘Package1’ with the class ‘NumberManipulation’ which contains an integer array (size 9) as its instance variable and two methods `extractDigits( )` and `findLastDigit( )`. The `extractDigits( )` method must take an integer number as argument and extract the individual digits of the number and store them in the 9-element integer array. The method `findLastDigit( )` should find out the 10th digit using the above formula and return it.
212+
* Define a main class outside of the package created above and read the 9-digit number as a String. Throw a user defined exception "InvalidInputException" if the length of the input String is not exactly 9 digits and prompt the user to enter the input again. If valid, convert the input into integer and pass it to the method `extractDigits( )` and also invoke `findLastDigit( )` method which would return the last digit. If the last digit is 10, append ‘X’ to the original string else append the digit itself’ and display the ISBN number.
210213
211214
## Contributions
212215
* Initial Author - [jacobjohn2016](github.com/jacobjohn2016)

0 commit comments

Comments
 (0)