This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.
Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom
The course is maintained by Technische Informatica Breda
A variable of type truth value (boolean
) can only have two values: either true
or false
). Here is an example on how to use boolean variables:
int num1 = 1;
int num2 = 5;
boolean firstGreater = true;
if (num1 <= num2) {
firstGreater = false;
}
if (firstGreater==true) {
System.out.println("num1 is greater");
} else {
System.out.println("num1 was not greater");
}
First, we assign the truth value variable firstGreater
the value true. The first if sentence checks whether num1
is less or equal to num2
. If it is, we change the value of firstGreater to false. The later if sentence selects which string to print based on the truth value.
As a matter of fact, using a truth value in a conditional sentence is easier than the description in the previous example. We can write the second if sentence as follows:
if (firstGreater) { // means the same as firstGreater==true
System.out.println("num1 was greater");
} else {
System.out.println("num1 was not greater");
}
If we want to check if the boolean variable holds the value true, we do not need to write ==true
, just writing the name of the variable is enough!
If we want to check if the boolean variable holds the value false, we can check that using the negation operation ! (exclamation mark):
if (!firstGreater) { // means the same as firstGreater==false
System.out.println("num1 was not greater");
} else {
System.out.println("num1 was greater");
}
Truth values come in especially handy when we want to write methods that check for validity. Let us create a method that checks if the list it gets as a parameter includes only positive numbers (here 0 is considered positive). The method returns the information as a boolean (i.e. truth value).
public static boolean allPositive(ArrayList<Integer> numbers) {
boolean noNegative = true;
for (int number : numbers) {
if (number < 0) {
noNegative = false;
}
}
// if one of the numbers on the list had a value that is below zero, noNegatives becomes false.
return noNegative;
}
The method has a boolean helper variable called noNegative
. First we assign the helper variable the value true. The method checks all numbers on the list one by one. If at least one number is less than 0, we assign the helper variable the value false. In the end the method returns the value of the helper variable. If no negative numbers were found, it has the value true, otherwise it has the value false.
The method is used as follows:
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(3);
numbers.add(1);
numbers.add(-1);
boolean result = allPositive(numbers);
if (result) { // means the same as result == true
System.out.println("all numbers are positive");
} else {
System.out.println("there is at least one negative number");
}
}
Usually it is not necessary to store the answer into a variable. We can write the method call directly as the condition:
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(4);
numbers.add(7);
numbers.add(12);
numbers.add(9);
if (allPositive(numbers)) {
System.out.println("all numbers are positive");
} else {
System.out.println("there is at least one negative number");
}
The execution of a method is stopped immediately when a command called return
is executed. Using this information to our advantage, we write the allPositive
method easier to understand.
public static boolean allPositive(ArrayList<Integer> numbers) {
for (int number : numbers) {
if (number < 0) {
return false;
}
}
// if the execution reached this far, no negative numbers were found
// so we return true
return true;
}
When we are going through the list of numbers and we find a negative number, we can exit the method by returning false. If there are no negative numbers on the list, we get to the end and therefore can return the value true. We now got rid of the helper variable inside the method!
Exercise truth-values-1: Is the number more than once in the list?
Create the method
moreThanOnce
that gets a list of integers and an integer (i.e. number) as parameter. If the number appears on the list more than once the method returnstrue
and otherwisefalse
.The program body is the following:
public static boolean moreThanOnce(ArrayList<Integer> list, int number) { // write your code here } public static void main(String[] args) { Scanner reader = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<Integer>(); list.add(3); list.add(2); list.add(7); list.add(2); System.out.println("Type a number: "); int number = Integer.parseInt(reader.nextLine()); if (moreThanOnce(list, number)) { System.out.println(number + " appears more than once."); } else { System.out.println(number + " does not appear more than once."); } }
Type a number: ~~2~~ 2 appears more than once
Type a number: ~~3~~ 3 does not appear more than once.
Exercise truth-values-2: Palindrome
Create the method
palindrome
that checks if a string is a palindrome (reads the same forward and backward).The method can use the method
reverse
(from assignment number 56. Reversing text) as a helper. The method type isboolean
, so it returns eithertrue
(the string is a palindrome) orfalse
(the string is not a palindrome).public static boolean palindrome(String text) { // write your code here } public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.println("Type a text: "); String text = reader.nextLine(); if (palindrome(text)) { System.out.println("The text is a palindrome!"); } else { System.out.println("The text is not a palindrome!"); } }
Example outputs:
Type a text: ~~madam~~ The text is a palindrome! ```output Type a word: ~~example~~ The text is not a palindrome!