Skip to content

Commit 14340d2

Browse files
finished exercise 36
1 parent 5f2cabe commit 14340d2

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/com/github/jonathanbirkey/chapter03/Exercise09.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
* program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN
1111
* (including leading zeros). Your program should read the input as an integer. Here are sample
1212
* runs:
13-
* <p>Enter the first 9 digits of an ISBN as integer: 013601267 The ISBN-10 number is 0136012671
14-
* Enter the first 9 digits of an ISBN as integer: 013031997 The ISBN-10 number is 013031997X
13+
* <p>Enter the first 9 digits of an ISBN as integer: 013601267
14+
* <p>The ISBN-10 number is 0136012671
15+
* <p>Enter the first 9 digits of an ISBN as integer: 013031997
16+
* <p>The ISBN-10 number is 013031997X
1517
*/
1618
package com.github.jonathanbirkey.chapter03;
1719

@@ -45,9 +47,9 @@ public static void main(String[] args) {
4547
int checksum =
4648
(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11;
4749
if (checksum == 10) {
48-
System.out.printf("%d%d%d%d%d%d%d%d%dX\n", d1, d2, d3, d4, d5, d6, d7, d8, d9);
50+
System.out.printf("The ISBN-10 number is %d%d%d%d%d%d%d%d%dX\n", d1, d2, d3, d4, d5, d6, d7, d8, d9);
4951
} else {
50-
System.out.printf("%d%d%d%d%d%d%d%d%d%d\n", d1, d2, d3, d4, d5, d6, d7, d8, d9, checksum);
52+
System.out.printf("The ISBN-10 number is %d%d%d%d%d%d%d%d%d%d\n", d1, d2, d3, d4, d5, d6, d7, d8, d9, checksum);
5153
}
5254
}
5355
}

src/com/github/jonathanbirkey/chapter05/Exercise36.java

+37-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,47 @@
22
* @author : Jonathan Birkey
33
* @mailto : [email protected]
44
* @created : 28Feb2024
5-
* <p>(Display leap years) Write a program that displays all the leap years, 10 per line, from
6-
* 101 to 2100, separated by exactly one space. Also display the number of leap years in this
7-
* period.
5+
* <p>(Business application: checking ISBN) Use loops to simplify Programming Exercise 3.9.
86
*/
97
package com.github.jonathanbirkey.chapter05;
108

9+
import java.util.Scanner;
10+
1111
public class Exercise36 {
1212
public static void main(String[] args) {
13-
// TODO: solve
13+
Scanner input = new Scanner(System.in);
14+
System.out.print("Enter the first 9 digits of an ISBN as integer: ");
15+
int number = input.nextInt();
16+
input.close();
17+
18+
int tempNumber = number;
19+
int leadingZeroCounter = 0;
20+
int checksum = 0;
21+
22+
for (int i = 9; i > 0; i--) {
23+
int digit = tempNumber % 10;
24+
tempNumber = tempNumber / 10;
25+
if(digit == 0) {
26+
leadingZeroCounter++;
27+
}else {
28+
leadingZeroCounter = 0;
29+
}
30+
checksum += digit * i;
31+
}
32+
33+
checksum = checksum % 11;
34+
System.out.printf("The ISBN-10 number is ");
35+
36+
if (checksum == 10) {
37+
for(int j = 0; j < leadingZeroCounter; j++) {
38+
System.out.print("0");
39+
}
40+
System.out.printf("%dX", number);
41+
} else {
42+
for(int j = 0; j < leadingZeroCounter; j++) {
43+
System.out.print("0");
44+
}
45+
System.out.printf("%d%d", number, checksum);
46+
}
1447
}
1548
}

0 commit comments

Comments
 (0)