ทส 213 การเขียนโปรแกรมเชิงวัตถุ 1
Post on 30-Dec-2015
34 Views
Preview:
DESCRIPTION
TRANSCRIPT
attawit_it@hotmail.com
ทส213 การเขียนโปรแกรมเชิ�งวั�ตถุ�1
Flow Control:
if & switch-case
อาจารย�อรรถุวั�ท ชิ�งคมานนท�สาขีาวั�ชิาเทคโนโลยสารสนเทศ
คณะวั�ทยาศาสตร�www.itsci.mju.ac.th
ทส213การเขียนโปรแกรมเชิ�งวั�ตถุ� 1- 1ObjectOr i ent ed Pr ogr ammi ng
attawit_it@hotmail.com
Selection: if statement
Syntax: if sttement ในกรณท#มทางเล$อกเดียวัif (condition) statement
condition
statement
true
false
เป็�น Statements ที่��ใช้�ในการตั�ดสิ�นใจ โดยขึ้��นอย��ก�บผลล�พธ์"ในการป็ระเมิ�นผลจากเงื่&�อนไขึ้ที่��ก(าหนดไว้�เป็�น true
IF statement จะยอมิให�การที่(างื่านสิามิารถขึ้�ามิไป็ย�งื่ statement ที่��ตั�องื่การ โดยขึ้��นอย��ก�บผลล�พธ์"ขึ้องื่เงื่&�อนไขึ้ที่��ถ�กก(าหนดไว้�
หากเงื่&�อนไขึ้แบบ Boolean เป็�นจร�งื่ การป็ระมิว้ลผลขึ้องื่ statement จะเก�ดขึ้��นตัามิล(าด�บ แตั�หากเป็�นเที่-จจะขึ้�ามิไป็โดยอ�ตัโนมิ�ตั�
attawit_it@hotmail.com
If statement (2)
โดียปกต�แล&วั IF statement จะมขีอบเขีตการท(างานไดี&เพียง 1 statement ดี�งน�+นในกรณท#ต&องการท(างานก�บหลาย ๆ statement จะต&องท(าการก(าหนดีขีอบเขีตดี&วัยเคร$#องหมายป.กกาล&อมรอบไวั&ใน state
ments ต/าง ๆ เชิ/น :
if (condition) {
Statement1;Statement2;…
}
attawit_it@hotmail.com
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();
}
}
attawit_it@hotmail.com
If statement (3)
ในกรณท#เป0นการเล$อกแบบสองทางจะมร1ปแบบดี�งน+if (condition)
statementT
else statementF
หากการประเม�นผลเง$#อนไขีเป0น FALSE statement ล(าดี�บถุ�ดีจาก else จะท(างานท�นท
condition
statementtrue
false
statement
attawit_it@hotmail.com
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();
}
}
attawit_it@hotmail.com
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
attawit_it@hotmail.com
If statement (4)
ในกรณขีองล1ปเชิ�งซ้&อนจะต&องระม�ดีระวั�งการประมวัลผลเง$#อนไขีดี&วัย:
if (Condition1)
((((((((((( ( 2
2Statement ;else
Statement3;
attawit_it@hotmail.com
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();
}
}
attawit_it@hotmail.com
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.
attawit_it@hotmail.com
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
เที่-จ
เที่-จ
attawit_it@hotmail.com
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();
}
}
attawit_it@hotmail.com
Conditional Assignment
int num = 5; int count;
count = num > 3 ? 1 : 0;
การป็ระมิว้ลผลจะอย��ในร�ป็ True หร&อ False
<boolean> ? <true condition> : <false condition>
attawit_it@hotmail.com
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());
}
}
attawit_it@hotmail.com
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(เงื่&�อนไขึ้)
จร�งื่
attawit_it@hotmail.com
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
attawit_it@hotmail.com
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");
attawit_it@hotmail.com
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())
attawit_it@hotmail.com
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
attawit_it@hotmail.com
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.");
attawit_it@hotmail.com
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.");
attawit_it@hotmail.com
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 %
attawit_it@hotmail.com
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;
attawit_it@hotmail.com
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
attawit_it@hotmail.com
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");
attawit_it@hotmail.com
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
attawit_it@hotmail.com
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.");
attawit_it@hotmail.com
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
}
attawit_it@hotmail.com
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)
attawit_it@hotmail.com
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");
}
attawit_it@hotmail.com
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 เที่�าน��น !!!
attawit_it@hotmail.com
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
attawit_it@hotmail.com
// 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"); }
attawit_it@hotmail.com
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");
}
}
attawit_it@hotmail.com
public static void main(String[] args) {
switch1 grade = new switch1(85);
System.out.print("Your mark is " + grade.getMark() + " and your ");
grade.testSwitch();
}
attawit_it@hotmail.com
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;
}
attawit_it@hotmail.com
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());
}
}
attawit_it@hotmail.com
attawit_it@hotmail.com
Q&A
top related