decomposition - bangkok universitytulip.bu.ac.th/~thirapon.w/gim/oop_files/slide2.pdf ·...
TRANSCRIPT
DecompositionSolving a large problem by breaking it into smaller
subproblems
Sunday, August 12, 12
Decomposition
• Avoiding repeating the same command sequences for the same task
• Breaking a large problem down into smaller pieces (subproblems) that are easier to solve
• Ex.
• turnRight()
• turnAround() and backup()
Sunday, August 12, 12
PotholeFillingKarel
Before After
Sunday, August 12, 12
PotholeFillingKarel
Public void run() {
move();
fillPothole();
move();
}
Sunday, August 12, 12
A small exercise
• Create two more potholes
• Command Karel to fill that holes
Sunday, August 12, 12
Control Statementsin Karel
Sunday, August 12, 12
Every day we repeat
• Our life
• Wake up
• Get dress
• Go school
• Study
• Go back home
Sunday, August 12, 12
Two types of control statements
• Conditional statements
• Iterative statements
Sunday, August 12, 12
Conditional Statements
• Is there a beeper in a hole?
• if yes, Karel should not put a new beeper in it
Sunday, August 12, 12
Conditions in Karel• Karel can test the following conditions:
frontIsClear() frontIsBlocked()
leftIsClear() leftIsBlocked()
rightIsClear() rightIsBlocked()
beepersPresent() noBeepersPresent()
beepersInBag() noBeepersInBag()
facingNorth() notFacingNorth()
facingEast() notFacingEast()
facingSouth() notFacingSouth()
facingWest() notFacingWest()
positive condition negative condition
Sunday, August 12, 12
IF
Sunday, August 12, 12
IF Syntax
if (conditional test) {
statements
}
Sunday, August 12, 12
PotholeFillingWithCondition
public void run() {
move(); fillPothole(); move(); }
private void fillPothole() { turnRight(); move(); if(noBeepersPresent()){ putBeeper(); } turnAround(); move(); turnRight(); }
Sunday, August 12, 12
Iterative Statements
• Tell a machine to repeat the same procedure for the same purpose
• Powerful tool
• Your mission is to instruct Karel to fill beepers for all potholes along the street
Sunday, August 12, 12
The for Statement
• In Karel, the syntax of for statement is:
for (int i = 0; i < count; i++) { statements to be repeated}
for (int i = 0; i < count; i++) { statements to be repeated}
Sunday, August 12, 12
The for Statement
• In Karel, the syntax of for statement is:
for (int i = 0; i < count; i++) { statements to be repeated}
Sunday, August 12, 12
The for Statement
• In Karel, the syntax of for statement is:
for (int i = 0; i < count; i++) { statements to be repeated}
for (int i = 0; i < count; i++) { statements to be repeated}
Sunday, August 12, 12
Improve turnRight()
public void turnRight() {
for(int i = 0; i < 3; i++) {
turnLeft();
}
}
Sunday, August 12, 12
KarelFillManyPotholes
for(int i=0; i< 5; i++) {
move();
fillPothole();
move();
}
Sunday, August 12, 12
If you don’t know a number of repeated steps
While (conditions) {
statements to be repeated
}
Sunday, August 12, 12
KarelFillManyPotholes
while ( frontIsClear() ) {
move();
fillPothole();
move();
}
Sunday, August 12, 12
Solving a general problem(KarelFixPotholes.java)
• Your algorithm should work with any length of the street
• Potholes may occur any position of the street
• Some potholes are already fixed
Sunday, August 12, 12
Exercise
• You need to create a world for your buddy to test his/her KarelFixPotholes.java
Sunday, August 12, 12
Fencepost Error
How many fenceposts do you need?
Sunday, August 12, 12
Assignment 2
• There is an earthquake and Karel is hired to repair broken stones
• Instruction
• Download Assignment2.zip from course website
• Write your codes in StoneMasonKarel.java
Sunday, August 12, 12
Summary
• Control statements
• IF
• For loop
• While loop
• Decomposition will help you to improve your programming skill.
• Good luck for your assignments
Sunday, August 12, 12