Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I...
Transcript of Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I...
![Page 1: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/1.jpg)
![Page 2: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/2.jpg)
Logical and Relational OperationSession 5
Course : T-0974 Algorithm & Object-Oriented Programming IYear : 2010
![Page 3: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/3.jpg)
Bina Nusantara
Learning Outcomes
After taking this course, student should be expected to apply and demonstrate using Relational and comparator operator, Logical operator and bitwise operator.
![Page 4: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/4.jpg)
Bina Nusantara
Lecture outline
• Relational & Comparator procedure• Relational & Comparator operator• Logic & Boolean procedure• Logic & Boolean operator• Truth table• Bitwise Operation
![Page 5: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/5.jpg)
Bina Nusantara
Relational procedure
• Relational procedure == Comparator procedure.• Comparing two values.• Using 6 relational/comparator operator.• The result has Boolean type.• The values have number, ASCII, or String data
type.
![Page 6: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/6.jpg)
Bina Nusantara
Comparator operator
Operator
Description Example
Result
< less than 1 < 2 true
<= less than or equal to 1 <= 2 true
> greater than 1 > 2 false
>= greater than or equal to
1 >= 2 false
== equal to 1 == 2 false
!= not equal to 1 != 2 true
![Page 7: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/7.jpg)
Bina Nusantara
Comparator operator
• Character can be compared by refering to its ASCII number. – Example: ‘a’ is more bigger than ‘A’ because ASCII
number for ‘a’ (97) is bigger than ASCII number for ‘A’ (65).
• Word can be compared by String helper.• Comparator operator is different than
asssignment operator.X = 14 store 14 to X.X == 14 compare if X is equal to 14.
![Page 8: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/8.jpg)
Bina Nusantara
Comparator Operation
![Page 9: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/9.jpg)
Bina Nusantara
Logic/Boolean Procedure
• Logical procedure == Boolean procedure.• Execute 1 or 2 logic values.• It has 4 types Logic/Boolean operator.• The result has Boolean data type.• Truth table
![Page 10: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/10.jpg)
Bina Nusantara
Boolean Operator
Operator
Name Description
! not logical negation&& and logical
conjunction|| or logical
disjunction^ exclusive
orlogical exclusion
![Page 11: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/11.jpg)
Bina Nusantara
NOT (!)
p !p Exampletrue false !(1>2) is true, because (1>2) is
falsefalse true !(1>0) is false, because (1>0) is
true• Operator not (!) inverts the original value.• true false and false true
![Page 12: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/12.jpg)
Bina Nusantara
AND (&&)
p1 p2 p1 && p2
Example
false false false (2>3) && (5>5) is falseBecause both (2>3) and (5>5) are false
false true false (2>3) && (6>5) is falseBecause (2>3) is false
true false false (6>5) && (2>3) is falseBecause (2>3) is false
true true true (3>2) && (5>=5) is trueBecause both (3>2) and (5>=5) are true
• AND Operator (&&) is true when all of its operands are true.
• If one of its operand is false, then AND is false.
![Page 13: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/13.jpg)
Bina Nusantara
OR (||)
p1 p2 p1 || p2 Example
false
false
false (2>3) || (5>5) is falseBecause both (2>3) and (5>5) are false
false
true true (2>3) || (6>5) is trueBecause (6>5) is true
true false
true (6>5) || (2>3) is trueBecause (6>5) is true
true true true (3>2) || (5>=5) is trueBecause both (3>2) and (5>=5) are true
• OR (||) Operator is true if one of every its operand is true.
• If all of its operands become false then OR is false.
![Page 14: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/14.jpg)
Bina Nusantara
XOR (^)
p1 p2 p1 ^ p2
Example
false
false
false (2>3) ^ (5>5) is falseBecause both (2>3) and (5>5) are false
false
true true (2>3) ^ (6>5) is trueBecause (2>3) is false and (6>5) is true
true false
true (6>5) ^ (2>3) is trueBecause (6>5) is true and (2>3) is false
true true false (3>2) ^ (5>=5) is trueBecause both (3>2) and (5>=5) are true
• Operator XOR (^) is true when both if its operands has different condition.
• When its operands has same condition then XOR is false.
![Page 15: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/15.jpg)
Bina Nusantara
Tabel Kebenaran: Demo
![Page 16: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/16.jpg)
Bina Nusantara
Thruth Table: Demo
![Page 17: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/17.jpg)
Bina Nusantara
Thruth table: Leap Year
![Page 18: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/18.jpg)
Bina Nusantara
Did You Know?
• When evaluating p1&&p2, Java evaluates p1 first.– If p1 is true then Java evaluates p2.– If p2 is false then Java doesn’t evaluate p2.
• When evaluating p1||p2, Java evaluates p1 first– If p1 is true then Java doesn’t evaluate p2.– If p1 is false then Java evaluates p2.
![Page 19: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/19.jpg)
Bina Nusantara
Advanced Learning
• Bit : the smallest unit in data.• 1 byte = 8 bits.• 1 bit has 0 or 1.• Java support bitwise operator to do bit
shift.• Example :
0000 0000 0 0000 1000 80000 0001 1 0001 0000 160000 0010 2 0010 0000 320000 0011 3 0100 1101 770000 0100 4 1111 1111 255
![Page 20: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/20.jpg)
Bina Nusantara
Advanced Learning
• Bit’s values is same as boolean– 1 : true– 0 : false
• Its procedure is same as logical procedure.& && (AND)| || (OR)^ ^ (XOR)~ ! (NOT/NEGATE)
• Additional operator<< : shift left (unsigned)>> : shift right (signed)>>> : shift right (unsigned)
![Page 21: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/21.jpg)
Bina Nusantara
Advanced Learning
Operator Nama Example Deskripsi
& AND 1010 1110 & 1001 0010Result: 1000 0010
If both of its bits is 1 then it produces 1.
| OR 1010 1110 | 1001 0010Result: 1011 1110
If one of its bits is 1 then it produces 1.
^ XOR 1010 1110 ^ 1001 0010Hasil: 0011 1110
If both of its bits have different values then it produces 1.
~ NEGATE ~1010 1110Hasil: 0101 0001
If bit = 1 0If bit = 0 1
<< LEFTSHIFT
1010 1110 << 2Hasil: 1011 1000
Shift 2 bit to the left then fill the latter with zeros.
>> RIGHTSHIFT
(SIGNED)
1010 1110 >> 2Hasil: 1110 10110010 1110 >> 2Hasil: 0000 1011
Shift 2 bit to the right then fill the latter with zeros. Its Least Significant Bit is sign bit.
>>> RIGHTSHIFT
(UNSIGNED)
1010 1110 >>> 2Hasil: 0010 1011
Shift 2 bit to the right then fill the latter with zeros.
![Page 22: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/22.jpg)
Bina Nusantara
Advanced Learning
• Shorthand for bitwise operation :&=, |=, ^=, <<=, >>=, >>>=
• Bitwise only can be used for real number like : byte, short, int and long.
• Bitwise for Char type converts to int which refer to ASCII number.
![Page 23: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/23.jpg)
Bina Nusantara
Advanced Learning
![Page 24: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/24.jpg)
Bina Nusantara
Advanced Learning
![Page 25: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/25.jpg)
25
Latihan• Buatlah program menerima input Nama, Total
belanja• Jika belanja >100rb, maka disc 10% selain itu tidak ada discount. (gunakan kelas Scanner)
Bina Nusantara University
![Page 26: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/26.jpg)
26
import java.util.*;class Belanja{public static void main(String args[]){String nama;boolean member=true;double belanja=0.0;Scanner in =new Scanner(System.in)System.out.println("Masukkan Nama");nama=in.nextLine();System.out.println("Masukkan Belanja");belanja=in.nextInt();
Bina Nusantara University
if ((belanja >100000|| member==true) && belanja <500000){belanja =belanja -(belanja*0.1);System.out.println ("Total belanja"+ belanja);}elseif (belanja >=500000){belanja =belanja -(belanja*0.2);System.out.println ("Total belanja"+ belanja);}
else{System.out.println ("Total belanja"+ belanja);}}}
![Page 27: Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c745503460f94926d6c/html5/thumbnails/27.jpg)
Bina Nusantara
References
• Introduction to Java Programming. 6ed. Liang. 2007. p94-98, p1279
• Dasar Pemrograman Java 2. Abdul Kadir. 2004. p76-83
• The Complete Reference Java. 5ed. Herbert Schildt. 2005. p62-76
• Java 2 Weekend Crash Course. Julio Sanchez. 2002. p85-96
• Bitwise:– http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.
html– http://www.sap-img.com/java/java-bitwise-logical-operators.ht
m– http://www.leepoint.net/notes-java/data/expressions/bitops.htm
l– http://www.javabeginner.com/java-operators.htm