Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide4.pdfHistory...
Transcript of Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide4.pdfHistory...
History
•Java is developed by Sun Microsystems•Later, Oracle company acquires Java from Sun in 2010
•Based on C and C++•Work on most OS platforms
3
Sunday, July 3, 2011
Hello World
public class HelloWorld {public static void main(String[] args) {
System.out.println(“Hello World!”);}
}
4
Sunday, July 3, 2011
Java Virtual Machine (JVM)
6
HelloWorld.java
Java Complier
HelloWorld.class
JVM
JVM
JVM
Windows UNIXMac OS X
• JVM contains–CPU–Register–Stack–Memory area
Sunday, July 3, 2011
Variables
•Type Identifier [, Identifier]–int i,j,k;–boolean logic;–short a,b,c;–float f;–double d;
Sunday, July 3, 2011
Data Type
•Primitive– Data Type ชนิดตัวเลขคือ byte, short, int และ long
– Data Type ชนิดทศนิยมคือ float และ double
– Data Type ชนิดตัวอักษรคือ char
– Data Type ชนิดจริงและเท็จคือ boolean
• Reference
Sunday, July 3, 2011
การเขียนค่าในตัวแปร (Literals)
•Integer–ฐานสิบ 3, 3L
–ฐานแปด 08, 08L
–ฐานสิบหก 0xA, 0xAL
Sunday, July 3, 2011
Example of integer literalslong a = 2147483647; // ไม่เกิด error เมื่อมีการ Compile เพราะจํานวนตัวเลขไม่
เกินค่าที่ integer สามารถเก็บได้
long b = 2147483648; // เกิด error เพราะตัวเลขมีค่าเกินค่าสูงสุดที่ integer เก็บค่าได้
long c = 214748648L; // ไม่เกิด error เพราะมีการเขียน L ลงท้ายเพื่อบอกให้ตัวแปลภาษา เปลี่ยนค่า integer เป็น long
byte a = 3, b = 5;
a = a + b; // Error เมื่อ Compile เพราะ a+b เป็นการบวกแบบ 32 บิตตามข้อกําหนดของ Java ทําให้ไม่สามารถกําหนดให้ตัวแปร byte เท่ากับ integer
a = (byte) (a + b); // ถูกต้อง การที่กําหนดตัวแปร byte ให้เท่ากับ integer ต้องใส่ (byte) เพื่อ บอกให้ตัวแปรภาษาเปลี่ยนข้อมูลจากการบวก 32 บิตให้เท่ากับตัวแปร ขนาด 8 บิต ซึ่งอาจเสียความแม่นยํา (Loss of Precision) บางส่วน
Sunday, July 3, 2011
Floating point Literals
• การคํานวณตัวเลขทศนิยมในภาษา Java ใช้การคํานวณแบบ 64 บิตหรือ double ดังนั้นการใส่ค่าให้ตัวแปรที่เป็น float ต้องเขียนลงท้ายด้วย F หรือ f
• สําหรับการใส่ค่าให้ตัวแปรที่เป็น double ลงท้ายด้วย D หรือ d แต่ไม่จําเป็นต้องเขียนก็ได้ เพราะการคํานวณตัวเลขทศนิยมในภาษา Java โดยปกติเป็น double
• ตัวอย่างของ float literals คือ 2.0F 3.f 5e7f 0.4e+38f
• ตัวอย่างของ double literals คือ 2.0 3. 3e-7 .9E+200
• double m = 2.3; // ถูกต้อง• float f = 2.3; // ผิด ต้องเขียน f ลงท้ายตัวเลข• float k = 2.3f; //ถูกต้อง
• float a = 3; // ไม่เกิด error เมื่อมีการ Compile
• float b = 3.0f; // ถูกต้อง
Sunday, July 3, 2011
Char literals
• char ch = ‘c’; // เท่ากับ c
• ch = ‘A’; // เท่ากับ A
• ch = ‘\n’; // เท่ากับ ตัวอักษรขึ้นบรรทัดใหม่• ch = ‘\\’ // เท่ากับเครื่องหมาย \
• ch = ‘\’’; // เท่ากับ ‘
Sunday, July 3, 2011
String literals
String st = “สวัสดีครับ”;st = “สวัสดีครับ\n”; \\ ตัวอักษรในตารางสามารถนํามาเขียนเป็นประโยคได้
เช่น \n st = “”; \\ ตัวแปร st เป็น String ที่มีตัวอักษร 0 ตัว
try thisst = “\1 สบายดีไหมครับ \u3f07 \n”;\\ เครื่องหมาย + จะรวม 2 Strings เป็น String เดียว เรียกว่าการ Concatenation
string
st = “Hello” + “ How are you?”;
Sunday, July 3, 2011
Exercise 0
public class TestString { public static void main(String[] args) { System.out.println(“hello” + “ teacher”); }}
Sunday, July 3, 2011
Exercise 1
• Which of the following lines will compile without warning or error.
1) float f=1.3; 2) char c="a"; 3) byte b=257; 4) boolean b=null; 5) int i=10;
Sunday, July 3, 2011
Exercise 2
•Which of the following are legal identifiers 1) 2variable
2) variable2 3) _whatavariable 4) _3_ 5) $anothervar 6) #myvar
Sunday, July 3, 2011
Exercise 3
•Which of the following are legal statements? 1) float f=1/3;
2) int i=1/3;3) float f=1.01;4) double d=999d;
Sunday, July 3, 2011
Exercise 4
•Which of the following are Java keywords? 1) NULL
2) new3) instanceOf4) wend
Sunday, July 3, 2011
Exercise 5
•Choose the valid identifiers from those listed below.
A. BigOlLongStringWithMeaninglessNameB. $intC. bytesD. $1E. finalist
Sunday, July 3, 2011
Operator
•Arithmetic Operators•Relational and Conditional Operators•Shift and Logical Operators•Assignment Operators•Miscellaneous Operators
Sunday, July 3, 2011
Example
1. public class Arithmetic {2. public static void main(String[] args) {3. int a = 30, b = 20;4. double x = 100.99, y = 20.78;5. System.out.println("a = " + a); System.out.println("b = " + b); 6. System.out.println("a+b = " + (a+b));7. System.out.println("a-b = " + (a-b));8. System.out.println("a*b = " + (a*b));
9. System.out.println("a/b = " + (a/b)); // ผลหารที่เป็นจํานวนเต็ม10. System.out.println("a%b = " + (a%b)); // เศษที่เหลือจากการหาร11. System.out.println(""); 12. System.out.println("x = " + x);System.out.println("y= " + y);13. System.out.println("x+y = " + (x+y));14. System.out.println("x-y = " + (x-y));15. System.out.println("x*y = " + (x*y));
16. System.out.println("x/y = " + (x/y)); // ผลหาร17. System.out.println("x%y = " + (x%y)); // เศษที่เหลือจากการหาร18. } 19.}
Sunday, July 3, 2011
Output
• ---------- Java Output ----------• a = 30• b = 20• a+b = 50• a-b = 10• a*b = 600• a/b = 1• a%b = 10• x = 100.99• y= 20.78• x+y = 121.77• x-y = 80.21• x*y = 2098.5722• x/y = 4.859961501443696• x%y = 17.86999999999999
Sunday, July 3, 2011
Prefix and Postfix1.public class ShortcutOperator {2. public static void main(String[] args) {3. int i = 5;4. int j;5. System.out.println("i = " + i);6. j = i++; // j เท่ากับ 5 และ i เท่ากับ 67. System.out.println("j = i++ Value j = " + j );8. System.out.println("Value i now= "+ i);9. j = ++i; // j เท่ากับ 7 และ i เท่ากับ 710. System.out.println("j = ++i Value j = " + j );11. System.out.println("Value i now= "+ i);12. }13.}14.---------- Java Output ----------15.i = 516.j = i++ Value j = 517.Value i now= 618.j = ++i Value j = 7
Sunday, July 3, 2011
Exercise 6
•After execution of the code fragment below, what are the values of the variables x, a, and b?
1. int x, a = 6, b = 7;2. x = a++ + b++;
A. x = 15, a = 7, b = 8B. x = 15, a = 6, b = 7C. x = 13, a = 7, b = 8D. x = 13, a = 6, b = 7
Sunday, July 3, 2011
Exercise 7
•Answer result for this codeclass ABC { public static void main(String[] args) { int users=0; System.out.println(++users); System.out.println(users++); }}
Sunday, July 3, 2011
Example1. public class Relational {2. public static void main(String[] args) {3. int i = 10;4. int j = 20;5. System.out.println("i = "+ i + " j = " + j);6. System.out.println("i>j "+ (i>j));7. System.out.println("i<j "+ (i<j));8. System.out.println("i>=j "+ (i>=j));9. System.out.println("i<=j "+ (i<=j));10. System.out.println("i==j "+ (i==j));11. System.out.println("i!=j "+ (i!=j));12. }13.}14.---------- Java Output ----------15.i = 10 j = 2016.i>j false17.i<j true18.i>=j false19.i<=j true20.i==j false21.i!=j true
Sunday, July 3, 2011
Conditional Operator
• (i<3) && (j > 5) จะส่งค่าจริงกลับ ในกรณีที่ i < 3 และ j > 5
• (i<3) || (j>5) จะส่งค่าจริงกลับ ในกรณีที่ i < 3 หรือ j > 5
• !hello จะส่งค่าจริงกลับ ในกรณีที่ตัวแปร hello เป็นเท็จ เครื่องหมาย ! ส่วนใหญ่ใช้ในการทดสอบเงื่อนไขในคําสั่ง if
Sunday, July 3, 2011
Example1.public class Conditional {2. public static void main(String[] args) {3. int i = 5;4. int j = 10;5. boolean result;6. System.out.println("i = " + i);7. System.out.println("j = " + j);8. result = (i <=5 ) && (j>=10);9. System.out.println("(i <=5) && (j>=10) " + result);10. result = (i <6 ) || (j>20);11. System.out.println("(i <6) || (j>20) " + result);12. result = (i <3 ) && (j>2);13. System.out.println("(i <3) && (j>2) " + result);14. result = (i >6 ) || (j>20);15. System.out.println("(i >6) || (j>20) " + result);16. 17. boolean hello = true;18. System.out.println("Value of hello is " + hello);19. System.out.println("Value of !hello is " + !hello);
Sunday, July 3, 2011
Output
• ---------- Java Output----------• i = 5• j = 10• (i <=5) && (j>=10) true• (i <6) || (j>20) true• (i <3) && (j>2) false• (i >6) || (j>20) false•Value of hello is true•Value of !hello is false
Sunday, July 3, 2011
Shift Operators
•ใช้ได้กับข้อมูล –Integer, character, byte, short, long
•Right shift–Arithmetic shift–Logical shift
•Left shift
Sunday, July 3, 2011
Logical Operators
1111 0000Complement0000 1111
0100 1011 0100 1100 1110 1010 AND OR XOR 1101 1100 1011 0011 1010 1001 0100 1000 1111 1111 0100 0011
Sunday, July 3, 2011
การใช้ Logical right shift กับตัวแปร byte
1.byte b = -1; // 1111 11112.b = (byte) (b>>>10);3.System.out.println(b);
• What is a result?
Sunday, July 3, 2011
Revised
1.byte b = -2; // 1111 11102.b = (byte) ((b & 0xFF) >> 4); // 0000
1111
• ดังนั้นเครื่องหมาย >>> ไม่ควรใช้กับตัวแปร byte หรือ short เพราะอาจทําให้ค่าที่ได้ผิดพลาด
Sunday, July 3, 2011
Example of bitwise1. public class Bitwise {2. public static void main(String[] args) {3. int op1 = 0xffffffff;4. int op2 = 0x000000ff;5. System.out.println("op1 = " + Integer.toHexString(op1) );6. System.out.println("op2 = " + Integer.toHexString(op2) );7. System.out.println();8. System.out.println("op1 & op2 = " + Integer.toHexString(op1 & op2) );9. System.out.println("op1 | op2 = " + Integer.toHexString(op1 | op2) );10. System.out.println("op1 ^ op2 = " + Integer.toHexString(op1 ^ op2) );11. System.out.println("~op2 = " + Integer.toHexString(~op2) );12. }13.}14.---------- Java Output----------15.op1 = ffffffff16.op2 = ff17.op1 & op2 = ff18.op1 | op2 = ffffffff19.op1 ^ op2 = ffffff0020.~op2 = ffffff00
Sunday, July 3, 2011
Exercise 8
• Find result of this codeclass BitShift{ public static void main(String[] args) {
int x = 0x80000000; System.out.print(x + “ and “); x = x >>> 31; System.out.println(x); }}
Sunday, July 3, 2011
Exercise 10
int x = 10 | 5;int y = 3^4;System.out.println(x);System.out.println(y);
•What is the answer?
Sunday, July 3, 2011
Exercise 11
•Which of the following expressions results in a positive value in x?
A. int x = 1; x = x >>> 5;B. int x = 1; x = x >>> 32;C. byte x = 1; x = x >>> 5;D. int x = 1; x = x >> 5;
Sunday, July 3, 2011
Exercise 12
• What results from running the following code?1. public class Xor {2. public static void main(String args[]) {3. int b = 10; // 00001010 binary4. int c = 15; // 00001111 binary5. b = b ^ c;6. System.out.println( “b contains ” + b);7. }8. }
A. The output: b contains 10B. The output: b contains 5C. The output: b contains 250D. The output: b contains 245
Sunday, July 3, 2011
Assignment Operators
• เครื่องหมาย ? : ใช้ในการเปรียบเทียบเงื่อนไข รูปแบบการเขียนเป็น Condition ? Value when condition true : Value when condition false
1.System.out.println( 2>3 ? "Yes" : "No"); // ผลลัพธ์เป็น No
2.System.out.println( 2<3 ? "Yes" : "No"); // ผลลัพธ์เป็น Yes
3. int i = 100>1 ? 200: 300; // ดังนั้น i = 200
Sunday, July 3, 2011
CastingWhy casting?
1. int i = 100;2. long p = 1000L;3. p = i; // OK, Implicit Casting4. i = p; // Compile error ตัวแปร i มีขนาด 32 บิต ซึ่งเล็กกว่า
ตัวแปร p ที่มีขนาด 64 บิต5. i = (int) p; // OK, Explicit Casting
• byte à short à int à long à float à double • Can be implicit casting
Sunday, July 3, 2011
Casting
1. int i;2. double d = 3.141;3. i = (int) d; // Truncates the real value4. System.out.println (i); // Prints `3‘
Boolean can not do casting
Sunday, July 3, 2011
1.public class Casting {2. public static void main(String[] args) {3. byte b=10;4. short s=100;5. int i = 1000;6. long l = 10000L;7. float f = 200F;8. double d=2000;9. b = (byte) s;10. System.out.println(b +" From short to byte,it may cause loss precision");11. b = (byte) l;12. System.out.println(b +" From long to byte,it may cause loss precision");13. d = i;14. System.out.println(d + " From integer to double"); 15. f = s;16. System.out.println(f + " From short to float"); 17. }18.}19.---------- Java Output----------20.100 From short to byte,it may cause loss precision21.16 From long to byte,it may cause loss precision22.1000.0 From integer to double
Sunday, July 3, 2011
Exercise 13
•Will the following code compile?1. byte b = 2;2. byte b1 = 3;3. b = b * b1;
Sunday, July 3, 2011
Exercise 14
•Which of the following are legal lines of code?
A. int w = (int) 888.8; B. byte x = (byte) 1000L; C. long y = (byte) 100; D. byte z = (byte) 100L;
Sunday, July 3, 2011
Exercise 18
• Which one line in the following code will not compile?1. byte b = 5;2. char c = 5 ;3. short s = 55;4. int i = 555;5. float f = 555.5f;6. b = s;7. i = c;8. if (f > b)9. f = i;
Sunday, July 3, 2011
If-else syntax
if (Condition)
Statement A;
else
Statement B;
if (Condition)
Statement;
if (Condition1)
Statement A;
else if (Condition2)
Statement B;
else if (Condition3) Statement C;
else
Statement D;
if (Condition) {
Statement M; … Statement N;} else {
Statement X; … Statement Y;}
Sunday, July 3, 2011
1.public class Grade {2.public static void main(String[] args) {3.int score = 72;4.char grade;5. if (score >= 80) {6. grade = 'A';7. } else if (score >= 70) {8. grade = 'B';9. } else if (score >= 60) {10. grade = 'C';11. } else if (score >= 50) {12. grade = 'D';13. } else {14. grade = 'F';15. }16. System.out.println("Grade = " + grade);17. }18.}19.---------- Java Output----------20.Grade = B
Sunday, July 3, 2011
Exercise 19
•Write code to compute reduced price by using these conditions–if a customer buy
•<500 Baht; reduce 0%•500-5,000 Baht; reduce 10%•5,001-20,000 Baht; reduce 15%•>20,000 Baht; reduce 20%
–using Ex19.java to get started
Sunday, July 3, 2011
Switch Statement
switch (expression) { case constant1: Statement A; break; case constant2: Statement B; break; case constant3: Statement C; break; … case constant: Statement; break; default: Default Statement;}
Sunday, July 3, 2011
1.public class If {2. public static void main(String[] args){3. int i = 0;4. if (i == 4)5. System.out.println ("Four");6. else if (i == 3)7. System.out.println ("Three");8. else if (i == 2)9. System.out.println ("Two");10. else if (i == 1)11. System.out.println ("One");12. else13. System.out.println ("Not
interesting");14. }15.}
Sunday, July 3, 2011
1.class Switch {2. public static void main(String[] args){3. int i = 3;4. 5. switch (i) {6. case 4: 7. System.out.println ("Four"); break;8. case 3: 9. System.out.println ("Three"); break;10. case 2: 11. System.out.println ("Two"); break; 12. case 1: 13. System.out.println ("One"); break;14. default:15. System.out.println ("Not interesting");16. }17. }18.}19.---------- Java Output----------20.Three
Sunday, July 3, 2011
1.class Switch {2. public static void main(String[] args){3. int i = 3;4. 5. switch (i) {6. case 4: 7. System.out.println ("Four"); break;8. case 3: // Forget to put break statement after println9. System.out.println ("Three"); 10. case 2: 11. System.out.println ("Two"); break;12. case 1: 13. System.out.println ("One"); break;14. default:15. System.out.println ("Not interesting");16. }17. }18.}19.---------- Java Output----------20.Three21.Two
Sunday, July 3, 2011
Exercise 20• What would be the output from this code fragment?
1. int x = 0, y = 4, z = 5;2. if (x > 2) {3. if (y < 5) {4. System.out.println( “message one” );5. }6. else {7. System.out.println( “message two” );8. }9. }10. else if (z > 5) {11. System.out.println( “message three” );12. }13. else {14. System.out.println( “message four” );15. }
A. message oneB. message twoC. message threeD. message four
Sunday, July 3, 2011
Exercise 21• Which statement is true about the following code fragment?
1. int j = 2;2. switch (j) {3. case 2:4. System.out.println( “value is two” );5. case 2 + 1:6. System.out.println( “value is three” );7. break;8. default:9. System.out.println( “value is ” + j);10. break;11. }
A. The code is illegal because of the expression at line 5.B. The acceptable types for the variable j, as the argument to the switch() construct, could be
any of byte, short, int, or long.C. The output would be only the text value is two.D. The output would be the text value is two followed by the text value is three.E. The output would be the text value is two, followed by the text value is three, followed by
the text value is 2.
Sunday, July 3, 2011
Exercise 22
int i=4;switch(i) {
default: System.out.println(“Default”); case 1: System.out.println(“1”); break; case 4: System.out.println(“4”); break;
Sunday, July 3, 2011
Exercise 23
int i=4;switch(i) {
default: System.out.println(“Default”); case 1: System.out.println(“1”); break; case 3: System.out.println(“4”); break;
Sunday, July 3, 2011
Exercise 24• What will be printed out if you attempt to compile and run the following
code ? int i=1; switch (i) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); case 2: System.out.println("two"); default: System.out.println("default"); } 1) one
2) one, default 3) one, two, default 4) default
Sunday, July 3, 2011
For Loopfor( Initialization; Condition Expression; Step Expression) {
Statement;
}
1. for(int i=0; i<100; i++) {
2. System.out.println(i);
3. }
Sunday, July 3, 2011
1.public class ForLoop {
2. public static void main(String[] args){
3. int sum = 0;
4. for(int i=1, j=100; i<10; i++, j-=2 ){
5. System.out.println("i = " + i + " ,j = " + j);
6. sum = i + j;
7. System.out.println("i + j = " + sum);
8. }
9. }
10.}
---------- Java Output----------i = 1 ,j = 100i + j = 101i = 2 ,j = 98i + j = 100i = 3 ,j = 96i + j = 99i = 4 ,j = 94i + j = 98i = 5 ,j = 92i + j = 97i = 6 ,j = 90i + j = 96i = 7 ,j = 88i + j = 95i = 8 ,j = 86i + j = 94i = 9 ,j = 84i + j = 93
Sunday, July 3, 2011
1.// Find factorial of i2.public class WhileLoop {3. public static void main(String[] args){4. int fac=1;5. int i=5;6. System.out.println("i = " + i);7. while (i != 0){8. fac *= i--; 9. }10. System.out.println("Factorial = " + fac);11. }12.}13.---------- Java Output----------14.i = 515.Factorial = 120
Sunday, July 3, 2011
1.// Find factorial of i2.public class DoWhileLoop {3. public static void main(String[] args){4. int fac=1;5. int i=5;6. System.out.println("i = " + i);7. 8. do9. {10. fac *= i--; 11. } while (i > 1 );12. 13. System.out.println("Factorial = " + fac);14. }15.}16.---------- Java Output----------17.i = 518.Factorial = 120
Sunday, July 3, 2011
1. // Print even number from 2 to 20 except 102. public class BreakContinue {3. public static void main(String[] args) {4. int i=1;5. while (true){ 6. i++;7. if (i == 10) { // won't print 108. continue;9. } 10. else if (i > 20) { //if i is greater than 20 stop the loop11. break;12. }13. if (i%2 == 0) { // remainder is equal to zero?14. System.out.println(i);15. } // end if 16. } // end while17. } // end main18.} // end class19.---------- Java Output----------20.221.422.623.8
Sunday, July 3, 2011
Exercise 25
class Exp{ public static void main(String[] args) { int i = 10; int j = 12; if( (i<j) || (i++ == 3) ) System.out.println(“OK”); System.out.println(i);}• What value of i ?
Sunday, July 3, 2011
Exercise 26
• What’s wrong?1. for(int i=0; i<10; ) {2. if( i = 0) 3. System.out.println(“i equals 1”);4. i++;5. }
Sunday, July 3, 2011
Exercise 27• Create an application that loops from 1 to 50 and prints
each value on separate line. Also print “foo” for every multiple of 3, “biz” for every multiple of 5, and “baz” for every multiple of 7– 1– 2– 3 foo– 4– 5 biz– 6 foo– 7 baz– 8– 9 foo– 10 biz– 11– 12 foo – …
Sunday, July 3, 2011
Exercise 28• Which of the following lines of code will compile without error
1) int i=0; if(i) { System.out.println("Hello"); }2) boolean b=true; boolean b2=true; if(b==b2) { System.out.println("So true"); }3) int i=1; int j=2; if(i==1|| j==2) System.out.println("OK");4) int i=1; int j=2; if(i==1 &| j==2) System.out.println("OK");
Sunday, July 3, 2011
Exercise 29
• Consider the following code:1. for (int i = 0; i < 2; i++) {2. for (int j = 0; j < 3; j++) {3. if (i == j) {4. continue;5. }6. System.out.println( “i = ” + i + “ j == ” + j);7. }8. }Which lines would be part of the output?A. i = 0 j = 0B. i = 0 j = 1C. i = 0 j = 2D. i = 1 j = 0E. i = 1 j = 1F. i = 1 j = 2
Sunday, July 3, 2011