Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
github-classroom[bot] authored Apr 30, 2024
0 parents commit aff214f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
.gradle/
22 changes: 22 additions & 0 deletions README.md
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.
```
18 changes: 18 additions & 0 deletions build.gradle
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"
}
}
45 changes: 45 additions & 0 deletions src/main/java/CheckDigit.java
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;
}

}
7 changes: 7 additions & 0 deletions src/main/java/Main.java
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));
}
}
26 changes: 26 additions & 0 deletions src/test/java/Tester.java
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);
}


}

0 comments on commit aff214f

Please sign in to comment.