Skip to content

Commit f8ea35f

Browse files
authored
Merge branch 'code-differently:main' into feature/lesson_06
2 parents 2383acb + 39c94a0 commit f8ea35f

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

lesson_04/ljmcwilliams/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
In this lesson, I explored the differences between Java and JavaScript. Despite their similar names, these languages have distinct features.
2+
3+
- ## Key Differences
4+
- ### Document Preparation:
5+
6+
- Java requires preparation before defining variables and functions.
7+
JavaScript has more flexibility with variable definitions.
8+
9+
- ### User Input:
10+
11+
- In Java, I used the Scanner class to prompt the user for number input.
12+
This required importing Java's Scanner class.
13+
In JavaScript, there is no built-in Scanner class, so I used Node.js as a workaround.
14+
With Node.js, I utilized process.stdin for input and process.stdout for output (similar to console.log).
15+
16+
- ### Loop Control:
17+
18+
- Both languages allow for loops to be exited early, but the methods differ:
19+
In Java, using a break statement within the loop will terminate it.
20+
In JavaScript, I opted for read.close(), which achieves a similar effect.
21+
22+
- ### Variable Declaration:
23+
24+
- Java requires explicit declaration of variable types.
25+
JavaScript automatically assigns variable types.
26+
- ## Loop Behavior
27+
Both languages share similarities in loop functionality:
28+
29+
- **For Loops**:
30+
- Take three expressions.
31+
32+
- **If Statements**:
33+
- Accept a condition and can be nested.
34+
35+
- **Java Example**
36+
```java
37+
for (int i = 2; i < userInput; i++) {
38+
if ((userInput / i) == (int) (userInput / i)) {
39+
System.out.println(userInput + " is not a prime number.");
40+
isConditionMet = true;
41+
break;
42+
}
43+
} if (!isConditionMet) {
44+
System.out.println(userInput + " is a prime number.");
45+
}
46+
```
47+
- **JavaScript Example**
48+
```javascript
49+
for (let i = 2; i < answer; i++) {
50+
if ((answer / i) == parseInt(answer / i)) {
51+
console.log(answer + " is not a prime number.");
52+
isConditionMet = true;
53+
read.close();
54+
}
55+
} if (!isConditionMet) {
56+
console.log(answer + " is a prime number.");
57+
read.close();
58+
}
59+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Scanner; //This allows us to use the scanner to take in user input
2+
public class findPrimeNum {
3+
public static void main(String[] args) {
4+
Scanner objScanner = new Scanner(System.in); //A scanner named 'objScanner'
5+
boolean isConditionMet = false;
6+
7+
/*if the condition is met, the print statement within the loop will be printed.
8+
If not, the print statement after the loop will be printed*/
9+
10+
System.out.print("Please enter a whole number: "); //User input prompt
11+
double userInput = objScanner.nextDouble(); //This reads an integer value from the user
12+
13+
/*to determine a number's primality, we can check to see if the division
14+
of that number by 2 to (1 - that number) results in any whole number quotients.
15+
Once we do the division we can compare that number to (int) number to see if the
16+
values are equal.*/
17+
18+
/*the program should end if A: the first whole number quotient is found, indicating that
19+
* indicating a number is not prime or if B: no whole number quotients are found within the
20+
* range indicating the number IS prime.*/
21+
22+
for (int i = 2; i < userInput; i++) {
23+
if ((userInput / i) == (int) (userInput / i)) {
24+
System.out.println(userInput + " is not a prime number.");
25+
isConditionMet = true;
26+
break; //this helps jump out of the loop once this first whole number quotient instace is found
27+
}
28+
} if (!isConditionMet) {
29+
System.out.println(userInput + " is a prime number.");
30+
}
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const readline = require('readline'); //"require()" imports the readline module which will process the user input
2+
const read = readline.createInterface({
3+
input: process.stdin, //this acts as an input scanner
4+
output: process.stdout //this works like a normal console.log combined with a user's input
5+
});
6+
let isConditionMet = false;
7+
8+
/* When looking at the Node.js docs, it states '.question()' displays the query (in this case the user prompt).
9+
It waits for an input and uses it as the first argument in a callback function (here I am using a arrow function). */
10+
read.question("Enter a number: ", (answer) => {
11+
for (let i = 2; i < answer; i++) {
12+
if ((answer / i) == parseInt(answer / i)) { //similar to the "(int)" statement in java, parseInt converts doubles to ints
13+
console.log(answer + " is not a prime number.");
14+
isConditionMet = true;
15+
read.close(); //this works similar to a break statement in java and ends the program
16+
}
17+
} if (!isConditionMet) {
18+
console.log(answer + " is a prime number.");
19+
read.close();
20+
}
21+
})

lesson_04/shawndunsmore/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Java Method
2+
```java
3+
4+
if (number <= 1){
5+
return false;
6+
}
7+
8+
for (int i = 2; <= Math.sqrt(number); i ++){
9+
if (number % i == 0){
10+
return false;
11+
}
12+
}
13+
14+
return true;
15+
16+
```
17+
18+
## Python Method
19+
```Python
20+
def is_prime(number):
21+
if number <= 1:
22+
return False
23+
24+
for i in range(2, int(math.sqrt(number)) + 1):
25+
if number % i == 0:
26+
return False
27+
28+
return True
29+
```
30+
## Explanation
31+
We start off with both methods making sure the input number is less then or equal to one. Then it redirects down to see if the number comes back as false. Once checked with i to see if the number can be divided by one its checked through with are % operator. Once are loop is complete if it tracks no false numbers then it'll return true.
32+
33+
34+
## Differences
35+
* Both functions are called differently (`java: system.out.print` `Python print`)
36+
37+
* Both languages import math differently (`java: import java.lang.Math`; `Python: import math`)
38+
## Similarities
39+
* Both languages use `math.sqrt` equally to find the square root of the number. (well if you wanna be technical "java" has it start with a capital M but come on man.)
40+
41+
* In both methods Java and Python still use if statements to make choices in the code.
42+
"Used Ai to give me code prompts of python and java"

0 commit comments

Comments
 (0)