26
Decomposition Solving a large problem by breaking it into smaller subproblems Sunday, August 12, 12

Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

DecompositionSolving a large problem by breaking it into smaller

subproblems

Sunday, August 12, 12

Page 2: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

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

Page 3: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

PotholeFillingKarel

Before After

Sunday, August 12, 12

Page 4: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

PotholeFillingKarel

Public void run() {

move();

fillPothole();

move();

}

Sunday, August 12, 12

Page 5: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

A small exercise

• Create two more potholes

• Command Karel to fill that holes

Sunday, August 12, 12

Page 6: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

Control Statementsin Karel

Sunday, August 12, 12

Page 7: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

Every day we repeat

• Our life

• Wake up

• Get dress

• Go school

• Study

• Go back home

Sunday, August 12, 12

Page 8: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

Two types of control statements

• Conditional statements

• Iterative statements

Sunday, August 12, 12

Page 9: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

Conditional Statements

• Is there a beeper in a hole?

• if yes, Karel should not put a new beeper in it

Sunday, August 12, 12

Page 10: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

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

Page 11: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

IF

Sunday, August 12, 12

Page 12: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

IF Syntax

if (conditional test) {

statements

}

Sunday, August 12, 12

Page 13: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

PotholeFillingWithCondition

public void run() {

move(); fillPothole(); move(); }

private void fillPothole() { turnRight(); move(); if(noBeepersPresent()){ putBeeper(); } turnAround(); move(); turnRight(); }

Sunday, August 12, 12

Page 14: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

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

Page 15: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

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

Page 16: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

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

Page 17: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

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

Page 18: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

Improve turnRight()

public void turnRight() {

for(int i = 0; i < 3; i++) {

turnLeft();

}

}

Sunday, August 12, 12

Page 19: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

KarelFillManyPotholes

for(int i=0; i< 5; i++) {

move();

fillPothole();

move();

}

Sunday, August 12, 12

Page 20: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

If you don’t know a number of repeated steps

While (conditions) {

statements to be repeated

}

Sunday, August 12, 12

Page 21: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

KarelFillManyPotholes

while ( frontIsClear() ) {

move();

fillPothole();

move();

}

Sunday, August 12, 12

Page 22: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

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

Page 23: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

Exercise

• You need to create a world for your buddy to test his/her KarelFixPotholes.java

Sunday, August 12, 12

Page 24: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

Fencepost Error

How many fenceposts do you need?

Sunday, August 12, 12

Page 25: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

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

Page 26: Decomposition - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide2.pdf · 2012-11-09 · Decomposition • Avoiding repeating the same command sequences for the same

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