Back

Activity 2.1 Boolean Logic

divider

Introduction

Activity 2.1

Boolean Logic

Topics

  • Application Logic
  • The Boolean Data Type
  • Boolean Logic Operators
  • String Equality

Application Logic

Computer processors can perform more than just arithmetic calculations. They can also make decisions based on certain conditions.

This ability to make decisions allows programs to handle different outcomes.

Application Logic Samples

What possible outcomes may occur when I type in my age?

Boolean Age Screenshot

Application Logic Samples

What possible outcomes may occur when I try to login?

Boolean Login Screenshot

Application Logic Samples

How does the game know when to finish the round or match?

Boolean Game Screenshot

The Boolean Data Type

the boolean data type is used to store values that can be either true or false. It is used for logical expressions, which are questions or conditions that the computer can evaluate.

boolean isRaining = true;
boolean hasUmbrella = false;

Boolean Logic

The study of how logical expressions are formed and evaluated is called boolean logic.

In programming, these logical expressions are constructed using operators and values, and they always result in either true or false.

Boolean Logic Operators

Relational Operators
Operator Name Description
== Equality Returns true if operands are equal.
!= Inequality Returns true if operands are not equal.
> Greater Than Returns true if left operand is greater than right.

Boolean Logic Operators

Relational Operators
Operator Name Description
< Less Than Returns true if left operand is less than right.
>= Greater Than or Equal Returns true if left operand is greater than or equal to right.
<= Less Than or Equal Returns true if left operand is less than or equal to right.

Boolean Logic Operators

Logical Operators
Operator Name Description
&& Logical AND Returns true if both operands are true.
|| Logical OR Returns true if at least one operand is true.
! Logical NOT Returns true if the operand is false, and vice versa.

Boolean Variables

Both Boolean values and expressions can be saved to a variable.

boolean health = 100;
boolean isAlive = health > 0;

String Comparison in Java: == vs. String.equals()

The behavior of the equality operator (==) differs significantly between Java's primitive types (int, boolean, double, etc.) and object types, such as String.

When using == with two String objects, the operator does not compare the actual character contents. Instead, it compares their memory references. It is checking if the two string variables point to the exact same object in memory.

String Comparison in Java: == vs. String.equals()

To compare the actual character content (to see if two strings are textually identical), you must use the equals() method (name1.equals(name2)).

String name1;
String name2;
boolean isSameName = name1.equals(name2);

Sample Boolean Expressions

"abc".equals("abc")

"ABC".equals("abc")

100 != 99

!"ABC".equals("abc")

100 > 100

100 >= 100

Sample Boolean Expressions

!true

!true == false

!(100 > 100)

Key Terms

Boolean
A data type that can only have one of two possible values: true or false. It is fundamental to computer logic, as it represents the outcome of a logical question or condition.
Logical Expression
A statement that a computer can evaluate to determine if it is true or false.

'F' → Fullscreen

Objectives

  • icon Evaluating boolean expressions
  • icon Saving boolean expression results to variables
divider

Activity Tasks

  • icon Create a new project named 2-1-Boolean-Logic.
  • icon Complete each task individually.

Task 1: Initialize Variables

Program.java
public class Program {
public static void main(String[] args) {
String name = "Mr. Mortimer";
int age = 30;
double balance = 150.00;
boolean isTeacher = true;
boolean isMillionaire = false;
//
}
}

Task 2: Name Checks

Program.java
public class Program {
public static void main(String[] args) {
String name = "Mr. Mortimer";
int age = 30;
double balance = 150.00;
boolean isTeacher = true;
boolean isMillionaire = false;
// -----------------------------------------------------------------------------
System.out.println("-Name Checks-");
boolean nameIsMortimer = name.equals("Mr. Mortimer");
System.out.println("Is my name Mr. Mortimer? " + nameIsMortimer);
System.out.println("Is my name Mr. Mertens? " + name.equals("Mr. Mertens"));
System.out.println("My name is not Mr. Merriman: " + !name.equals("Mr. Merriman"));
//
}
}

Task 3: Age-Related Checks

Program.java
public class Program {
public static void main(String[] args) {
String name = "Mr. Mortimer";
int age = 30;
double balance = 150.00;
boolean isTeacher = true;
boolean isMillionaire = false;
// -----------------------------------------------------------------------------
System.out.println("-Name Checks-");
boolean nameIsMortimer = name.equals("Mr. Mortimer");
System.out.println("Is my name Mr. Mortimer? " + nameIsMortimer);
System.out.println("Is my name Mr. Mertens? " + name.equals("Mr. Mertens"));
System.out.println("My name is not Mr. Merriman: " + !name.equals("Mr. Merriman"));
// -----------------------------------------------------------------------------
System.out.println("\n-Age-Related Checks-");
boolean canDrive = age >= 16;
boolean canRentCar = age >= 25;
boolean isSenior = age >= 65;
System.out.println("Am I old enough to drive? " + canDrive);
System.out.println("Am I old enough to rent a car? " + canRentCar);
System.out.println("Am I eligible for a senior citizen discount? " + isSenior);
//
}
}

Task 4: Bank Account Checks

Program.java
public class Program {
public static void main(String[] args) {
String name = "Mr. Mortimer";
int age = 30;
double balance = 150.00;
boolean isTeacher = true;
boolean isMillionaire = false;
// -----------------------------------------------------------------------------
System.out.println("-Name Checks-");
boolean nameIsMortimer = name.equals("Mr. Mortimer");
System.out.println("Is my name Mr. Mortimer? " + nameIsMortimer);
System.out.println("Is my name Mr. Mertens? " + name.equals("Mr. Mertens"));
System.out.println("My name is not Mr. Merriman: " + !name.equals("Mr. Merriman"));
// -----------------------------------------------------------------------------
System.out.println("\n-Age-Related Checks-");
boolean canDrive = age >= 16;
boolean canRentCar = age >= 25;
boolean isSenior = age >= 65;
System.out.println("Am I old enough to drive? " + canDrive);
System.out.println("Am I old enough to rent a car? " + canRentCar);
System.out.println("Am I eligible for a senior citizen discount? " + isSenior);
// -----------------------------------------------------------------------------
System.out.println("\n-Bank Account Checks-");
boolean canBuyCar = balance > 31000;
boolean hasFunds = balance > 0;
System.out.println("Do I have enough to buy a Dodge Challenger? " + canBuyCar);
System.out.println("Do I have a positive bank balance? " + hasFunds);
//
}
}

Task 5: Boolean checks

Program.java
public class Program {
public static void main(String[] args) {
String name = "Mr. Mortimer";
int age = 30;
double balance = 150.00;
boolean isTeacher = true;
boolean isMillionaire = false;
// -----------------------------------------------------------------------------
System.out.println("-Name Checks-");
boolean nameIsMortimer = name.equals("Mr. Mortimer");
System.out.println("Is my name Mr. Mortimer? " + nameIsMortimer);
System.out.println("Is my name Mr. Mertens? " + name.equals("Mr. Mertens"));
System.out.println("My name is not Mr. Merriman: " + !name.equals("Mr. Merriman"));
// -----------------------------------------------------------------------------
System.out.println("\n-Age-Related Checks-");
boolean canDrive = age >= 16;
boolean canRentCar = age >= 25;
boolean isSenior = age >= 65;
System.out.println("Am I old enough to drive? " + canDrive);
System.out.println("Am I old enough to rent a car? " + canRentCar);
System.out.println("Am I eligible for a senior citizen discount? " + isSenior);
// -----------------------------------------------------------------------------
System.out.println("\n-Bank Account Checks-");
boolean canBuyCar = balance > 31000;
boolean hasFunds = balance > 0;
System.out.println("Do I have enough to buy a Dodge Challenger? " + canBuyCar);
System.out.println("Do I have a positive bank balance? " + hasFunds);
// -----------------------------------------------------------------------------
System.out.println("\n-Boolean Checks-");
System.out.println(name + " is a teacher: " + isTeacher);
System.out.println(name + " is a millionaire: " + isMillionaire);
boolean notMillionaire = !isMillionaire;
System.out.println("So, " + name + " isn't a millionaire? " + notMillionaire);
//
}
}

Task 6: Logical Operators

Program.java
public class Program {
public static void main(String[] args) {
String name = "Mr. Mortimer";
int age = 30;
double balance = 150.00;
boolean isTeacher = true;
boolean isMillionaire = false;
// -----------------------------------------------------------------------------
System.out.println("-Name Checks-");
boolean nameIsMortimer = name.equals("Mr. Mortimer");
System.out.println("Is my name Mr. Mortimer? " + nameIsMortimer);
System.out.println("Is my name Mr. Mertens? " + name.equals("Mr. Mertens"));
System.out.println("My name is not Mr. Merriman: " + !name.equals("Mr. Merriman"));
// -----------------------------------------------------------------------------
System.out.println("\n-Age-Related Checks-");
boolean canDrive = age >= 16;
boolean canRentCar = age >= 25;
boolean isSenior = age >= 65;
System.out.println("Am I old enough to drive? " + canDrive);
System.out.println("Am I old enough to rent a car? " + canRentCar);
System.out.println("Am I eligible for a senior citizen discount? " + isSenior);
// -----------------------------------------------------------------------------
System.out.println("\n-Bank Account Checks-");
boolean canBuyCar = balance > 31000;
boolean hasFunds = balance > 0;
System.out.println("Do I have enough to buy a Dodge Challenger? " + canBuyCar);
System.out.println("Do I have a positive bank balance? " + hasFunds);
// -----------------------------------------------------------------------------
System.out.println("\n-Boolean Checks-");
System.out.println(name + " is a teacher: " + isTeacher);
System.out.println(name + " is a millionaire: " + isMillionaire);
boolean notMillionaire = !isMillionaire;
System.out.println("So, " + name + " isn't a millionaire? " + notMillionaire);
// -----------------------------------------------------------------------------
System.out.println("\n-Logical Operator Examples-");
boolean canDriveAndBuy = canDrive && canBuyCar;
System.out.println("Am I old enough to drive AND have enough to buy a Dodge Challenger? " + canDriveAndBuy);
boolean canDriveOrBuy = canDrive || canBuyCar;
System.out.println("Am I old enough to drive OR have enough to buy a Dodge Challenger? " + canDriveOrBuy);
//
}
}
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
-Name Checks-
Is my name Mr. Mortimer? true
Is my name Mr. Mertens? false
My name is not Mr. Merriman: true
-Age-Related Checks-
Am I old enough to drive? true
Am I old enough to rent a car? true
Am I eligible for a senior citizen discount? false
-Bank Account Checks-
Do I have enough to buy a Dodge Challenger? false
Do I have a positive bank balance? true
-Boolean Checks-
Mr. Mortimer is a teacher: true
Mr. Mortimer is a millionaire: false
So, Mr. Mortimer isn't a millionaire? true
-Logical Operator Examples-
Am I old enough to drive AND have enough to buy a Dodge Challenger? false
Am I old enough to drive OR have enough to buy a Dodge Challenger? true
Mr. Mortimer is a millionaire: false
So, Mr. Mortimer isn't a millionaire? true
Mr. Mortimer is a millionaire: false
Mr. Mortimer is a millionaire: false
So, Mr. Mortimer isn't a millionaire? true
-Logical Operator Examples-
Am I old enough to drive AND have enough to buy a Dodge Challenger? false
Am I old enough to drive OR have enough to buy a Dodge Challenger? true
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. How do boolean expressions and logical operators connect to real-world decision-making processes, like the examples of age verification or logging in? Think about how they help a program "decide" what to do.
  2. The activity uses different types of operators (relational and logical). Describe a scenario where you would need to use a combination of relational operators and a logical operator (&& or ||) to get the correct result. For example, checking if a person is both old enough to drive AND has enough money to buy a car.
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete