1 / 39

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

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

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Selection: if statement Syntax: if sttement ในกรณีที่มีทางเลือกเดียว if (condition) statement • เป็น Statements ที่ใช้ในการตัดสินใจ โดยขึ้นอยู่กับผลลัพธ์ในการประเมินผลจากเงื่อนไขที่กำหนดไว้เป็น true • IF statement จะยอมให้การทำงานสามารถข้ามไปยัง statement ที่ต้องการ โดยขึ้นอยู่กับผลลัพธ์ของเงื่อนไขที่ถูกกำหนดไว้ • หากเงื่อนไขแบบ Boolean เป็นจริง การประมวลผลของ statement จะเกิดขึ้นตามลำดับ แต่หากเป็นเท็จจะข้ามไปโดยอัตโนมัติ true condition false statement

  3. If statement (2) • โดยปกติแล้ว IF statement จะมีขอบเขตการทำงานได้เพียง 1 statement ดังนั้นในกรณีที่ต้องการทำงานกับหลาย ๆ statement จะต้องทำการกำหนดขอบเขตด้วยเครื่องหมายปีกกาล้อมรอบไว้ใน statements ต่าง ๆ เช่น : if(condition) { Statement1; Statement2; … }

  4. publicclass if1 { privateinttemperature; public if1(int temp) { temperature = temp; } publicintgetTemp() { returntemperature; } publicvoidtestif () { if (temperature < 30) System.out.println("Nice day"); System.out.println("Let goes to the beach"); } publicstaticvoid main(String[] args) { if1 temp = new if1(30); System.out.println("Temperature = " + temp.getTemp()); temp.testif(); } }

  5. If statement (3) • ในกรณีที่เป็นการเลือกแบบสองทางจะมีรูปแบบดังนี้ if (condition) statementT else statementF • หากการประเมินผลเงื่อนไขเป็น FALSE statement ลำดับถัดจาก else จะทำงานทันที false condition true statement statement

  6. publicclass if2 { privateinttemperature; publicif2(int temp) { temperature = temp; } publicintgetTemp() { returntemperature; } publicvoidtestif () { if(temperature < 28) System.out.println("Nice day"); else System.out.println("Hot day"); } publicstaticvoid main(String[] args) { if2 temp = new if2(25); System.out.println("Temperature = " + temp.getTemp()); temp.testif(); } }

  7. More Traps • a = 0เป็นการกำหนดค่า (assignment) • a == 0เป็นการเปรียบเทียบ (relational) • if (a = 0)Stmt แบบนี้คอมไพเลอร์จะทำการแจ้งข้อผิดพลาด • floatsและdoublesไม่สามารถนำมาเปรียบเทียบกันได้โดยตรง เช่น double x = 6.4, y = 8.6; if (x + y == 15.)// การประเมินผลเงื่อนไขจะเป็น fail if (Math.abs (x + y – 15.) < .0001)//will achieve the desired effect

  8. If statement (4) • ในกรณีของลูปเชิงซ้อนจะต้องระมัดระวังการประมวลผลเงื่อนไขด้วย: if (Condition1) if (Condition2) Statement2; else Statement3;

  9. publicclass if3 { privateinttemperature; publicif3(int temp) { temperature = temp; } publicintgetTemp() { returntemperature; } publicvoidtestif () { if(temperature < 30) if(temperature > 25) System.out.println("Nice day"); else System.out.println("Cold day"); } publicstaticvoid main(String[] args) { if3 temp = new if3(32); System.out.println("Temperature = " + temp.getTemp()); temp.testif(); } }

  10. if (a > 10) { if (b > 12) { statement1; } } else { statement2; } statement3; Traps else pairs with nearest unmatched if 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.

  11. If statement (5) if (Condition1) if (Condition2) Statement1; else Statement2; else Statement3;

  12. publicclass if4 { privateinttemperature; publicif4(int temp) { temperature = temp; } publicintgetTemp() { returntemperature; } publicvoidtestif () { if(temperature < 30) if(temperature > 25) System.out.println("Nice day"); else System.out.println("Cold day"); else System.out.println("Hot day"); } publicstaticvoid main(String[] args) { if4 temp = new if4(28); System.out.println("Temp = " + temp.getTemp()); temp.testif(); } }

  13. Conditional Assignment <boolean> ? <true condition> : <false condition> int num = 5; int count; count = num > 3 ? 1 : 0; การประมวลผลจะอยู่ในรูป True หรือ False

  14. publicclass condition { privateintnum; booleancount; publiccondition(int number) { num= number; } publicbooleangetCount(){ returncount; } publicvoidtestCond () { count= num > 3 ? true:false; } publicstaticvoid main(String[] args) { condition c = new condition(2); c.testCond(); System.out.println("Count is " + c.getCount()); } }

  15. If statement (6) • ในบางกรณีอาจใช้ if-then-else ร่วมกันในลักษณะที่นำมาซ้อนกันได้ if (Condition1) Statement1; else if (Condition2) Statement2;

  16. Multiple-Alternatives if (grade >= 90) System.out.println("A"); elseif (grade >= 80) System.out.println("B"); elseif (grade >= 70) System.out.println("C"); elseif (grade >= 60) System.out.println("D"); else System.out.println("F"); Exam score Grade Assigned 90 & above A 80-89 B 70-79 C 60-69 D Below 60 F

  17. Multiple-Alternatives if (grade >= 90) System.out.println("A"); elseif (grade >= 80) System.out.println("B"); elseif (grade >= 70) System.out.println("C"); elseif (grade >= 60) System.out.println("D"); else System.out.println("F"); if (grade >= 60) System.out.println("D"); elseif (grade >= 70) System.out.println("C"); elseif (grade >= 80) System.out.println("B"); elseif (grade >= 90) System.out.println("A"); else System.out.println("F");

  18. if (a > b && c != 7) if (a < 0 || a > 10) if (a == 5 ^ b == 5) if (!person.isFemale()) Boolean Operatorscombine boolean values • b1 && b2 true ในกรณีที่ทั้ง b1 and b2 เป็น true • b1 ||b2 true หาก b1 or b2 ค่าใดค่าหนึ่งเป็น true • b1 ^ b2 true เฉพาะ b1 หรือ b2 ค่าเดียวเท่านั้นที่เป็น true • !b1 True หาก b1 ไม่เป็น true

  19. true pH < 3 false very acidic pH>=3 true pH<7 false acidic pH>=7 true pH ==7 false neutral pH >7 true pH<12 false alkaline pH>=12 very alkaline Solution pH: Equivalent if -else if format if (condition1 ) statement1 elseif (condition2 ) statement2 elseif (condition3 ) statement3 elseif (conditionn ) statementn else statementn+1 . . .

  20. if (pH < 3) System.out.println("Solution is very acidic."); elseif (pH < 7) System.out.println("Solution is acidic."); elseif (pH == 7) System.out.println("Solution is neutral."); elseif (pH < 12) System.out.println("Solution is alkaline."); else System.out.println("Solution is very alkaline.");

  21. neutral very acidic acidic alkaline very alkaline pH 3 7 12 if (pH >= 12) System.out.println("Solution is very alkaline."); elseif (pH > 7) System.out.println("Solution is alkaline."); elseif (pH == 7) System.out.println("Solution is neutral."); elseif (pH >= 3) System.out.println("Solution is acidic."); else System.out.println("Solution is very acidic.");

  22. 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 %

  23. Decision table for a payroll system if (salary < 0.00) // error checking System.out.println("Error: negative salary $" + salary); elseif (salary < 1500.00) // first range tax = 0.15 * salary; elseif (salary < 3000.00) // second range tax = 225.00 + (salary - 1500.00) * 0.16; elseif (salary < 5000.00) // third range tax = 465.00 + (salary - 3000.00) * 0.18; elseif (salary < 8000.00) // fourth range tax = 825.00 + (salary - 5000.00) * 0.20; else // fifth range tax = 1425.00 + (salary - 8000.00) * 0.25;

  24. false true earnings>2000 true false apply to local college SAT>1300 apply to first choice college apply to parents alma mater Nested if statement with independent conditions

  25. Nested if statement with independent conditions if (earnings > 2000.00) { if (SAT > 1300) System.out.println("Apply to first choice college"); else // SAT <= 1300 System.out.println("Apply to parents alma mater"); } else // earnings <= 2000 System.out.println("Apply to local college"); braces { } not necessary

  26. Compare two date objects d1 and d2 (year, month, day) true false d1.y <d2.y false true d1.y ==d2.y d1 before d2 d1 after d2 true false d1.m <d2.m true false d1.m ==d2.m d1 before d2 d1 after d2 false true d1.d <d2.d false true d1d ==d2.d d1 before d2 d1 equal to d2 d1 after d2

  27. if (d1.year < d2.year) // first date < second date System.out.println("The first date is before the second."); elseif (d1.year == d2.year) // must compare months, days if (d1.month < d2.month) // first date < second date System.out.println("The first date is before the second."); elseif (d1.month == d2.month) // must compare days if (d1.day < d2.day) // first date < second date System.out.println("The first date is before the second."); elseif (d1.day == d2.day) // dates are equal System.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.");

  28. 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 }

  29. Switch statement • ค่าของselectorจะถูกเปรียบเทียบกับค่าของlabelทีละค่าในแต่ละรอบของการทำงาน • เมื่อผลการเปรียบเทียบมีค่าเท่ากันในstatements ที่ถูกอ้างถึงในlabelนั้น ๆ จะถูกประมวลผลไปจนกว่าจะถึงคำสั่ง break หรือ returnซึ่งจะมีผลทำให้ statements อื่น ๆ ใน switch block ถูกข้ามไป • หากผลการเปรียบเทียบไม่เท่ากัน statements ที่อยู่ในdefaultจะถูกประมวลผล • default จะเป็นทางเลือกหากไม่ได้ทำการระบุไว้จะไม่มีการประมวลผลใด ๆ เกิดขึ้นเมื่อผลการเปรียบเทียบไม่เท่ากัน • หากคำสั่งbreak (return) statement ไม่ได้มีการระบุไว้การประมวลผลจะกระทำต่อไปยัง labels ถัดไปตามลำดับ • ชนิดข้อมูลของทั้งselectorและlabels จะต้องเป็นแบบordinal type (ใน Java, ordinal types จะได้แก่: integer types, boolและchar)

  30. switch (paycode) { case2: exempt = false; break; case4: case6: case7: exempt = true; break; default: System.out.println ("Bad paycode"); } switch Statement switch (expression) { case value1: // do something break; casevalue2: // do something break; case value3: // do something break; default: // do something }

  31. finalint MALE = 1; finalint FEMALE = 2; switch (gender) { case MALE: … break; case FEMALE: … break } Use of Constants in switch statements switch (gender) { case 1: … break; case 2: … break; } • ชนิดของข้อมูลในเงื่อนไขของ switch : byte, char, short, int เท่านั้น !!!

  32. Switch statement examples // Set price for a light bulb of // 40, 60, 75, 100 or 150 watts switch (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

  33. // Method to return the price of a light bulb publicdouble 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; elseif (watts== 60) price = 0.69; elseif (watts == 75) price = 0.85; elseif (watts == 100 || watts==150) price = 1; else { price = 0; displayResult ("No bulb of "+watts + "watts in stock"); }

  34. publicclass switch1 { privateint mark; publicswitch1(int number) { mark = number; } publicintgetMark() { return mark; } publicvoidtestSwitch() { switch(mark / 10) { case0: case1: case2: case3: case4: System.out.println("Grade F "); break; case5: System.out.println("Grade D "); break; case6: System.out.println("Grade C "); break; case7: System.out.println("Grade B "); break; case8: case9: System.out.println("Grade A "); break; default: System.out.println("Invalid mark"); } }

  35. publicstaticvoid main(String[] args) { switch1 grade = new switch1(85); System.out.print("Your mark is " + grade.getMark() + " and your "); grade.testSwitch(); }

  36. Case Statement publicclass switch2 { privateint num1; privateint num2; privatechar operand; privateint result; publicswitch2(intaValue, charop,intbValue) { num1 = aValue; operand = op; num2 = bValue; } publicintgetResult() { returnresult; }

  37. publicvoidtestSwitch() { 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"); } } publicstaticvoid main(String[] args) { switch2 number = new switch2(8, '*', 6); number.testSwitch(); System.out.println("The result is " + number.getResult()); } }

  38. Q&A

More Related