Skip to content

Commit 0c3308a

Browse files
committed
Renamed chap 03 to chap 04
1 parent 05a50db commit 0c3308a

File tree

6 files changed

+485
-0
lines changed

6 files changed

+485
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
public class ArithmeticOperations {
2+
3+
public static void main(String[] args) {
4+
String banner = repeatChar('=', 50);
5+
6+
// String text = "Numeric Operations in Java";
7+
// int width = 52;
8+
9+
// String centeredText = centerText(text, width);
10+
System.out.println(banner);
11+
System.out.println(centerText("Numeric Operations in Java", 50));
12+
System.out.println(banner);
13+
14+
// Declare numeric variables
15+
int intA = 10;
16+
int intB = 20;
17+
double doubleA = 15.5;
18+
double doubleB = 4.5;
19+
20+
// Arithmetic Operations
21+
System.out.println(centerText("Arithmetic Operations:", 50));
22+
23+
System.out.println("intA + intB = " + (intA + intB)); // Addition
24+
System.out.println("intA - intB = " + (intA - intB)); // Subtraction
25+
System.out.println("intA * intB = " + (intA * intB)); // Multiplication
26+
System.out.println("intA / intB = " + (intA / intB)); // Division
27+
System.out.println("intA % intB = " + (intA % intB)); // Modulus
28+
29+
System.out.println("doubleA + doubleB = " + (doubleA + doubleB)); // Addition
30+
System.out.println("doubleA - doubleB = " + (doubleA - doubleB)); // Subtraction
31+
System.out.println("doubleA * doubleB = " + (doubleA * doubleB)); // Multiplication
32+
System.out.println("doubleA / doubleB = " + (doubleA / doubleB)); // Division
33+
System.out.println("doubleA % doubleB = " + (doubleA % doubleB)); // Modulus
34+
35+
// Relational Operations
36+
System.out.println("\nRelational Operations:");
37+
System.out.println("intA == intB = " + (intA == intB)); // Equal to
38+
System.out.println("intA != intB = " + (intA != intB)); // Not equal to
39+
System.out.println("intA > intB = " + (intA > intB)); // Greater than
40+
System.out.println("intA < intB = " + (intA < intB)); // Less than
41+
System.out.println("intA >= intB = " + (intA >= intB)); // Greater than or equal to
42+
System.out.println("intA <= intB = " + (intA <= intB)); // Less than or equal to
43+
44+
// Bitwise Operations
45+
System.out.println("\nBitwise Operations:");
46+
System.out.println("intA & intB = " + (intA & intB)); // Bitwise AND
47+
System.out.println("intA | intB = " + (intA | intB)); // Bitwise OR
48+
System.out.println("intA ^ intB = " + (intA ^ intB)); // Bitwise XOR
49+
System.out.println("~intA = " + (~intA)); // Bitwise NOT
50+
System.out.println("intA << 2 = " + (intA << 2)); // Left shift
51+
System.out.println("intA >> 2 = " + (intA >> 2)); // Right shift
52+
System.out.println("intA >>> 2 = " + (intA >>> 2)); // Unsigned right shift
53+
54+
// Type Casting
55+
System.out.println("\nType Casting:");
56+
int intC = (int) doubleA; // Casting double to int
57+
double doubleC = intA; // Implicit casting int to double
58+
System.out.println("Casting doubleA to int: " + intC);
59+
System.out.println("Implicit casting intA to double: " + doubleC);
60+
61+
System.out.println(banner);
62+
}
63+
64+
// Method to repeat a character n times and used as a banner
65+
public static String repeatChar(char ch, int n) {
66+
StringBuilder sb = new StringBuilder(n);
67+
for (int i = 0; i < n; i++) {
68+
sb.append(ch);
69+
}
70+
return sb.toString();
71+
}
72+
73+
// Method to center text within a given width
74+
public static String centerText(String text, int width) {
75+
if (text.length() >= width) {
76+
return text;
77+
}
78+
79+
int padding = width - text.length();
80+
int paddingLeft = padding / 2;
81+
int paddingRight = padding - paddingLeft;
82+
83+
StringBuilder sb = new StringBuilder();
84+
for (int i = 0; i < paddingLeft; i++) {
85+
sb.append(' ');
86+
}
87+
sb.append(text);
88+
for (int i = 0; i < paddingRight; i++) {
89+
sb.append(' ');
90+
}
91+
92+
return sb.toString();
93+
}
94+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*****************************************************************************
2+
* PROJECT: Java-101 Comprehensive Programming Course
3+
* MODULE: Introduction to Java Fundamentals
4+
* LESSON: 2.1 - Arithmetic Operators
5+
*
6+
* AUTHOR: Dr. Saad Laouadi, Ph.D.
7+
* CREATED: March 2025
8+
*
9+
* Copyright © 2025 Dr. Saad Laouadi. All Rights Reserved.
10+
* See LICENSE file for complete terms.
11+
*****************************************************************************/
12+
13+
public class ArithmeticOperators {
14+
public static void main(String[] args) {
15+
// Variables for demonstration
16+
int a = 10;
17+
int b = 3;
18+
19+
System.out.println("ARITHMETIC OPERATORS DEMONSTRATION");
20+
System.out.println("==================================");
21+
System.out.println("Working with variables: a = " + a + ", b = " + b);
22+
23+
// Addition operator (+)
24+
int sum = a + b;
25+
System.out.println("\n1. Addition (+)");
26+
System.out.println(" a + b = " + sum);
27+
System.out.println(" 15 + 7 = " + (15 + 7));
28+
29+
// Subtraction operator (-)
30+
int difference = a - b;
31+
System.out.println("\n2. Subtraction (-)");
32+
System.out.println(" a - b = " + difference);
33+
System.out.println(" 15 - 7 = " + (15 - 7));
34+
35+
// Multiplication operator (*)
36+
int product = a * b;
37+
System.out.println("\n3. Multiplication (*)");
38+
System.out.println(" a * b = " + product);
39+
System.out.println(" 5 * 6 = " + (5 * 6));
40+
41+
// Division operator (/)
42+
int quotient = a / b;
43+
double preciseQuotient = (double)a / b;
44+
System.out.println("\n4. Division (/)");
45+
System.out.println(" a / b = " + quotient + " (integer division truncates decimal part)");
46+
System.out.println(" (double)a / b = " + preciseQuotient + " (casting to double gives decimal result)");
47+
System.out.println(" 15 / 2 = " + (15 / 2) + " (integer division)");
48+
System.out.println(" 15.0 / 2 = " + (15.0 / 2) + " (floating-point division)");
49+
50+
// Modulus operator (%)
51+
int remainder = a % b;
52+
System.out.println("\n5. Modulus (%)");
53+
System.out.println(" a % b = " + remainder + " (remainder of a divided by b)");
54+
System.out.println(" 17 % 5 = " + (17 % 5) + " (remainder of 17 divided by 5)");
55+
56+
// Unary plus operator (+)
57+
int positive = +a;
58+
System.out.println("\n6. Unary Plus (+)");
59+
System.out.println(" +a = " + positive + " (maintains the sign)");
60+
61+
// Unary minus operator (-)
62+
int negative = -a;
63+
System.out.println("\n7. Unary Minus (-)");
64+
System.out.println(" -a = " + negative + " (reverses the sign)");
65+
66+
// Increment operator (++)
67+
int c = a;
68+
System.out.println("\n8. Increment (++)");
69+
System.out.println(" Starting with c = " + c);
70+
System.out.println(" Prefix increment (++c): " + (++c) + " (increments first, then uses the value)");
71+
System.out.println(" c is now: " + c);
72+
System.out.println(" Postfix increment (c++): " + (c++) + " (uses the value first, then increments)");
73+
System.out.println(" c is now: " + c);
74+
75+
// Decrement operator (--)
76+
int d = a;
77+
System.out.println("\n9. Decrement (--)");
78+
System.out.println(" Starting with d = " + d);
79+
System.out.println(" Prefix decrement (--d): " + (--d) + " (decrements first, then uses the value)");
80+
System.out.println(" d is now: " + d);
81+
System.out.println(" Postfix decrement (d--): " + (d--) + " (uses the value first, then decrements)");
82+
System.out.println(" d is now: " + d);
83+
84+
// Example with expressions
85+
System.out.println("\nComplex Expressions:");
86+
System.out.println(" (4 + 5) * 2 = " + ((4 + 5) * 2) + " (parentheses control order of operations)");
87+
System.out.println(" 5 + 3 * 2 = " + (5 + 3 * 2) + " (multiplication has higher precedence than addition)");
88+
89+
// Operator precedence demonstration
90+
System.out.println("\nOperator Precedence:");
91+
System.out.println(" 10 + 20 / 5 = " + (10 + 20 / 5) + " (division happens before addition)");
92+
System.out.println(" (10 + 20) / 5 = " + ((10 + 20) / 5) + " (parentheses override default precedence)");
93+
}
94+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
public class ArithmeticOperations {
2+
3+
public static void main(String[] args) {
4+
String banner = repeatChar('=', 50);
5+
6+
// String text = "Numeric Operations in Java";
7+
// int width = 52;
8+
9+
// String centeredText = centerText(text, width);
10+
System.out.println(banner);
11+
System.out.println(centerText("Numeric Operations in Java", 50));
12+
System.out.println(banner);
13+
14+
// Declare numeric variables
15+
int intA = 10;
16+
int intB = 20;
17+
double doubleA = 15.5;
18+
double doubleB = 4.5;
19+
20+
// Relational Operations
21+
System.out.println("\nRelational Operations:");
22+
System.out.println("intA == intB = " + (intA == intB)); // Equal to
23+
System.out.println("intA != intB = " + (intA != intB)); // Not equal to
24+
System.out.println("intA > intB = " + (intA > intB)); // Greater than
25+
System.out.println("intA < intB = " + (intA < intB)); // Less than
26+
System.out.println("intA >= intB = " + (intA >= intB)); // Greater than or equal to
27+
System.out.println("intA <= intB = " + (intA <= intB)); // Less than or equal to
28+
29+
// Bitwise Operations
30+
System.out.println("\nBitwise Operations:");
31+
System.out.println("intA & intB = " + (intA & intB)); // Bitwise AND
32+
System.out.println("intA | intB = " + (intA | intB)); // Bitwise OR
33+
System.out.println("intA ^ intB = " + (intA ^ intB)); // Bitwise XOR
34+
System.out.println("~intA = " + (~intA)); // Bitwise NOT
35+
System.out.println("intA << 2 = " + (intA << 2)); // Left shift
36+
System.out.println("intA >> 2 = " + (intA >> 2)); // Right shift
37+
System.out.println("intA >>> 2 = " + (intA >>> 2)); // Unsigned right shift
38+
39+
// Type Casting
40+
System.out.println("\nType Casting:");
41+
int intC = (int) doubleA; // Casting double to int
42+
double doubleC = intA; // Implicit casting int to double
43+
System.out.println("Casting doubleA to int: " + intC);
44+
System.out.println("Implicit casting intA to double: " + doubleC);
45+
46+
System.out.println(banner);
47+
}
48+
49+
// Method to repeat a character n times and used as a banner
50+
public static String repeatChar(char ch, int n) {
51+
StringBuilder sb = new StringBuilder(n);
52+
for (int i = 0; i < n; i++) {
53+
sb.append(ch);
54+
}
55+
return sb.toString();
56+
}
57+
58+
// Method to center text within a given width
59+
public static String centerText(String text, int width) {
60+
if (text.length() >= width) {
61+
return text;
62+
}
63+
64+
int padding = width - text.length();
65+
int paddingLeft = padding / 2;
66+
int paddingRight = padding - paddingLeft;
67+
68+
StringBuilder sb = new StringBuilder();
69+
for (int i = 0; i < paddingLeft; i++) {
70+
sb.append(' ');
71+
}
72+
sb.append(text);
73+
for (int i = 0; i < paddingRight; i++) {
74+
sb.append(' ');
75+
}
76+
77+
return sb.toString();
78+
}
79+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*****************************************************************************
2+
* PROJECT: Java-101 Comprehensive Programming Course
3+
* MODULE: Introduction to Java Fundamentals
4+
* LESSON: 3.2 - Relational Operators
5+
*
6+
* AUTHOR: Dr. Saad Laouadi, Ph.D.
7+
* CREATED: March 2025
8+
*
9+
* Copyright © 2025 Dr. Saad Laouadi. All Rights Reserved.
10+
* See LICENSE file for complete terms.
11+
*****************************************************************************/
12+
13+
public class RelationalOperators {
14+
public static void main(String[] args) {
15+
// Variables for demonstration
16+
int a = 10;
17+
int b = 20;
18+
int c = 10;
19+
20+
System.out.println("RELATIONAL OPERATORS DEMONSTRATION");
21+
System.out.println("==================================");
22+
System.out.println("Working with variables: a = " + a + ", b = " + b + ", c = " + c);
23+
24+
// Equal to operator (==)
25+
System.out.println("\n1. Equal to (==)");
26+
System.out.println(" a == b: " + (a == b) + " (checks if a equals b)");
27+
System.out.println(" a == c: " + (a == c) + " (checks if a equals c)");
28+
29+
// Not equal to operator (!=)
30+
System.out.println("\n2. Not equal to (!=)");
31+
System.out.println(" a != b: " + (a != b) + " (checks if a is not equal to b)");
32+
System.out.println(" a != c: " + (a != c) + " (checks if a is not equal to c)");
33+
34+
// Greater than operator (>)
35+
System.out.println("\n3. Greater than (>)");
36+
System.out.println(" a > b: " + (a > b) + " (checks if a is greater than b)");
37+
System.out.println(" b > a: " + (b > a) + " (checks if b is greater than a)");
38+
System.out.println(" a > c: " + (a > c) + " (checks if a is greater than c)");
39+
40+
// Less than operator (<)
41+
System.out.println("\n4. Less than (<)");
42+
System.out.println(" a < b: " + (a < b) + " (checks if a is less than b)");
43+
System.out.println(" b < a: " + (b < a) + " (checks if b is less than a)");
44+
System.out.println(" a < c: " + (a < c) + " (checks if a is less than c)");
45+
46+
// Greater than or equal to operator (>=)
47+
System.out.println("\n5. Greater than or equal to (>=)");
48+
System.out.println(" a >= b: " + (a >= b) + " (checks if a is greater than or equal to b)");
49+
System.out.println(" b >= a: " + (b >= a) + " (checks if b is greater than or equal to a)");
50+
System.out.println(" a >= c: " + (a >= c) + " (checks if a is greater than or equal to c)");
51+
52+
// Less than or equal to operator (<=)
53+
System.out.println("\n6. Less than or equal to (<=)");
54+
System.out.println(" a <= b: " + (a <= b) + " (checks if a is less than or equal to b)");
55+
System.out.println(" b <= a: " + (b <= a) + " (checks if b is less than or equal to a)");
56+
System.out.println(" a <= c: " + (a <= c) + " (checks if a is less than or equal to c)");
57+
58+
// Comparing different data types
59+
System.out.println("\nComparing Different Data Types:");
60+
double dVal = 10.0;
61+
System.out.println(" int a = " + a + ", double dVal = " + dVal);
62+
System.out.println(" a == dVal: " + (a == dVal) + " (Java performs type conversion before comparison)");
63+
64+
// Comparing objects
65+
System.out.println("\nComparing Objects:");
66+
String str1 = new String("Hello");
67+
String str2 = new String("Hello");
68+
String str3 = str1;
69+
70+
System.out.println(" String str1 = new String(\"Hello\")");
71+
System.out.println(" String str2 = new String(\"Hello\")");
72+
System.out.println(" String str3 = str1");
73+
74+
System.out.println(" str1 == str2: " + (str1 == str2) + " (== compares references, not content)");
75+
System.out.println(" str1 == str3: " + (str1 == str3) + " (same reference, so true)");
76+
System.out.println(" str1.equals(str2): " + str1.equals(str2) + " (equals() compares content)");
77+
78+
// Relational operators in control flow
79+
System.out.println("\nUsing Relational Operators in Control Flow:");
80+
if (a < b) {
81+
System.out.println(" a is less than b - this code executes when condition is true");
82+
}
83+
84+
if (a == c) {
85+
System.out.println(" a equals c - this code executes when condition is true");
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)