Skip to content

Commit

Permalink
Update CheckDigit.java
Browse files Browse the repository at this point in the history
  • Loading branch information
dialtamirano committed May 4, 2024
1 parent 49cf52a commit 2ac21c8
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/CheckDigit.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
public class CheckDigit
{
/** Returns the check digit for num, as described in part (a).
* Precondition: The number of digits in num is between one and
* Precondition: The number of digits in num is between one and
* six, inclusive.
* num >= 0
*/
public static int getCheck(int num)
{
/* to be implemented in part (a) */
int count = 0;
for ( int i = 1; i <= getNumberOfDigits(num);i++){
count = count + ((8-i) * getDigit(num, i));
}
return count % 10;
}

/** Returns true if numWithCheckDigit is valid, or false
Expand All @@ -18,7 +22,14 @@ public static int getCheck(int num)
*/
public static boolean isValid(int numWithCheckDigit)
{
/* to be implemented in part (b) */
int check = numWithCheckDigit % 10;
int num = numWithCheckDigit/10;
int newCheck = getCheck(num);
if (check == newCheck){
return true;
}else{
return false;
}
}

/** Returns the number of digits in num. */
Expand Down

0 comments on commit 2ac21c8

Please sign in to comment.