generated from APCSLowell/2019CD1-CheckDigit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit aff214f
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build/ | ||
.gradle/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# AP Computer Science A FRQ CheckDigit | ||
|
||
|
||
|
||
Both the instructions on pages 199-202 and the quick reference guide on page 209 are available at this link: **[https://apcentral.collegeboard.org/pdf/ap-computer-science-a-course-and-exam-description.pdf?course=ap-computer-science-a](https://apcentral.collegeboard.org/pdf/ap-computer-science-a-course-and-exam-description.pdf?course=ap-computer-science-a)** | ||
|
||
Correct solution and scoring guidelines are available at this link: | ||
|
||
**[https://apcentral.collegeboard.org/pdf/ap-computer-science-a-2019-ced-scoring-guidelines.pdf?course=ap-computer-science-a](https://apcentral.collegeboard.org/pdf/ap-computer-science-a-2019-ced-scoring-guidelines.pdf?course=ap-computer-science-a)** | ||
|
||
The correct answer outputs should be as follows. | ||
|
||
``` | ||
Part A | ||
getCheck(283415) -> 6 | ||
getCheck(2183) -> 2 | ||
``` | ||
``` | ||
Part B | ||
isValid(1592) -> true => valid combo of 159 and its check digit 2 | ||
isValid(1593) -> false => not a valid combo of number 159 and check digit 3 because the check digit of 159 is 2. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testImplementation('org.junit.jupiter:junit-jupiter:5.6.0') | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
testLogging { | ||
events "passed", "skipped", "failed" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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 | ||
* six, inclusive. | ||
* num >= 0 | ||
*/ | ||
public static int getCheck(int num) | ||
{ | ||
/* to be implemented in part (a) */ | ||
} | ||
|
||
/** Returns true if numWithCheckDigit is valid, or false | ||
* otherwise, as described in part (b). | ||
* Precondition: The number of digits in numWithCheckDigit | ||
* is between two and seven, inclusive. | ||
* numWithCheckDigit >= 0 | ||
*/ | ||
public static boolean isValid(int numWithCheckDigit) | ||
{ | ||
/* to be implemented in part (b) */ | ||
} | ||
|
||
/** Returns the number of digits in num. */ | ||
public static int getNumberOfDigits(int num) | ||
{ | ||
if(num < 10) | ||
return 1; | ||
return 1 + getNumberOfDigits(num/10); | ||
} | ||
|
||
/** Returns the nthdigit of num. | ||
* Precondition: n >= 1 and n <= the number of digits in num | ||
*/ | ||
public static int getDigit(int num, int n) | ||
{ | ||
int pos = getNumberOfDigits(num)-n+1; | ||
while(pos > 1){ | ||
num/=10; | ||
pos--; | ||
} | ||
return num%10; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println(CheckDigit.getCheck(159)); | ||
System.out.println(CheckDigit.isValid(1592)); | ||
System.out.println(CheckDigit.isValid(1593)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import java.io.*; | ||
|
||
public class Tester { | ||
|
||
@Test | ||
public void partA () { | ||
assertEquals(CheckDigit.getCheck(283415),6); | ||
assertEquals(CheckDigit.getCheck(2183),2); | ||
} | ||
@Test | ||
public void partB(){ | ||
assertEquals(CheckDigit.isValid(1592), true); | ||
assertEquals(CheckDigit.isValid(1593),false); | ||
} | ||
|
||
|
||
} |