between references
2) Relational Operators (<
, >
)
<
>
<=
>=
==
!=
3) Testing with mod (%
)
18 % 5 // the remainder is ....
3
Test if a number is even ?
Test if a number is odd ?
1) Relational Operators in if
Statements
boolean isRaining = true;
System.out.println("BEFORE");
if (isRaining) {
System.out.println(" THEN BRANCH");
}
System.out.println("AFTER");
BEFORE THEN BRANCH AFTER
boolean isRaining = false;
System.out.println("BEFORE");
if (isRaining) {
System.out.println("THEN BRANCH");
}
System.out.println("AFTER");
BEFORE AFTER
if (x == y) // JAVA
if (x === y) // JAVASCRIPT
if (x = y) // BAD
boolean isRaining = true;
System.out.println("BEFORE");
if (isRaining) {
System.out.println(" THEN BRANCH");
} else {
System.out.println(" ELSE BRANCH");
}
System.out.println("AFTER");
BEFORE THEN BRANCH AFTER
boolean isRaining = false;
System.out.println("BEFORE");
if (isRaining) {
System.out.println(" THEN BRANCH");
} else {
System.out.println(" ELSE BRANCH");
}
System.out.println("AFTER");
BEFORE ELSE BRANCH AFTER
boolean condA = true;
boolean condB = false;
if (condA) {
if (condB) {
System.out.println("condA and condB");
} else {
System.out.println("only condA");
}
} else {
}
only condA
boolean condA = true;
boolean condB = true;
if (condA) {
if (condB) {
System.out.println("condA and condB");
} else {
System.out.println("only condA");
}
} else {
}
condA and condB
int x = -5;
if (x < 0) {
System.out.println("x is negative");
}
else if (x == 0) {
System.out.println("x is zero");
}
else {
System.out.println("x is positive");
}
x is negative
&&
), Or (||
), and Not (!
)¶||
¶a |
b |
a || b |
---|---|---|
false | false | false |
false | true | true |
true | false | true |
true | true | true |
!
¶a |
!a |
---|---|
false | true |
true | false |
!(true && false)
(!true) || (!(true || false))
!(!true)
!(true && (false || true))
!(!(!false))
true && (!(!((!false) && true)))
false || (false || ((true && true) && (true && false)))
x
,y
give true
?¶NOTE: there can be many combinations that produce true
, find them all
x || (!x)
(!x) && (!y)
x && (y || y)
x && (!y)
(!x) || y
y || !(y && x)
x && ((!x) || !(y))
(!(!x)) && !(x && y)
x && (x || (!(x) || !(!(x || !(x)))))
x
,y
give false
?¶QUESTION: For each of these expressions, for which values of x
&& y
they give false
?
NOTE: there can be many combinations that produce false
, find them all
x || ((!y) || z)
x || (!y) || (!z)
!(x && y && (!z))
!(x && (!y) && (x || z))
y || ((x || y) && (!z))
false && superComplicatedBooleanExpression
true && superComplicatedBooleanExpression
a |
b |
a && b |
---|---|---|
false | false | false |
false | true | false |
true | false | false |
true | true | true |
false || superComplicatedBooleanExpression
true || superComplicatedBooleanExpression
a |
b |
a || b |
---|---|---|
false | false | false |
false | true | true |
true | false | true |
true | true | true |
Let's pick an error:
1/0
---------------------------------------------------------------------------
java.lang.ArithmeticException: / by zero
at .(#119:1)
Let's create a faulty boolean expression:
1/0 == 5
---------------------------------------------------------------------------
java.lang.ArithmeticException: / by zero
at .(#119:1)
true && (1/0 == 5)
false && (1/0 == 5)
true || (1/0 == 5)
false || (1/0 == 5)
Write some code which puts true
in boolean var isSecB
if the second character of the string s
is the character 'B'
. Otherwise, stores false
.
if
statements// 01234
String s = "ABACO"; // true
// String s = "BIRBA"; // false
// write here
boolean isSecB =
System.out.println(isSecB);
true || (1/0==5 && true)
(!false) || !(1/0==5)
true && 1/0==5 && true
(!true) || 1/0!=5 || true
true && (!true) && 1/0==5
1) De Morgan’s Laws
!(a && b)
is equivalent to !a || !b
!(a || b)
is equivalent to !a && !b
2) Truth Tables
3) Simplifying Boolean Expressions
Look at following expressions, and try to rewrite them in equivalent ones by using De Morgan laws, simplifying the result wherever possible. Then verify the translation produces the same result as the original for all possible values of x
&& y
.
(!x) || y
(!x) && (!y)
(!x) && (!(x || y))
Example:
boolean x = false, y = false;
//boolean x = false, y = true;
//boolean x = true, y = false;
//boolean x = true, y = true;
boolean orig = x || y;
boolean trans = !((!x) && (!y));
System.out.println("orig=" + orig);
System.out.println("trans=" + trans);
==
vs equals
import java.util.Date; // (note this class is actually deprecated)
Date d1 = new Date(121,0,28);
Date d2 = new Date(121,0,28);
System.out.println(d1 == d2); // ?
System.out.println(d1.equals(d2)); // ?
import java.util.Date;
Date d1 = new Date(121,0,28);
Date d2 = d1;
System.out.println(d1 == d2); // ?
System.out.println(d1.equals(d2)); // ?
import java.util.Date;
Date d1 = new Date(121,0,28);
Date d2 = new Date(682,7,14);
System.out.println(d1 == d2); // ?
System.out.println(d1.equals(d2)); // ?
import java.util.Date;
Date d1 = new Date(121,0,28);
Date d2 = new Date(121,0,28);
d2.setYear(300);
System.out.println(d1 == d2); // ?
System.out.println(d1.equals(d2)); // ?
2) Equality with New Strings
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
false true
// WARNING: Java caches strings !
// This very particular behaviour happens *ONLY* because we have strings !!
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
true true
Integer i1 = new Integer(3);
Integer i2 = new Integer(3);
System.out.println(i1 == i2); // ?
System.out.println(i1.equals(i2)); // ?
Integer i1 = 3; // autoboxed
Integer i2 = 3;
System.out.println(i1 == i2); // ?
System.out.println(i1.equals(i2)); // ?
a.equals(b)
You have a map of an island where zones in which a treasure could be are marked in green (ignore other colors). A drone which can land and drill the terrain. Strong winds could move the drone away from the target, so the drone needs to know whether or not is on a zone it should drill. Given the map side length d
and two coordinates x
and y
, RETURN true
if the place is to drill (that is, the drone is on a green zone), otherwise return false
. ASSUME THE ORIGIN (0,0)
IS AT THE CENTRE OF THE MAP
public class MyClass {
private static int d = 10;
/** RETURN true if the place is to drill, otherwise return false. */
public static boolean to_drill(double x, double y){
throw new UnsupportedOperationException("TODO IMPLEMENT ME!");
}
public static void check(boolean expected, boolean actual){
if (expected != actual){
throw new AssertionError("ERROR! Expected: " + expected + " Actual: " + actual);
}
}
public static void main(String[] args) {
// inner radius
check(true, to_drill(0,0));
check(true, to_drill(0.2*d,0));
check(true, to_drill(0,0.1*d));
check(true, to_drill(0,-0.03*d));
check(true, to_drill(-0.01*d,-d*0.05));
// corona
check(false, to_drill(0,-0.3*d));
check(false, to_drill(0.35*d,0));
check(false, to_drill(0.4*d,0.27*d));
check(false, to_drill(0.31*d,-0.4*d));
check(false, to_drill(-0.31*d,0.4*d));
check(false, to_drill(-0.3*d,-0.38*d));
// corners
check(true, to_drill(0.49*d,0.49*d));
check(false, to_drill(0.45*d,-0.46*d));
check(false, to_drill(-0.48*d,0.45*d));
check(true, to_drill(-0.49*d,-0.47*d));
}
}
---------------------------------------------------------------------------
java.lang.UnsupportedOperationException: TODO IMPLEMENT ME!
at MyClass.to_drill(#38:1)
at MyClass.main(#38:1)
at .(#39:1)
public class Magpie
{
public String getGreeting() {
return "Hello, let's talk.";
}
public String getResponse(String statement) {
String response = getRandomResponse();
return response;
}
private String getRandomResponse() {
String response = "Interesting, tell me more.";
return response;
}
private void answer(String msg) {
System.out.println(">" + msg);
System.out.println(this.getResponse(msg));
}
public static void check(boolean expected, boolean actual) {
}
public static void main(String[] args) {
Magpie maggie = new Magpie();
System.out.println(maggie.getGreeting());
maggie.answer("My mother and I talked last night.");
}
}