George Boole
More than 150 years ago,
there was a mathematician
named George Boole, who took
statements and wrote them in
a precise format, such that a
statement is always true or false.
He founded a branch of mathematics
called Boolean Algebra.
Statements that are either true or false are called Boolean
statements. The conditions you used with selection and
repetition back in Chapter 5 were all Boolean statements.
There is a boolean datatype named after George Boole.
What is a Boolean Statement?
The sentence, statement, condition, whatever, must be
true or false.
No questions, no ambiguities, no arguments.
The basis of processing data is the binary system of on
and off, or 1 and 0, which certainly sounds a bunch like
true or false.
Each of the following five statements is a Boolean
statement:
A mile is longer than a kilometer.
July and August both have the same number of days.
A pound of feathers is lighter than a pound of lead.
The Moon is larger than the Sun.
New York City has more people than Baltimore.
Boolean Statement Examples
English Sentence Boolean Statement
T/F
A mile is longer than a
kilometer.
Mile > Kilometer
True
July and August have
the same days.
A pound of feathers is
lighter than a pound of
lead.
JulDays == AugDays
True
PoundF < PoundL
False
The Moon is larger than MoonSize > SunSize
the Sun.
New York City has more NYPop > BaltPop
people than Baltimore.
False
True
Sentences are not always so short and straight forward.
Frequently there are multiple conditions in one statement.
Special rules need to be followed to determine if the entire
statement is true or false.
Consider the following sentences with compound conditions:
She is a computer science teacher and she is a math teacher.
The number is odd or the number is even.
Enter again if gender is not male or gender is not female.
Employment requires a CPA and five years experience.
The same sentences converted into Boolean statements are:
(She ==
(Number
(Gender
(CPA ==
CSTeacher) and (She == MathTeacher)
% 2 == 1) or (Number % 2 == 0)
!= ‘M’) or (Gender != ‘F’)
‘Y’) and (YrExp >= 5)
Logical OR Example
Consider two young ladies who have a rather
simplistic, and quite politically incorrect, view of
judging potential dates. The first is Kathy who will
date a guy if he is Good Looking OR drives a Nice
Car. This chart shows her 4 possible cases:
Good Looking?
Nice Car?
Date Material?
Boolean Operators
Boolean OR
A
B
A or B
T
T
F
F
T
F
T
F
T
T
T
F
Logical AND Example
Suzy is more picky than Kathy. Suzy will only date
a guy if he BOTH Good Looking AND drives a Nice
Car. This chart shows her 4 possible cases:
Good Looking?
Nice Car?
Date Material?
Boolean Operators
Boolean AND
A
B
A and B
T
T
F
F
T
F
T
F
T
F
F
F
Boolean Operators
Boolean XOR
A
B
A xor B
T
T
F
F
T
F
T
F
F
T
T
F
Boolean Operators
Boolean NOT
A
~A
T
F
F
T
Venn Diagram #1
The Boolean Algebra logical and ( * ) can be
demonstrated with Venn Diagrams, using intersection.
A intersect B also A and B also A * B also AB
Venn Diagram #2
The Boolean Algebra logical or ( + ) can be
demonstrated with Venn Diagrams, using union.
A union B also A or B also A + B
Venn Diagram #3
The Boolean Algebra logical xor ( ⊕ ) can also be
demonstrated with Venn Diagrams.
A exclusive or B also A xor B also A ⊕ B
Venn Diagram #4
The Boolean Algebra logical not( ~ )
not A also ~A
This is the negation or the opposite of A.
Venn Diagram #5
The Boolean Algebra logical not( ~ )
not B also ~B
This is the negation or the opposite of B.
Venn Diagram #6
not (A and B)
~(A * B)
This is the opposite of (A and B).
Venn Diagram #7
not (A or B)
~(A + B)
This is the opposite of (A or B).
Venn Diagram #8
not A and not B
~A * ~B
This is identical to not(A or B).
Venn Diagram #9
not A or not B
~A + ~B
This is identical to not(A and B).
DeMorgan’s Law
not(A and B) = not A or not B
not(A or B) = not A and not B
Venn Diagram #10
not (A xor B)
~(A ⊕ B)
This is the opposite of (A xor B).
Tricky Venn Diagrams
A and not (B)
A * ~B
A or not(B)
A + ~B
A xor not(B)
A ⊕ ~B
How would you shade these Venn Diagrams?
A
B
Tricky Venn Diagrams
A and not (B) A or not(B) A xor not(B)
A * ~B
A + ~B
A ⊕ ~B
Step 1 – Shade A
A
B
Tricky Venn Diagrams
A and not (B) A or not(B) A xor not(B)
A * ~B
A + ~B
A ⊕ ~B
Step 2 – Shade ~B a different way
A
B
Tricky Venn Diagram
A and not (B)
A * ~B
Answer: The part that is shaded twice.
A
B
Tricky Venn Diagram
A or not (B)
A + ~B
Answer: Everything that is shaded.
Tricky Venn Diagram
A xor not (B)
A ⊕ ~B
Answer: Everything shaded only once.
// Java1001.java
// This program demonstrates a boolean expression being used in an if statement.
public class Java1001
{
public static void main(String args[])
{
System.out.println("\nJAVA1001.JAVA\n");
int x = 10;
if (x == 10)
System.out.println("true");
else
System.out.println("false");
if (x == 5)
System.out.println("true");
else
System.out.println("false");
System.out.println();
}
}
JAVA1001.JAVA
true
False
// Java1002.java
// This program demonstrates that conditional statements have
// true or false Boolean values and these values can be displayed.
JAVA1002.JAVA
public class Java1002
true
{
False
public static void main(String args[])
{
System.out.println("\nJAVA1002.JAVA\n");
int x = 10;
System.out.println(x == 10);
System.out.println(x == 5);
System.out.println();
}
}
Output for the next
3 programs…
JAVA1003.JAVA
What is the GCF of 120 and 108?
-->
1
What is the GCF of 120 and 108?
-->
2
What is the GCF of 120 and 108?
-->
3
What is the GCF of 120 and 108?
-->
4
What is the GCF of 120 and 108?
-->
12
Answered correctly after 5 Attempt(s).
// Java1003.java
// This program shows a boolean expression being used in a while statement.
public class Java1003
{
public static void main (String args[])
{
System.out.println("\nJAVA1003.JAVA\n");
int gcf = 0;
int attempt = 0;
while (gcf != 12)
{
attempt++;
System.out.print("\nWhat is the GCF of 120 and 108? --> ");
gcf = Expo.enterInt();
}
System.out.println("\nAnswered correctly after " + attempt + " Attempt(s).\n");
}
}
// Java1004.java
// This program introduces the <boolean> data type, which only has two
// values, true or false. Boolean variables add readability to programs.
public class Java1004
{
public static void main (String args[])
{
System.out.println("\nJAVA1004.JAVA\n");
int gcf;
boolean correct = false;
int attempt = 0;
while (!correct) // same as saying while (correct == false)
{
attempt++;
System.out.print("\nWhat is the GCF of 120 and 108? --> ");
gcf = Expo.enterInt();
if (gcf == 12)
correct = true;
else
correct = false;
}
System.out.println("\nAnswered correctly after " + attempt + " Attempt(s).\n");
}
}
// Java1005.java
// This program executes in the same manner as Java1004.java.
// The abbreviated Boolean assignment statement is used in place of the
// longer if ... else syntax.
public class Java1005
{
public static void main (String args[])
{
System.out.println("\nJAVA1005.JAVA\n");
int gcf;
boolean correct = false;
int attempt = 0;
while (!correct)
{
attempt++;
System.out.print("\nWhat is the GCF of 120 and 108? --> ");
gcf = Expo.enterInt();
correct = (gcf == 12);
}
System.out.println("\nAnswered correctly after " + attempt + " Attempt(s).\n");
}
}
Think of it this way…
Variable
int x;
Expression
23 + 45
Assignment
x = 23 + 45;
double y;
66.23 - 8.11
y = 66.23 - 8.11;
String name; “John” + “Smith” name = “John”+”Smith”;
boolean pass; grade >= 70
pass = grade >= 70;
Java Logical Operators
Java uses || to indicate a logical OR.
Java uses && to indicate a logical AND.
Java uses ! to indicate a logical NOT.
Java uses != for not equals, but != can
also be used to indicate a logical XOR.
// Java1006.java
// This program demonstrates compound decisions with the logical or ( || )
// operator.
public class Java1006
{
public static void main (String args[])
{
System.out.println("Java1006\n");
int education; // years of education
int experience; // years of work experience
System.out.print("Enter years of education ===>> ");
education = Expo.enterInt();
System.out.print("Enter years of experience ===>> ");
experience = Expo.enterInt();
System.out.println();
if (education >= 16 || experience >= 5)
System.out.println("You are hired!");
else
System.out.println("You are not qualified.");
System.out.println();
}
}
NICE BOSS
Java1006.java
Enter years of education ===>>
Enter years of experience ===>>
Java1006.java
16
0
Enter years of education ===>>
Enter years of experience ===>>
You are hired!
You are hired!
Java1006.java
Java1006.java
Enter years of education ===>>
Enter years of experience ===>>
You are hired!
18
10
Enter years of education ===>>
Enter years of experience ===>>
You are not qualified.
13
7
12
3
// Java1007.java
// This program demonstrates compound decisions with the logical and ( && )
// operator.
public class Java1007
{
public static void main (String args[])
{
System.out.println("Java1007\n");
int education; // years of education
int experience; // years of work experience
System.out.print("Enter years of education ===>> ");
education = Expo.enterInt();
System.out.print("Enter years of experience ===>> ");
experience = Expo.enterInt();
System.out.println();
if (education >= 16 && experience >= 5)
System.out.println("You are hired!");
else
System.out.println("You are not qualified.");
System.out.println();
}
}
PICKY BOSS
Java1007.java
Enter years of education ===>>
Enter years of experience ===>>
Java1007.java
16
0
Enter years of education ===>>
Enter years of experience ===>>
You are not qualified.
You are not qualified.
Java1007.java
Java1007.java
Enter years of education ===>>
Enter years of experience ===>>
You are hired!
18
10
Enter years of education ===>>
Enter years of experience ===>>
You are not qualified.
13
7
12
3
// Java1008.java
// This program demonstrates compound decisions with not equals ( != ) operator.
// != can be used to implement XOR (eXclusive OR).
public class Java1008
{
public static void main (String args[])
{
System.out.println("Java1008\n");
int education; // years of education
int experience; // years of work experience
System.out.print("Enter years of education ===>> ");
education = Expo.enterInt();
System.out.print("Enter years of experience ===>> ");
experience = Expo.enterInt();
System.out.println();
if (education >= 16 != experience >= 5)
System.out.println("You are hired!");
else
System.out.println("You are not qualified.");
System.out.println();
}
}
CHEAP BOSS
Java1008.java
Enter years of education ===>>
Enter years of experience ===>>
Java1008.java
16
0
Enter years of education ===>>
Enter years of experience ===>>
You are hired!
You are hired!
Java1008.java
Java1008.java
Enter years of education ===>>
Enter years of experience ===>>
You are not qualified.
18
10
Enter years of education ===>>
Enter years of experience ===>>
You are not qualified.
13
7
12
3
Java XOR Operator != Explained
The result is only true
when A is different from B.
A
B
A xor B
T
T
F
F
T
F
T
F
F
T
T
F
// Java1009.java
// This program demonstrates the logical not ( ! ) operator.
public class Java1009
{
public static void main (String args[])
{
System.out.println("Java1009\n");
int education; // years of education
int experience; // years of work experience
System.out.print("Enter years of education ===>> ");
education = Expo.enterInt();
System.out.print("Enter years of experience ===>> ");
experience = Expo.enterInt();
System.out.println();
if ( !(education >= 16 || experience >= 5) )
System.out.println("You are hired!");
else
System.out.println("You are not qualified.");
System.out.println();
}
}
CRAZY BOSS
Java1009.java
Enter years of education ===>>
Enter years of experience ===>>
Java1009.java
16
0
Enter years of education ===>>
Enter years of experience ===>>
You are not qualified.
You are not qualified.
Java1009.java
Java1009.java
Enter years of education ===>>
Enter years of experience ===>>
You are not qualified.
18
10
Enter years of education ===>>
Enter years of experience ===>>
You are hired!
13
7
12
3
// Java1010.java
// This program determines if an age is between 16 and 89.
public class Java1010
{
public static void main (String args[])
{
System.out.println("Java1010\n");
int age;
System.out.print("How old are you? ===>> ");
age = Expo.enterInt();
System.out.println();
if (age >= 16 && age <= 89)
System.out.println("You are the proper age to drive.");
else
System.out.println("You are not the proper age to drive.");
System.out.println();
}
}
Valid Range: 16-89
Java1010.java
How old are you?
Java1010.java
===>>
16
How old are you?
===>>
43
You are the proper age to drive. You are the proper age to drive.
Java1010.java
How old are you?
===>>
13
You are not the proper age to drive.
Java1010.java
How old are you?
===>>
107
You are not the proper age to drive.
// Java1011.java
// This program shows ranges, nested two-way selection,
// and multi-way selection all at the same time.
// This is how you have to do multi-way selection with real numbers.
public class Java1011
{
public static void main (String args[])
{
System.out.println("Java1011\n");
double gpa;
System.out.print("What is your gpa ===>> ");
gpa = Expo.enterDouble();
System.out.println();
if (gpa >= 3.9)
System.out.println("Summa Cum Laude");
else if (gpa >= 3.75)
System.out.println("Magna Cum Laude");
else if (gpa >= 3.5)
System.out.println("Cum Laude");
else if (gpa >= 2.0)
System.out.println("Graduate without honors");
else
System.out.println("Will not graduate");
}
}
System.out.println();
Java1011.java
What is your GPA?
Java1011.java
===>>
4.0
What is your GPA?
Summa Cum Laude
Magna Cum Laude
Java1011.java
Java1011.java
What is your GPA?
===>>
3.5
Cum Laude
Will not graduate
3.8
===>>
2.5
Graduate without honors
Java1011.java
What is your GPA?
What is your GPA?
===>>
===>>
1.5
Why didn’t you use switch?
In the previous program many if…else statements
were used to simulate multi-way selection.
These statements are necessary because the
switch statement has 2 limitations:
a.
switch does not work with ranges.
b. switch does not work with the double data type.
One new feature of Java Version 7 (jdk 1.7.0)
is that switch now works with Strings.
// Java1012.java
// This program uses an <if> structure inside a <do...while> loop to protect
// against faulty data entry. Valid SAT scores are between 400 and 1600.
// If the user enters a score outside the proper range, he/she will be forced to reenter.
public class Java1012
{
public static void main (String args[])
{
System.out.println("\nJAVA1012.JAVA\n");
boolean scoreOK = false;
int sat = 0;
while (!scoreOK)
{
System.out.print("Enter SAT ===>> ");
sat = Expo.enterInt();
System.out.println();
scoreOK = (sat >= 400 && sat <= 1600);
if (!scoreOK)
{
System.out.println(
"Error... Please enter a valid SAT score between 400 and 1600.");
System.out.println();
}
}
if (sat >= 1100)
{
System.out.println("You are admitted");
System.out.println("Orientation will start in June");
}
else
{
System.out.println("You are not admitted");
System.out.println("Please try again when your SAT improves.");
}
System.out.println();
}
}
Java1012.java
Enter SAT
===>>
100
Error... Please enter a valid SAT score between 400 and 1600.
Enter SAT
===>>
2000
Error... Please enter a valid SAT score between 400 and 1600.
Enter SAT
===>>
-365
Error... Please enter a valid SAT score between 400 and 1600.
Enter SAT
===>>
1000000
Error... Please enter a valid SAT score between 400 and 1600.
Enter SAT
===>>
1300
You are admitted
Orientation will start in June
© Copyright 2025 Paperzz