Practical Applications of Boolean Values
In this step, you will learn how to use boolean values in practical scenarios such as conditional statements and methods. These are common patterns you will encounter in real-world Java programming.
Using Booleans in Conditional Statements
Booleans are most commonly used with if-else
statements to control the flow of your program. Let's create a new file to explore this:
- Create a new file in the
booleans
directory called BooleanConditions.java
- Add the following code to the file:
public class BooleanConditions {
public static void main(String[] args) {
// Boolean for controlling access
boolean isLoggedIn = true;
// Using a boolean in an if-else statement
if (isLoggedIn) {
System.out.println("Welcome back, user!");
System.out.println("You have access to the system.");
} else {
System.out.println("Please log in to continue.");
}
// Using boolean expressions directly in if statements
int age = 20;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
// Combined boolean conditions
boolean hasCompletedCourse = true;
boolean hasPaidFees = false;
if (hasCompletedCourse && hasPaidFees) {
System.out.println("Certificate is ready for download.");
} else if (hasCompletedCourse) {
System.out.println("Please pay the fees to get your certificate.");
} else {
System.out.println("Please complete the course first.");
}
}
}
This program demonstrates:
- Using a boolean variable directly in an
if
statement
- Using boolean expressions in conditional logic
- Combining multiple boolean conditions with logical operators (
&&
, ||
)
Boolean Methods and Returns
Another common use of booleans is creating methods that return boolean values. These methods typically check conditions and return true
or false
accordingly.
Let's modify our file to include a couple of boolean methods:
- Add the following code to the end of your
BooleanConditions.java
file, inside the class but outside the main
method:
// Method that returns a boolean value
public static boolean isPasswordValid(String password) {
return password.length() >= 8;
}
// Method that checks multiple conditions
public static boolean isEligibleForDiscount(int age, boolean isStudent) {
return age < 25 && isStudent;
}
- Now, add code to the
main
method to use these new methods:
// Using methods that return boolean values
String password = "pass123";
boolean isValid = isPasswordValid(password);
System.out.println("Is password valid? " + isValid);
if (isValid) {
System.out.println("Password meets the requirements.");
} else {
System.out.println("Password is too short.");
}
// Testing the eligibility method
boolean eligibleForDiscount = isEligibleForDiscount(22, true);
System.out.println("Eligible for student discount: " + eligibleForDiscount);
Compiling and Running the Program
Now let's compile and run your program:
cd ~/project/booleans
javac BooleanConditions.java
java BooleanConditions
You should see output similar to:
Welcome back, user!
You have access to the system.
You are an adult.
Please pay the fees to get your certificate.
Is password valid? false
Password is too short.
Eligible for student discount: true
The output will vary based on the boolean values and conditions in your code. Feel free to modify the values and see how the output changes.