ทส 213  การเขียนโปรแกรมเชิงวัตถุ 1

39
[email protected] ทท213 ททททททททททททททททททท ททททท 1 Flow Control: if & switch-case ทททททททททททททท ทททททททททท ททททททททททททททททททททททททท ทททททททททททททท www.itsci.mju.ac.th ทท213ทททททททททททททททททททททททท 1 Object-Oriented Programming 1

Upload: seth-emerson

Post on 30-Dec-2015

34 views

Category:

Documents


6 download

DESCRIPTION

ทส 213 การเขียนโปรแกรมเชิงวัตถุ 1 Object-Oriented Programming 1. ทส 213  การเขียนโปรแกรมเชิงวัตถุ 1. อาจารย์อรรถวิท ชังคมานนท์ สาขาวิชาเทคโนโลยีสารสนเทศ คณะวิทยาศาสตร์ www.itsci.mju.ac.th. Flow Control: if & switch-case. Selection: if statement. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

ทส213  การเขียนโปรแกรมเชิ�งวั�ตถุ�1

Flow Control:

if & switch-case

อาจารย�อรรถุวั�ท ชิ�งคมานนท�สาขีาวั�ชิาเทคโนโลยสารสนเทศ

คณะวั�ทยาศาสตร�www.itsci.mju.ac.th

ทส213การเขียนโปรแกรมเชิ�งวั�ตถุ� 1- 1ObjectOr i ent ed Pr ogr ammi ng

Page 2: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Selection: if statement

Syntax: if sttement ในกรณท#มทางเล$อกเดียวัif (condition) statement

condition

statement

true

false

เป็�น Statements ที่��ใช้�ในการตั�ดสิ�นใจ โดยขึ้��นอย��ก�บผลล�พธ์"ในการป็ระเมิ�นผลจากเงื่&�อนไขึ้ที่��ก(าหนดไว้�เป็�น true

IF statement จะยอมิให�การที่(างื่านสิามิารถขึ้�ามิไป็ย�งื่ statement ที่��ตั�องื่การ โดยขึ้��นอย��ก�บผลล�พธ์"ขึ้องื่เงื่&�อนไขึ้ที่��ถ�กก(าหนดไว้�

หากเงื่&�อนไขึ้แบบ Boolean เป็�นจร�งื่ การป็ระมิว้ลผลขึ้องื่ statement จะเก�ดขึ้��นตัามิล(าด�บ แตั�หากเป็�นเที่-จจะขึ้�ามิไป็โดยอ�ตัโนมิ�ตั�

Page 3: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

If statement (2)

โดียปกต�แล&วั IF statement จะมขีอบเขีตการท(างานไดี&เพียง 1 statement ดี�งน�+นในกรณท#ต&องการท(างานก�บหลาย ๆ statement จะต&องท(าการก(าหนดีขีอบเขีตดี&วัยเคร$#องหมายป.กกาล&อมรอบไวั&ใน state

ments ต/าง ๆ เชิ/น :

if (condition) {

Statement1;Statement2;…

}

Page 4: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

public class if1 {

private int temperature;

public if1(int temp) { temperature = temp; }

public int getTemp() { return temperature; }

public void testif () {

if (temperature < 30)

System.out.println("Nice day");

System.out.println("Let goes to the beach");

}

public static void main(String[] args) {

if1 temp = new if1(30);

System.out.println("Temperature = " + temp.getTemp());

temp.testif();

}

}

Page 5: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

If statement (3)

ในกรณท#เป0นการเล$อกแบบสองทางจะมร1ปแบบดี�งน+if (condition)

statementT

else statementF

หากการประเม�นผลเง$#อนไขีเป0น FALSE statement ล(าดี�บถุ�ดีจาก else จะท(างานท�นท

condition

statementtrue

false

statement

Page 6: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

public class if2 {

private int temperature;

public if2(int temp) { temperature = temp; }

public int getTemp() { return temperature; }

public void testif () {

if (temperature < 28)

System.out.println("Nice day");

else

System.out.println("Hot day");

}

public static void main(String[] args) {

if2 temp = new if2(25);

System.out.println("Temperature = " + temp.getTemp());

temp.testif();

}

}

Page 7: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

More Traps

a = 0 เป0นการก(าหนดีค/า (assignment) a == 0 เป0นการเปรยบเทยบ (relational) if (a = 0) Stmt แบบน+คอมไพีเลอร�จะท(าการแจ&งขี&อผ�ดีพีลาดี floats และ doubles ไม/สามารถุน(ามาเปรยบเทยบก�นไดี&โดียตรง เชิ/น

double x = 6.4, y = 8.6;if (x + y == 15.) // การประเม�นผลเง$#อนไขีจะเป0น failif (Math.abs (x + y – 15.) < .0001) //will achieve the desired effect

Page 8: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

If statement (4)

ในกรณขีองล1ปเชิ�งซ้&อนจะต&องระม�ดีระวั�งการประมวัลผลเง$#อนไขีดี&วัย:

if (Condition1)

((((((((((( ( 2

2Statement ;else

Statement3;

Page 9: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

public class if3 {

private int temperature;

public if3(int temp) { temperature = temp; }

public int getTemp() { return temperature; }

public void testif () {

if (temperature < 30)

if (temperature > 25)

System.out.println("Nice day");

else

System.out.println("Cold day");

}

public static void main(String[] args) {

if3 temp = new if3(32);

System.out.println("Temperature = " + temp.getTemp());

temp.testif();

}

}

Page 10: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Traps else pairs with nearest unmatched if

if (a > 10)

if (b > 12)

statement1;

else

statement2;

statement3;

if (a > 10){ if (b > 12) { statement1; }}else { statement2;}statement3;

Matching rule: ภายใน nested if-else statement else is matched with the closest preceding if that is not matched yet.

Page 11: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

If statement 5( )

1if (Condition )

( 2 )if Condi t i on 1Statement ; ((((

2Statement ; else 3 ;Statement

เร��มิตั�น

1เงื่&�อนไ ขึ้

stmt 2

temp input

จบการที่(างื่าน

จร�งื่

2เงื่&�อนไ ขึ้

stmt 1

จร�งื่

stmt 3

เที่-จ

เที่-จ

Page 12: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

public class if4 {

private int temperature;

public if4(int temp) { temperature = temp; }

public int getTemp() { return temperature; }

public void testif () {

if (temperature < 30)

if (temperature > 25)

System.out.println("Nice day");

else

System.out.println("Cold day");

else

System.out.println("Hot day");

}

public static void main(String[] args) {

if4 temp = new if4(28);

System.out.println("Temp = " + temp.getTemp());

temp.testif();

}

}

Page 13: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Conditional Assignment

int num = 5; int count;

count = num > 3 ? 1 : 0;

การป็ระมิว้ลผลจะอย��ในร�ป็ True หร&อ False

<boolean> ? <true condition> : <false condition>

Page 14: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

public class condition {

private int num;

boolean count;

public condition(int number) {

num = number;

}

public boolean getCount(){

return count;

}

public void testCond () {

count = num > 3 ? true:false;

}

public static void main(String[] args) {

condition c = new condition(2);

c.testCond();

System.out.println("Count is " + c.getCount());

}

}

Page 15: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

If statement 6( ) ในบางกรณอาจใชิ& if-then-else ร/วัมก�นในล�กษณะท#น(ามาซ้&อนก�นไดี&

1if (Condition ) 1Statement ; else 2if (Condition ) 2Statement ;

เร��มิตั�น

if (เงื่&�อนไขึ้)

else if stmt

if stmt

จบการที่(างื่าน

จร�งื่

else if(เงื่&�อนไขึ้)

else stmt

จร�งื่

else if stmt else if(เงื่&�อนไขึ้)

จร�งื่

Page 16: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Multiple-Alternatives

if (grade >= 90)

System.out.println("A");

else if (grade >= 80)

System.out.println("B");

else if (grade >= 70)

System.out.println("C");

else if (grade >= 60)

System.out.println("D");

else

System.out.println("F");

Exam score Grade Assigned

90 & above A80-89 B70-79 C60-69 DBelow 60 F

Page 17: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Multiple-Alternatives

if (grade >= 90)

System.out.println("A");

else if (grade >= 80)

System.out.println("B");

else if (grade >= 70)

System.out.println("C");

else if (grade >= 60)

System.out.println("D");

else

System.out.println("F");

if (grade >= 60)System.out.println("D");

else if (grade >= 70)System.out.println("C");

else if (grade >= 80)System.out.println("B");

else if (grade >= 90)System.out.println("A");

elseSystem.out.println("F");

Page 18: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Boolean Operatorscombine boolean values

b1 && b2 true ในกรณี�ที่��ที่� �งื่ b1 and b2 เป็�น true

b1 || b2true หาก b1 or b2 ค่�าใดค่�าหน��งื่เป็�น true

b1 ^ b2true เฉพาะ b1 หร&อ b2 ค่�าเด�ยว้เที่�าน��นที่��เป็�น true

!b1True หาก b1 ไมิ�เป็�น true

if (a > b && c != 7)

if (a < 0 || a > 10)

if (a == 5 ^ b == 5)

if (!person.isFemale())

Page 19: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Solution pH:

Equivalent if -else if format

if (condition1 )statement1

else if (condition2 )statement2

else if (condition3 )statement3

else if (conditionn )statementn

elsestatementn+1

.

.

.

very acidic

pH < 3true

false

acidic

pH<7true

false

neutral

pH ==7true

false

alkaline

pH<12true

false

very alkaline

pH>=3

pH>=7

pH >7

pH>=12

Page 20: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

if (pH < 3)System.out.println("Solution is very acidic.");

else if (pH < 7)System.out.println("Solution is acidic.");

else if (pH == 7)System.out.println("Solution is neutral.");

else if (pH < 12)System.out.println("Solution is alkaline.");

elseSystem.out.println("Solution is very alkaline.");

Page 21: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

3 7 12 pH

neutralacidicvery

acidicalkaline very

alkaline

if (pH >= 12)System.out.println("Solution is very alkaline.");

else if (pH > 7)System.out.println("Solution is alkaline.");

else if (pH == 7)System.out.println("Solution is neutral.");

else if (pH >= 3)System.out.println("Solution is acidic.");

elseSystem.out.println("Solution is very acidic.");

Page 22: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Decision table for a payroll system

Salary range Base tax Percentage of excess

0.00 - 1,499.99 0.00 15 %1,500.00 - 2,999.99 225.00 16 %3,000.00 - 4,999.99 465.00 18 %5,000.00 - 7,999.99 825.00 20 %8,000.00 and over 1425.00 25 %

Page 23: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Decision table for a payroll system

if (salary < 0.00) // error checkingSystem.out.println("Error: negative salary $" +

salary);else if (salary < 1500.00) // first range

tax = 0.15 * salary;else if (salary < 3000.00) // second range

tax = 225.00 + (salary - 1500.00) * 0.16;else if (salary < 5000.00) // third range

tax = 465.00 + (salary - 3000.00) * 0.18;else if (salary < 8000.00) // fourth range

tax = 825.00 + (salary - 5000.00) * 0.20;else // fifth range

tax = 1425.00 + (salary - 8000.00) * 0.25;

Page 24: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Nested if statement with independent conditions

apply to first choice college

earnings>2000false true

apply to local college SAT>1300

false true

apply to parents alma mater

Page 25: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

braces { } not necessary

Nested if statement with independent conditions

if (earnings > 2000.00) {if (SAT > 1300)

System.out.println("Apply to first choice college");else

// SAT <= 1300System.out.println("Apply to parents alma mater");

} else// earnings <= 2000System.out.println("Apply to local college");

Page 26: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Compare two date objects d1 and d2 (year, month, day)

d1.m ==d2.m

d1 after d2d1 before d2

d1.y <d2.ytrue false

d1.y ==d2.ytrue false

trued1.m <d2.m

false

true false

d1 before d2 d1 after d2true false

d1.d <d2.d

d1d ==d2.dtrue false

d1 after d2d1 before d2 d1 equal to d2

Page 27: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

if (d1.year < d2.year) // first date < second dateSystem.out.println("The first date is before the second.");

else if (d1.year == d2.year) // must compare months, daysif (d1.month < d2.month) // first date < second date

System.out.println("The first date is before the second.");

else if (d1.month == d2.month) // must compare daysif (d1.day < d2.day) // first date < second date

System.out.println("The first date is before the second.");else if (d1.day == d2.day)

// dates are equalSystem.out.println("The two dates are equal.");

else// here (d1.day > d2.day)System.out.println(" The first date is after the second.");

else// here (d1.month > d2.month)System.out.println(" The first date is after the second.");

else// here (d1.year > d2.year)System.out.println(" The first date is after the second.");

Page 28: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Switch statement

Switch statement syntax:switch (selector ) { case label11: case label12: . . . case label1k: statements1

break; . . .

case labeln1: case labeln2: . . . case labelnm: statementsn

break; default: statementsd

}

Page 29: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Switch statement

ค่�าขึ้องื่ selector จะถ�กเป็ร�ยบเที่�ยบก�บค่�าขึ้องื่ label ที่�ละค่�าในแตั�ละรอบขึ้องื่การที่(างื่าน

เมิ&�อผลการเป็ร�ยบเที่�ยบมิ�ค่�าเที่�าก�นใน statements ที่��ถ�กอ�างื่ถ�งื่ใน label น��น ๆ จะถ�ก ป็ระมิว้ลผลไป็จนกว้�าจะถ�งื่ค่(าสิ��งื่ break หร&อ return ซึ่��งื่จะมิ�ผล

ที่(าให� statements อ&�น ๆ ใน switch block ถ�กขึ้�ามิไป็ หากผลการเป็ร�ยบเที่�ยบไมิ�เที่�าก�น statements ที่��อย��ใน default จะถ�ก

ป็ระมิว้ลผล default จะเป็�นที่างื่เล&อก หากไมิ�ได�ที่(าการระบ4ไว้�จะไมิ�มิ�การป็ระมิว้ลผลใด ๆ

เก�ดขึ้��นเมิ&�อผลการเป็ร�ยบเที่�ยบไมิ�เที่�าก�น หากค่(าสิ��งื่ break (return) statement ไมิ�ได�มิ�การระบ4ไว้� การป็ระมิว้ลผล

จะกระที่(าตั�อไป็ย�งื่ labels ถ�ดไป็ตัามิล(าด�บ ช้น�ดขึ้�อมิ�ลขึ้องื่ที่��งื่ selector และ labels จะตั�องื่เป็�นแบบ ordinal type

( ใน Java, ordinal types จะได�แก�: integer types, bool และ char)

Page 30: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

switch Statement

switch (expression){case value1:

// do somethingbreak;

case value2:// do somethingbreak;

case value3:// do somethingbreak;

default:// do something

}

switch (paycode)

{

case 2:

exempt = false;

break;

case 4:

case 6:

case 7:

exempt = true;

break;

default:

System.out.println

("Bad paycode");

}

Page 31: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Use of Constants in switch statements

switch (gender){case 1: … break;case 2: … break;}

final int MALE = 1;

final int FEMALE = 2;

switch (gender) {

case MALE:

break;

case FEMALE:

break

}

ช้น�ดขึ้องื่ขึ้�อมิ�ลในเงื่&�อนไขึ้ขึ้องื่ switch : byte, char, short, int เที่�าน��น !!!

Page 32: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Switch statement examples// Set price for a light bulb of// 40, 60, 75, 100 or 150 wattsswitch (watts) {case 40:

price = 0.50;break;

case 60:price = 0.69;break;

case 75:price = 0.85;break;

case 100:case 150:

price = 1;break;

default:price = 0;System.out.println("No bulb of" + watts +

"watts in stock");} // end switch

Page 33: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

// Method to return the price of a light bulbpublic double bulbPrice (int watts) {

switch (watts) {case 40: return 0.50;case 60: return 0.69;case 75: return 0.85;case 100:case 150: return 1;default: return 0;} // end switch

}

if (watts == 40) price = 0.50;else if (watts== 60) price = 0.69;else if (watts == 75) price = 0.85;else if (watts == 100 || watts==150) price = 1;else {

price = 0;displayResult ("No bulb of "+watts + "watts in

stock"); }

Page 34: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

public class switch1 {

private int mark;

public switch1(int number) { mark = number; }

public int getMark() { return mark; }

public void testSwitch() {

switch (mark / 10) {

case 0:

case 1:

case 2:

case 3:

case 4: System.out.println("Grade F "); break;

case 5: System.out.println("Grade D "); break;

case 6: System.out.println("Grade C "); break;

case 7: System.out.println("Grade B "); break;

case 8:

case 9: System.out.println("Grade A "); break;

default: System.out.println("Invalid mark");

}

}

Page 35: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

public static void main(String[] args) {

switch1 grade = new switch1(85);

System.out.print("Your mark is " + grade.getMark() + " and your ");

grade.testSwitch();

}

Page 36: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Case Statement

public class switch2 {

private int num1;

private int num2;

private char operand;

private int result;

public switch2(int aValue, char op,int bValue) {

num1 = aValue;

operand = op;

num2 = bValue;

}

public int getResult() {

return result;

}

Page 37: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

public void testSwitch() {switch (operand) {

case '*' : result = num1 * num2; break;

case '/' : result = num1 / num2; break;

case '+' : result = num1 + num2; break;

case '-' : result = num1 - num2; break;

default: System.out.println("Invalid Operand");

}

}

public static void main(String[] args) {

switch2 number = new switch2(8, '*', 6);

number.testSwitch();

System.out.println("The result is " + number.getResult());

}

}

Page 39: ทส 213  การเขียนโปรแกรมเชิงวัตถุ  1

[email protected]

Q&A