From aff214f13297490243ac9119b5f4d50713d715ed Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Tue, 30 Apr 2024 15:08:50 +0000 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ README.md | 22 +++++++++++++++++ build.gradle | 18 ++++++++++++++ src/main/java/CheckDigit.java | 45 +++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 7 ++++++ src/test/java/Tester.java | 26 ++++++++++++++++++++ 6 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 build.gradle create mode 100644 src/main/java/CheckDigit.java create mode 100644 src/main/java/Main.java create mode 100644 src/test/java/Tester.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f2a078 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +.gradle/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..1f150ba --- /dev/null +++ b/README.md @@ -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. +``` diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..7cccf6c --- /dev/null +++ b/build.gradle @@ -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" + } +} diff --git a/src/main/java/CheckDigit.java b/src/main/java/CheckDigit.java new file mode 100644 index 0000000..ee0092a --- /dev/null +++ b/src/main/java/CheckDigit.java @@ -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; + } + +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..dafff2d --- /dev/null +++ b/src/main/java/Main.java @@ -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)); + } +} diff --git a/src/test/java/Tester.java b/src/test/java/Tester.java new file mode 100644 index 0000000..935c887 --- /dev/null +++ b/src/test/java/Tester.java @@ -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); + } + + +}