-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckDigit.java
More file actions
50 lines (42 loc) · 1.55 KB
/
CheckDigit.java
File metadata and controls
50 lines (42 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class CheckDigit
{
// adapted from: https://www.cs.princeton.edu/courses/archive/spr20/cos217/asgts/03symtable/index.html
public static int getCheck(int num) {
int HASH_MULTIPLIER = 65599;
long hash = 0;
long l;
while(num > 0) {
hash = hash * HASH_MULTIPLIER + (num % 10);
num /= 10;
}
return (int) ((hash+7) % 10);
}
// part (a)
public static boolean isValid(int numWithCheckDigit) {
int prefix = numWithCheckDigit / 10;
int check = numWithCheckDigit % 10;
return getCheck(prefix) == check;
}
public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
System.out.printf("%d -> %d|%d\ngetCheck(%d) = %d\n%b\n",
num,
num/10,
num%10,
num/10,
getCheck(num/10),
isValid(num)
);
}
// part (b)
/*
Introduce a new private static int variable numIncorect to the
class, with initial value 0.
Within the code to isValid, instead of returning the result of
the comparison immediately, store it into a new local boolean
variable, valid. If valid is false, then increment
numIncorrect. Then unconditionally return valid.
Potentially add a static getter method for numIncorrect, either
public or private depending on indented use.
*/
}