Skip to content

Commit 838d49d

Browse files
authored
Merge pull request mouredev#5419 from kronstadt-lambda/main
#1 - Java
2 parents fc1ac1f + 605cfcc commit 838d49d

File tree

2 files changed

+367
-0
lines changed

2 files changed

+367
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/**
2+
* @author KronstadtLambda
3+
* @version 1.0
4+
*/
5+
6+
// package roadmap.java.r01;
7+
8+
public class KronstadtLambda {
9+
10+
public static int suma(int a, int b) {
11+
return a + b; // Return the sum of a and b
12+
}
13+
public static void main(String[] args) {
14+
15+
// Initialize variables
16+
float float_a = 2.0f;
17+
float float_b = 2.0f;
18+
int bit_a = 0b01000001;
19+
int bit_b = 0b01010011;
20+
String day = "Monday";
21+
int edad = 1;
22+
boolean isBool;
23+
int[] numbers = {1, 2, 3, 4, 5};
24+
25+
/*
26+
* Operators in Java
27+
*/
28+
// Arithmetic operators
29+
System.out.println("float_a + float_b = " + (float_a + float_b));
30+
System.out.println("float_a - float_b = " + (float_a - float_b));
31+
System.out.println("float_a * float_b = " + (float_a * float_b));
32+
System.out.println("float_a / float_b = " + (float_a / float_b));
33+
System.out.println("float_a % float_b = " + (float_a % float_b)); // Remainder of the division
34+
System.out.println("float_a++ = " + (float_a++)); // Post-increment
35+
System.out.println("float_a-- = " + (float_a--)); // Post-decrement
36+
System.out.println("++float_a = " + (++float_a)); // Pre-increment
37+
System.out.println("--float_a = " + (--float_a)); // Pre-decrement
38+
39+
// Assignment operators
40+
System.out.println("float_a += float_b -> " + (float_a += float_b)); // float_a = float_a + float_b
41+
System.out.println("float_a -= float_b -> " + (float_a -= float_b)); // float_a = float_a - float_b
42+
System.out.println("float_a *= float_b -> " + (float_a *= float_b)); // float_a = float_a * float_b
43+
System.out.println("float_a /= float_b -> " + (float_a /= float_b)); // float_a = float_a / float_b
44+
System.out.println("float_a %= float_b -> " + (float_a %= float_b)); // float_a = float_a % float_b
45+
System.out.println("bit_a &= bit_b -> " + (bit_a &= bit_b)); // bit_a = bit_a & bit_b (bitwise AND)
46+
System.out.println("bit_a |= bit_b -> " + (bit_a |= bit_b)); // bit_a = bit_a | bit_b (bitwise OR)
47+
System.out.println("bit_a ^= bit_b -> " + (bit_a ^= bit_b)); // bit_a = bit_a ^ bit_b (bitwise XOR)
48+
System.out.println("bit_a >>= 1 -> " + (bit_a >>= 1)); // bit_a = int_a >> 1 (bitwise right shift)
49+
System.out.println("bit_a <<= 1 -> " + (bit_a <<= 1)); // bit_a = int_a << 1 (bitwise left shift)
50+
51+
// Comparison operators
52+
System.out.println("float_a == float_b -> " + (float_a == float_b)); // Equal to
53+
System.out.println("float_a != float_b -> " + (float_a != float_b)); // Not equal to
54+
System.out.println("float_a > float_b -> " + (float_a > float_b)); // Greater than
55+
System.out.println("float_a < float_b -> " + (float_a < float_b)); // Less than
56+
System.out.println("float_a >= float_b -> " + (float_a >= float_b)); // Greater than or equal to
57+
System.out.println("float_a <= float_b -> " + (float_a <= float_b)); // Less than or equal to
58+
59+
// Logical operators
60+
System.out.println("float_a == float_b && float_a != 0 -> " + (float_a == float_b && float_a != 0)); // Logical AND
61+
System.out.println("float_a == float_b || float_a != 0 -> " + (float_a == float_b || float_a != 0)); // Logical OR
62+
System.out.println("!(float_a == float_b) -> " + !(float_a == float_b)); // Logical NOT
63+
64+
// Bitwise operators
65+
System.out.println("bit_a & bit_b -> " + (bit_a & bit_b)); // Bitwise AND
66+
System.out.println("bit_a | bit_b -> " + (bit_a | bit_b)); // Bitwise OR
67+
System.out.println("bit_a ^ bit_b -> " + (bit_a ^ bit_b)); // Bitwise XOR
68+
System.out.println("~bit_a -> " + (~bit_a)); // Bitwise NOT
69+
System.out.println("bit_a >> 1 -> " + (bit_a >> 1)); // Bitwise right shift
70+
System.out.println("bit_a << 1 -> " + (bit_a << 1)); // Bitwise left shift
71+
System.out.println("bit_a >>> 1 -> " + (bit_a >>> 1)); // Bitwise right shift with zero fill
72+
73+
// Java don't have identity and membership operators
74+
75+
/*
76+
* Control structures in Java
77+
*/
78+
// If, if-else, else statement
79+
if (float_a == float_b) {
80+
System.out.println("float_a is equal to float_b");
81+
} else if (float_a > float_b) {
82+
System.out.println("float_a is greater than float_b");
83+
} else {
84+
System.out.println("float_a is less than float_b");
85+
}
86+
// Switch statement
87+
switch (day) {
88+
case "Monday":
89+
System.out.println("Today is Monday");
90+
break; // Exit the switch statement to avoid fall-through
91+
case "Tuesday":
92+
System.out.println("Today is Tuesday");
93+
break; // Exit the switch statement to avoid fall-through
94+
case "Wednesday":
95+
System.out.println("Today is Wednesday");
96+
break; // Exit the switch statement to avoid fall-through
97+
case "Thursday":
98+
System.out.println("Today is Thursday");
99+
break; // Exit the switch statement to avoid fall-through
100+
case "Friday":
101+
System.out.println("Today is Friday");
102+
break; // Exit the switch statement to avoid fall-through
103+
case "Saturday":
104+
System.out.println("Today is Saturday");
105+
break; // Exit the switch statement to avoid fall-through
106+
case "Sunday":
107+
System.out.println("Today is Sunday");
108+
break; // Exit the switch statement to avoid fall-through
109+
default: // if none of the above cases are true
110+
System.out.println("Invalid day");
111+
break; // Exit the switch statement to avoid fall-through
112+
}
113+
// For loop
114+
for (int i = 10; i >= 0; i--) {
115+
System.out.println("The loop end in ... " + i);
116+
}
117+
// While loop
118+
while (edad < 18) {
119+
System.out.println("You are " + edad + " years old");
120+
edad++;
121+
}
122+
// do-while loop
123+
do {
124+
isBool = false;
125+
System.out.println("At least one execution");
126+
} while (isBool);
127+
// For-each loop
128+
for (int number : numbers) {
129+
System.out.println("Square of " + number + " is " + number * number);
130+
}
131+
// break statement
132+
for (int i = 0; i < 10; i++) {
133+
if (i == 5) {
134+
System.out.println("Break in i = " + i);
135+
break; // Exit the loop
136+
}
137+
System.out.println("i = " + i);
138+
}
139+
// continue statement
140+
for (int i = 0; i < 10; i++) {
141+
if (i == 5) {
142+
System.out.println("Continue (skip) in i = " + i);
143+
continue; // Skip the rest of the loop
144+
}
145+
System.out.println("Cube of " + i + " is " + i * i * i);
146+
}
147+
// return statement
148+
System.out.println("The sum of 5 and 7 is " + suma(5, 7));
149+
// try-catch
150+
try {
151+
int[] array = {1, 2, 3};
152+
System.out.println(array[3]); // ArrayIndexOutOfBoundsException
153+
} catch (ArrayIndexOutOfBoundsException e) {
154+
System.out.println("Array index out of bounds");
155+
}
156+
// try-catch-finally
157+
try {
158+
int[] array = {1, 2, 3};
159+
System.out.println(array[3]); // ArrayIndexOutOfBoundsException
160+
} catch (ArrayIndexOutOfBoundsException e) {
161+
System.out.println("Array index out of bounds"); // Execute if an exception is thrown
162+
} finally {
163+
System.out.println("Finally block executed"); // Necesary for close resources or other actions
164+
}
165+
// throw statement
166+
try {
167+
throw new ArithmeticException("Arithmetic exception"); // Intentionally throw an exception
168+
/*
169+
* It is useful for testing the behavior of the program when an exception is thrown like error handling.
170+
*/
171+
} catch (ArithmeticException e) {
172+
System.out.println(e.getMessage());
173+
}
174+
175+
/*
176+
* Extra exercise
177+
* Create a program that prints to the console all numbers between 10 and 55 (inclusive), even, and that are neither 16 nor multiples of 3
178+
*/
179+
for (int i = 10; i <= 55; i++) {
180+
if (i % 2 == 0 && i != 16 && i % 3 != 0) {
181+
System.out.println("The solution is " + i);
182+
}
183+
}
184+
185+
}
186+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* @author: Kronstadt-Lambda
3+
* @version: 1.0
4+
*/
5+
6+
// Initialize variables
7+
let float_a = 2.0;
8+
let float_b = 2.0;
9+
let bit_a = 0b00000101;
10+
let bit_b = 0b00000110;
11+
const day = "Wendesday";
12+
let edad = 1;
13+
let isBool;
14+
const person = { name: "Jhon", age: 30 };
15+
const numbers = [1, 2, 3];
16+
17+
/**
18+
* Operators in JavaScript
19+
*/
20+
// Arithmetic operators
21+
console.log("Addition:", float_a + float_b);
22+
console.log("Subtraction:", float_a - float_b);
23+
console.log("Multiplication:", float_a * float_b);
24+
console.log("Division:", float_a / float_b);
25+
console.log("Exponentiation:", float_a ** float_b);
26+
console.log("Modulus:", float_a % float_b);
27+
console.log("Pre-Increment:", ++float_a);
28+
console.log("Pre-Decrement:", --float_a);
29+
console.log("Post-increment:", float_a++);
30+
console.log("Post-decrement:", float_a--);
31+
32+
// Assignment operators
33+
console.log("Simple Assignment:", float_a = float_b);
34+
console.log("Addition Assignment:", float_a += float_b);
35+
console.log("Subtraction Assignment:", float_a -= float_b);
36+
console.log("Multiplication Assignment:", float_a *= float_b);
37+
console.log("Division Assignment:", float_a /= float_b);
38+
console.log("Exponentiation Assignment:", float_a **= float_b);
39+
console.log("Modulus Assignment:", float_a %= float_b);
40+
console.log("Bitwise AND Assignment:", bit_a &= bit_b);
41+
console.log("Bitwise OR Assignment:", bit_a |= bit_b);
42+
console.log("Bitwise XOR Assignment:", bit_a ^= bit_b);
43+
console.log("Left Shift Assignment:", bit_a <<= bit_b);
44+
console.log("Right Shift Assignment:", bit_a >>= bit_b);
45+
46+
// Comparison operators
47+
console.log("Equal to:", float_a == float_b);
48+
console.log("Not equal to:", float_a != float_b);
49+
console.log("Strict equal (value and type) to:", float_a === float_b);
50+
console.log("Strict not equal (value and type) to:", float_a !== float_b);
51+
console.log("Greater than:", float_a > float_b);
52+
console.log("Less than:", float_a < float_b);
53+
console.log("Greater than or equal to:", float_a >= float_b);
54+
console.log("Less than or equal to:", float_a <= float_b);
55+
56+
// Logical operators
57+
console.log("Logical AND:", true && false);
58+
console.log("Logical OR:", true || false);
59+
console.log("Logical NOT:", !true);
60+
61+
// Bitwise operators
62+
console.log("Bitwise AND:", bit_a & bit_b);
63+
console.log("Bitwise OR:", bit_a | bit_b);
64+
console.log("Bitwise XOR:", bit_a ^ bit_b);
65+
console.log("Bitwise NOT:", ~bit_a);
66+
console.log("Left Shift:", bit_a << 1);
67+
console.log("Right Shift:", bit_a >> 1);
68+
console.log("Zero-fill Right Shift:", bit_a >>> 1);
69+
70+
// Identity operators
71+
console.log("Equal value and type:", float_a === float_b);
72+
console.log("Not equal value or not equal type:", float_a !== float_b);
73+
74+
// Membership operators
75+
console.log("Is in:", 'name' in person); // Verify if the property exists in the object
76+
console.log("Is not in:", 'lastname' in person); // Verify if the property does not exist in the object
77+
console.log("Is in:", numbers.includes(2)); // Verify if the value exists in the array
78+
79+
/**
80+
* Control structures in JavaScript
81+
*/
82+
// If, if-else, else statement
83+
if (float_a === float_b) {
84+
console.log("The numbers are equal.");
85+
} else if (float_a > float_b) {
86+
console.log("float_a is greater than float_b.");
87+
} else {
88+
console.log("float_a is less than float_b.");
89+
}
90+
// Switch statement
91+
switch (day) {
92+
case "Monday":
93+
console.log("Today is Monday.");
94+
break; // Exit the switch statement to avoid executing the next cases
95+
case "Tuesday":
96+
console.log("Today is Tuesday.");
97+
break; // Exit the switch statement to avoid executing the next cases
98+
case "Wednesday":
99+
console.log("Today is Wednesday.");
100+
break; // Exit the switch statement to avoid executing the next cases
101+
case "Thursday":
102+
console.log("Today is Thursday.");
103+
break; // Exit the switch statement to avoid executing the next cases
104+
case "Friday":
105+
console.log("Today is Friday.");
106+
break; // Exit the switch statement to avoid executing the next cases
107+
default: // If no case matches
108+
console.log("It's the weekend.");
109+
break; // Exit the switch statement to avoid executing the next cases
110+
}
111+
// For loop
112+
for (let i = 10; i >= 0; i--) {
113+
console.log("The loop end in ...", i);
114+
}
115+
// While loop
116+
while (edad < 18) {
117+
console.log("I am ", edad, " years old.");
118+
edad++;
119+
}
120+
// Do-while loop
121+
do {
122+
isBool = false;
123+
console.log("At least one execution.");
124+
} while (isBool);
125+
// For-in loop: Usefull for objects
126+
for (let key in person) {
127+
console.log(key, ":", person[key]);
128+
}
129+
// For-of loop: Usefull for arrays
130+
for (let number of numbers) {
131+
console.log("Number:", number);
132+
}
133+
// Break statement
134+
for (let i = 0; i < 10; i++) {
135+
if (i === 5) {
136+
console.log("Break at:", i);
137+
break; // Exit the loop
138+
}
139+
console.log("i = :", i);
140+
}
141+
// Continue statement
142+
for (let i = 0; i < 10; i++) {
143+
if (i === 5) {
144+
console.log("Continue (skip) at:", i);
145+
continue; // Skip the current iteration
146+
}
147+
console.log("Cube of", i, "is", i ** 3);
148+
}
149+
// Label statement: Exit an outer loop
150+
outerloop: for (let i = 0; i < 5; i++) {
151+
console.log("Outerloop:", i);
152+
innerloop: for (let j = 0; j < 5; j++) {
153+
if (j === 3) {
154+
break outerloop; // Exit the outer loop
155+
}
156+
console.log("Innerloop:", j);
157+
}
158+
}
159+
// Return statement: Exit a function
160+
function add(a, b) {
161+
return a + b;
162+
}
163+
// try-catch-finally statement: Handle exceptions
164+
try {
165+
console.log(numbers[3]);
166+
} catch (error) {
167+
console.error("An error occurred:", error);
168+
} finally {
169+
console.log("Finally block.");
170+
}
171+
172+
/**
173+
* Extra exercises
174+
* Create a program that prints to the console all numbers between 10 and 55 (inclusive), even, and that are neither 16 nor multiples of 3
175+
*/
176+
177+
for (let i = 10; i <= 55; i++) {
178+
if (i % 2 === 0 && i !== 16 && i % 3 !== 0) {
179+
console.log("The solution is: ", i);
180+
}
181+
}

0 commit comments

Comments
 (0)