Skip to content

Commit 193e69f

Browse files
authored
feat: adds Tyran's prime_java and prime_python with a README.md (#137)
1 parent fee9b89 commit 193e69f

File tree

14 files changed

+721
-0
lines changed

14 files changed

+721
-0
lines changed

lesson_04/tyranricejr/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# IsPrimeNumber Functionality Between Python & Java
2+
3+
This markdown file compares the implementation of a function that checks if a number is prime in both Python and Java. It includes code snippets for each language, test cases to verify correctness, and an explanation of the logic used. The document also highlights key differences between the two languages in terms of syntax, type handling, and function usage.
4+
5+
## Python Implementation
6+
7+
### IsPrimeNumber.py Snippet
8+
```python
9+
def IsAPrimeNumber(num):
10+
if num <= 1:
11+
return False
12+
for i in range(2, int(num**0.5) + 1):
13+
if num % i == 0:
14+
return False
15+
return True
16+
```
17+
18+
### IsPrimeNumberTest.py Snippet
19+
```python
20+
def IsAPrimeNumberTest(num):
21+
self.assertTrue(IsAPrimeNumber.IsAPrimeNumber(11))
22+
self.assertFalse(IsAPrimeNumber.IsAPrimeNumber(9))
23+
self.assertFalse(IsAPrimeNumber.IsAPrimeNumber(0))
24+
```
25+
26+
## JavaScript Implementation
27+
28+
### IsPrimeNumber.java
29+
```java
30+
public boolean isPrime(int number) {
31+
if (number <= 1) {
32+
return false;
33+
}
34+
for (int i = 2; i <= Math.sqrt(number); i++) {
35+
if (number % i == 0) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
```
42+
43+
### IsPrimeNumberTest.java
44+
```java
45+
@BeforeEach
46+
public void setUp() {
47+
primeChecker = new IsAPrimeNumber();
48+
}
49+
50+
public boolean isPrimeTest(int number) {
51+
assertTrue(primeChecker.isPrime(11));
52+
assertFalse(primeChecker.isPrime(9));
53+
assertFalse(primeChecker.isPrime(-3));
54+
}
55+
```
56+
57+
## Explanation
58+
59+
I define a function that determines whether a given integer is a prime number. The function returns `True` if the number is prime, and `False` otherwise. The logic checks if the number is less than or equal to 1 (not prime), then iterates from 2 up to the square root of the number to see if any divisor exists. If a divisor is found, the function returns false; otherwise, it returns true.
60+
61+
Test cases are also provided in both languages to verify the correctness of the function, ensuring that known prime and non-prime numbers are handled as expected.
62+
63+
The following sections provide code snippets and further explanation for each implementation.
64+
65+
66+
## Differences
67+
68+
### 1. Syntax:
69+
- Python uses indentation to define code blocks, while Java uses curly braces `{}`.
70+
- Python function definitions use `def`, whereas Java uses explicit type declarations (`public boolean`).
71+
- Python uses `True`/`False` (capitalized), Java uses `true`/`false` (lowercase).
72+
73+
### 2. Type Coercion:
74+
- Python is dynamically typed, so variable types are inferred at runtime.
75+
- Java is statically typed, requiring explicit type declarations for variables and function return types.
76+
77+
### 3. Function Calls:
78+
- In Python, functions can be called directly without creating an object if defined at the module level.
79+
- In Java, non-static methods require creating an instance of the class before calling the method (e.g., `primeChecker.isPrime(11)`).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.8/userguide/building_java_projects.html in the Gradle documentation.
6+
*/
7+
8+
plugins {
9+
// Apply the application plugin to add support for building a CLI application in Java.
10+
application
11+
java
12+
}
13+
14+
repositories {
15+
// Use Maven Central for resolving dependencies.
16+
mavenCentral()
17+
}
18+
19+
dependencies {
20+
// Use JUnit Jupiter for testing.
21+
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
22+
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.10.2")
23+
24+
// This dependency is used by the application.
25+
implementation("com.google.guava:guava:33.2.0-jre")
26+
27+
implementation("org.apache.commons:commons-text:1.10.0")
28+
}
29+
30+
// Apply a specific Java toolchain to ease working on different environments.
31+
java {
32+
toolchain {
33+
languageVersion = JavaLanguageVersion.of(21)
34+
}
35+
}
36+
37+
application {
38+
// Define the main class for the application.
39+
mainClass.set("prime_java.IsAPrimeNumber")
40+
}
41+
42+
tasks.named<Test>("test") {
43+
// Use JUnit Platform for unit tests.
44+
useJUnitPlatform()
45+
testLogging {
46+
events("PASSED", "FAILED", "SKIPPED", "STANDARD_OUT", "STANDARD_ERROR")
47+
}
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This source file was generated by the Gradle 'init' task
3+
*/
4+
package prime_java;
5+
6+
/*
7+
* Title: IsAPrimeNumber.java
8+
*
9+
* Description: This class contains a method to check if a number is prime.
10+
*
11+
* Created by: Tyran Rice Jr.
12+
* Date: 2023-10-01
13+
*/
14+
public class IsAPrimeNumber {
15+
16+
/*
17+
* Title: isPrime
18+
*
19+
* Description: This method checks if a given number is prime.
20+
*
21+
* Parameters:
22+
* - number: an integer to check for primality.
23+
*
24+
* Returns:
25+
* - true if the number is prime, false otherwise.
26+
*/
27+
public boolean isPrime(int number) {
28+
if (number <= 1) {
29+
return false;
30+
}
31+
for (int i = 2; i <= Math.sqrt(number); i++) {
32+
if (number % i == 0) {
33+
return false;
34+
}
35+
}
36+
return true;
37+
}
38+
39+
public static void main(String[] args) {
40+
41+
}
42+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This source file was generated by the Gradle 'init' task
3+
*/
4+
package prime_java;
5+
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
12+
/*
13+
* Title: IsAPrimeNumberTest.java
14+
*
15+
* Description: This class contains unit tests for the IsAPrimeNumber class.
16+
*
17+
* Created by: Tyran Rice Jr.
18+
* Date: 2023-10-01
19+
*/
20+
class IsAPrimeNumberTest {
21+
22+
IsAPrimeNumber primeChecker;
23+
24+
/*
25+
* Title: setUp
26+
*
27+
* Description: This method initializes the IsAPrimeNumber instance before each test.
28+
*
29+
* Parameters: None
30+
*
31+
* Returns: None
32+
*/
33+
@BeforeEach
34+
public void setUp() {
35+
primeChecker = new IsAPrimeNumber();
36+
}
37+
38+
/*
39+
* Title: testIsPrime
40+
*
41+
* Description: This method tests the isPrime method for prime numbers.
42+
*/
43+
@Test
44+
public void testIsPrime() {
45+
assertTrue(primeChecker.isPrime(11));
46+
assertTrue(primeChecker.isPrime(2));
47+
assertTrue(primeChecker.isPrime(3));
48+
}
49+
50+
/*
51+
* Title: testIsNotPrime
52+
*
53+
* Description: This method tests the isPrime method for non-prime numbers.
54+
*/
55+
@Test
56+
public void testIsNotPrime() {
57+
assertFalse(primeChecker.isPrime(4));
58+
assertFalse(primeChecker.isPrime(9));
59+
assertFalse(primeChecker.isPrime(10));
60+
}
61+
62+
/*
63+
* Title: testEdgeCases
64+
*
65+
* Description: This method tests the isPrime method for edge cases.
66+
*/
67+
@Test
68+
public void testEdgeCases() {
69+
assertFalse(primeChecker.isPrime(1));
70+
assertFalse(primeChecker.isPrime(0));
71+
assertFalse(primeChecker.isPrime(-3));
72+
}
73+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file was generated by the Gradle 'init' task.
2+
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
3+
4+
[versions]
5+
guava = "33.0.0-jre"
6+
junit-jupiter = "5.10.2"
7+
8+
[libraries]
9+
guava = { module = "com.google.guava:guava", version.ref = "guava" }
10+
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)