250 likes | 267 Views
Learn about compare and logical operators in Java, along with examples and solutions to check if a number is between 10 and 20. Also, explore the priority of logical operators and a programming example of determining leap years.
E N D
Previously discussed • Recall that there are 2 types of operators that return a boolean result (true or false): • Compare operators: • A compare operator compares 2 numerical expressions and return a Boolean result. • Example: • the compare operation 3 > 1 returns the Boolean value true
Previously discussed (cont.) • Logical operators: • A logical operator compares 2 Boolean (logical) expressions and return a Boolean result. • Example: • the logical operation true AND false returns the Boolean value false
Logical operators • Logical operators in Java:
The logical AND operator && • The && operatoronly operate on the Boolean valuestrue and false • The results are as follows:
The logical AND operator && (cont.) • Example 1: (3 > 1) && (3 < 5) = true && true = true
The logical AND operator && (cont.) • Example 2: (3 > 1) && (3 > 5) = true && false = false
Example program: test if a number is between 10 and 20 • Problem description: • Write a Java program that reads in a number a • The program prints "yes" when 10 ≤ a ≤ 20 and "no" otherwise.
Example program: test if a number is between 10 and 20 (cont.) • Wrong solution: if ( 10 <= a <= 20 ) System.out.println("yes"); else System.out.println(“no");
Example program: test if a number is between 10 and 20 (cont.) Because 10 <= a <= 20 is evaluated as follows: It is illegal to use the compare <= operator between a Boolean value and a number Expression: 10 <= a <= 20 Operators: <= <= Evaluated as: 10 <= a <= 20 (10 <= a is either true or false) true <= 20 or false <= 20
Example program: test if a number is between 10 and 20 (cont.) • Correct solution: Because only numbers that are between 10 and 20 will satisfy the condition 10 <= a && a <= 20 if ( 10 <= a&&a <= 20 ) System.out.println("yes"); else System.out.println(“no");
Example program: test if a number is between 10 and 20 (cont.) import java.util.Scanner; public class Between01 { public static void main(String[] args) { int a; Scanner in = new Scanner(System.in); // Construct Scanner object a = in.nextInt(); // Read in number into a if ( 10 <= a && a <= 20 ) { System.out.println("Number is between 10 and 20"); } else { System.out.println("Number is NOT between 10 and 20"); } } } • Java program:
Example program: test if a number is between 10 and 20 (cont.) • Example Program: (Demo above code) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/Between01.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Between01.java • To run: java Between01
Priority of the logical operators • Priority ranking of the logical operators against the previously discussed operators:
Priority of the logical operators (cont.) • Reference: http://introcs.cs.princeton.edu/java/11precedence/
Priority of the logical operators (cont.) • Example 1: boolean a; Statement: a = 3 > 1 && 4 < 5 ; Operators in statement: = > && < Executed as follows: a = 3 > 1 && 4 > 5 ; // > and < have highest priority a = true && true ; a = true;
Priority of the logical operators (cont.) • Example 2: boolean a; Statement: a = 3 > 1 && ! 4 < 5 Operators in statement: = > && ! < Executed as follows: a = 3 > 1 && ! 4 < 5 // ! has the highest priority Result: error Logical NOT operator (!) cannot be applied to a number
Priority of the logical operators (cont.) boolean a; Statement: a = 3 > 1 && ! (4 < 5) Operators in statement: = > && ! ( < ) Executed as follows: a = 3 > 1 && ! (4 < 5); // ( ... ) has the highest priority a = 3 > 1 && ! true; // ! has the highest priority now a = 3 > 1 && false; // > has the highest priority now a = true && false; // && has the highest priority now a = false; • Example 3:
Programming example: Leap year using a complicated Boolean expression • Leap year description (Wikipedia): • In the Gregorian calendar, the current standard calendar in most of the world, most years that are evenly divisible by 4 are leap years. • Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years
Programming example: Leap year using a complicated Boolean expression (cont.) • Constructing the Boolean expression for "leap year": • most years that are evenly divisible by 4 are leap years: Year is leap year if: year % 4 == 0 (divisible by 4)
Programming example: Leap year using a complicated Boolean expression (cont.) • Years that are evenly divisible by 100 are not leap years: Year is leap year if: (year % 4 == 0) && !(year % 100 == 0) divisible by 4 AND not divisible by 100
Programming example: Leap year using a complicated Boolean expression (cont.) • unless they are also evenly divisible by 400, in which case they are leap years Year is leap year if: (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0)
Programming example: Leap year using a complicated Boolean expression (cont.) • Java program: import java.util.Scanner; public class LeapYear02 { public static void main(String[] args) { int year; boolean leap; Scanner in = new Scanner(System.in); // Construct Scanner object
Programming example: Leap year using a complicated Boolean expression (cont.) System.out.print("Enter year:"); year = in.nextInt(); // Read in year if ( (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0) ) { System.out.println("It is a leap year"); } else { System.out.println("It is NOT a leap year"); } } }
Programming example: Leap year using a complicated Boolean expression (cont.) • Example Program: (Demo above code) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/LeapYear02.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac LeapYear02.java • To run: java LeapYear02