Review of Previous Lesson 11/05/2019 Review of Previous Lesson State as many Vocabulary words and Learning Objectives that you remember from the last lesson as you can. Remember to grade yourself from 0 - 3.
Conditionals & Control Flow 11/05/2019 Conditionals & Control Flow Logical Operators
Language Features and other Testable Topics 11/05/2019 Tested in the AP CS A Exam Notes Not tested in the AP CS A Exam, but potentially relevant/useful Operators Logical: !, &&, || 1, 3 Control Statements if, if/else
Language Features and other Testable Topics 11/05/2019 Notes: 1. Students are expected to understand the operator precedence rules of the listed operators. 3. Students need to understand the “short circuit” evaluation of the && and ||
Topic Outline II. Program Implementation Expression evaluation 11/05/2019 Topic Outline II. Program Implementation Expression evaluation De Morgan’s law
Types of Operator in Java 11/05/2019 Types of Operator in Java Basic Arithmetic Operators Assignment Operators Unary +, -, Relational (comparison) operators Logical Operators Unary Auto-increment & Auto-decrement Operators Concatenation Bitwise Operators Ternary Operator Types 1 - 4 have been covered in previous presentations. Types 5 will be covered in this presentation.
Testing multiple Boolean Statement conditions 11/05/2019 Testing multiple Boolean Statement conditions The boolean condition has so far consisted of one test. A multiple boolean condition has two or more tests and each one is either true or false. For this you need to use logical operators.
Logical Operators &&, ||, ! 11/05/2019 Assume 2 boolean variables b1 and b2. b1&&b2 Returns true if both b1 and b2 are true else it would return false. b1||b2 Returns false if both b1 and b2 are false else it would return true. !b1 Returns the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true. Please note that in general logical operators && || are used with 2 different variables e.g. x & y. So if you find that you are writing x > … && x < …, you are most likely using the logical operator unnecessarily.
double rounding problems 11/05/2019 double rounding problems As discussed in the previous presentation basically avoid testing for equality using doubles. However, your syllabus expects you to be aware of this problem and answer questions regarding it. Possible solutions that are on your syllabus: Round manually. e.g. positive numbers with (int) (x + 0.5), negative numbers with (int) (x - 0.5). Shown in the previous presentation. Introduce some form of ‘tolerance’. See the next slide
Introducing the idea of ‘tolerance’ 11/05/2019 Introducing the idea of ‘tolerance’ double a = 0.7; double b = 0.9; double z = a + 0.1; // = 0.7999999999999999 but was expected to be 0.8! double y = b - 0.1; // = 0.8 as expected // Introduce the idea of a tolerance e.g. a 1000th double max = z + 0.001; double min = z - 0.001; System.out.println(y <= max && y >= min); // now true as expected
“Short Circuit” Evaluation 11/05/2019 && and || operators are short circuit operators. Don't necessarily evaluate all of its operands. int x = 0; int y = 4; if (x == 1 && y == 4) { …… } You might expect Java to ask itself if x = 1, and then ask if y = 4. But with &&, that's not what happens, instead, Java does the following: Evaluates x == 1 and discovers that x == 1 is false. Realises that the condition (x == 1 && whatever) can't possibly be true, no matter what the whatever condition happens to be. Returns false without bothering to check if y == 4. Remember, && wants both conditions, on its left and right sides, to be true. The same kind of thing happens with the || operator (another short circuit operator) when the value on the operator's left side is true.
Operator Precedence Rules Associativity unary + - (logical) ! not associative cast () right to left multiplicative * / % left to right additive + - relational < <= > >= equality == != logical && || assignment = += -= *= /= %= Most programmers do not memorize these, and even those that do still use parentheses for clarity, so I am not sure why the AP syllabus mentions them. Associativity. When an expression has two operators with the same precedence, the expression is evaluated according to its associativity. e.g. x = y = z = 17 is treated as x = (y = (z = 17)), leaving all 3 variables with the value 17, since the = operator has right-to-left associativity (and an assignment statement evaluates to the value on the right hand side). On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right associativity. Some operators are not associative: for example, the expressions (x <= y <= z) and x++-- are invalid. Decreasing Precedence
Ranks of Operators 11/05/2019 ! -(unary)(cast) * / % + - Highest ! -(unary)(cast) * / % + - < <= > >= == != && || Lowest
Truth Tables & Conventional Order 11/05/2019 Another way to show the evaluation of a boolean expression. Usually organized by putting the operands in the conventional order shown. Following this order helps prevent mistakes. Often False is written as 0 and True as 1. Operands Boolean Expressions x < 12 y > 10 x < 12 && y > 10 x < 12 || y > 10 1 ? ? ? ? ? ? ? ?
Conventional Order – 3 operands 11/05/2019 Conventional Order – 3 operands A B C - 1 A B C - F T ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? The conventional order is easy to remember if you think of F as 0 and T as 1.
A || B !B !A De Morgan's Law !B !A 11/05/2019 Augustus De Morgan was a nineteenth century British mathematician who showed the importance of several rules of logic. Two of these, De Morgan's Rules, show how the NOT operator can be moved to the inside of an expression. Although these rules are named after De Morgan, they were, in fact, known to Aristotle. !A || !B A&&B !B !A !(A && B) is equivalent to !A || !B !(A || B) is equivalent to !A && !B !(A && B) !(A || B) A || B !B !A e.g. A - Rich, B - Famous !A && !B
De Morgan's Law 11/05/2019 if ( !( (input == “quit”) || (count > limit)) ) { . . . } equivalent to: if ( !(input == “quit”) && !(count > limit)) ) { if ( !(input == “quit”) && (count <= limit)) ) { ? ?
11/05/2019 De Morgan's Law boolean oilOK = !( months >= 3 || miles >= 3000 ); equivalent to: boolean oilOK = !( months >= 3 ) && !( miles >= 3000 ); boolean oilOK = ( months < 3 ) && ( miles < 3000 ); ? ?
11/05/2019 Write your own programs: Write your own programs from “scratch”. Of course you should use previous programs for reference, but write your code from “scratch” (do not copy and paste).
double rounding problems 11/05/2019 Make one of the ways shown below work as expected by introducing the idea of ‘tolerance’ (as shown on slide 10). double d = 1.0; double e = 0.1; double f = 9 * e; // = 0.9 as expected d = d – f; // = 0.09999999999999998 but was expected to be 0.1! double x = 0.333333; System.out.println(3.0 == x * (3.0 / x)); // false but this would normally be expected to be always true! // Except for when x = 0 of course, in which case JVM would throw an arithmetic exception (see Data Types & Identifiers/Variables in Java).
Night Club Specification: 11/05/2019 Night Club Specification: A program to test conditions for customers waiting to enter into a club holding a ladies only night: age >= 18 gender == ‘F’ Each of these is either true or false. Use a Logical Expression. The program should produce one of these messages as appropriate: "Allow into nightclub." "Do not allow into nightclub!"
11/05/2019 Bowling Club Members of a ten-pin bowling club get an award if, during one season, they score at least 240 points on 5 or more occasions, or they score 200 points on 10 or more occasions. Use a Logical Expression.
11/05/2019 Hotel Write a program for a person wishing to attend an overnight conference: They cannot afford to pay more than €40.00 for their hotel but it must be no more than 3km from the conference hall. Use a Logical Expression. The program should ask for the cost per night and distance from the conference hall and then display a message stating whether the booking should be made or not. Extension: Give appropriate messages if the distance and the cost are not good (and what they should be), if the distance is good but the price is not (and what it should be) and vice versa.
Deciding Exam Grades Extended 11/05/2019 Deciding Exam Grades Extended Change the “Deciding Exam Grades” Program written in the previous presentation. To give a general error message. “You have entered an invalid mark!” Use a logical expression that is false when the mark is within the range and true when the mark is outside the range of 0 – 100 and use Or to produce the general error message above. Note: Make a copy of the previous program to keep the original program but rename.
11/05/2019 Work Hours For each employee the hours worked module collects data for five days. Each person can work up to 9 hours a day for up to 5 days a week. Use a logical expression. No-one may work more than 40 hours. If all the above requirements are met then the hours are added up and displayed.
11/05/2019 Winner or Winners Write a program to accept three marks for three candidates Candidate A, Candidate B and Candidate C. The program should display the winner (highest mark). Extension: Adapt the program to deal correctly with three or two of the candidates receiving equal marks. Hints: Test for clear winners first, then for three winners and then for two winners. Use a series of ElseIf statements. So as to avoid the chance of a correct winner being replaced by incorrect winners later on in the program.
5/11/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.