Skip to content

Commit 7c27fe9

Browse files
authored
Add files via upload
1 parent c3e964a commit 7c27fe9

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
public class BooleanExample {
2+
public static void main(String[] args) {
3+
// Initialize boolean variables
4+
boolean isJavaFun = true;
5+
boolean isFishTasty = false;
6+
7+
// Print initial values
8+
System.out.println("Initial Boolean Values:");
9+
System.out.println("Is Java fun? " + isJavaFun);
10+
System.out.println("Is fish tasty? " + isFishTasty);
11+
System.out.println(); // Print an empty line for better readability
12+
13+
// Demonstrate logical operations
14+
demonstrateLogicalOperations(isJavaFun, isFishTasty);
15+
16+
// Demonstrate relational operations
17+
demonstrateRelationalOperations();
18+
19+
// Demonstrate real-world use cases
20+
demonstrateUseCases();
21+
}
22+
23+
// Method to demonstrate logical operations with boolean values
24+
private static void demonstrateLogicalOperations(boolean value1, boolean value2) {
25+
System.out.println("Logical Operations:");
26+
// Logical AND
27+
System.out.println("Logical AND (value1 && value2): " + (value1 && value2));
28+
29+
// Logical OR
30+
System.out.println("Logical OR (value1 || value2): " + (value1 || value2));
31+
32+
// Logical NOT
33+
System.out.println("Logical NOT (!value1): " + (!value1));
34+
System.out.println(); // Empty line for separation
35+
}
36+
37+
// Method to demonstrate relational operations that produce boolean results
38+
private static void demonstrateRelationalOperations() {
39+
System.out.println("Relational Operations:");
40+
int number1 = 10;
41+
int number2 = 20;
42+
43+
// Equal to
44+
System.out.println("number1 == number2: " + (number1 == number2));
45+
46+
// Not equal to
47+
System.out.println("number1 != number2: " + (number1 != number2));
48+
49+
// Greater than
50+
System.out.println("number1 > number2: " + (number1 > number2));
51+
52+
// Less than
53+
System.out.println("number1 < number2: " + (number1 < number2));
54+
55+
// Greater than or equal to
56+
System.out.println("number1 >= number2: " + (number1 >= number2));
57+
58+
// Less than or equal to
59+
System.out.println("number1 <= number2: " + (number1 <= number2));
60+
System.out.println(); // Empty line for separation
61+
}
62+
63+
// Method to demonstrate real-world use cases of boolean values
64+
private static void demonstrateUseCases() {
65+
System.out.println("Real-World Use Cases:");
66+
67+
// Example 1: Checking eligibility
68+
int age = 18;
69+
boolean isEligibleToVote = age >= 18;
70+
System.out.println("Is eligible to vote: " + isEligibleToVote);
71+
72+
// Example 2: Toggle state
73+
boolean isLightOn = false;
74+
isLightOn = !isLightOn; // Toggle the state
75+
System.out.println("Light status after toggle: " + (isLightOn ? "On" : "Off"));
76+
77+
// Example 3: Decision-making in conditional statements
78+
if (isLightOn) {
79+
System.out.println("The light is ON.");
80+
} else {
81+
System.out.println("The light is OFF.");
82+
}
83+
84+
System.out.println(); // Empty line for separation
85+
}
86+
}

0 commit comments

Comments
 (0)