For Wednesday Read Becker, chapter 4, sections 3-5 Program 2 design - - PowerPoint PPT Presentation

for wednesday
SMART_READER_LITE
LIVE PREVIEW

For Wednesday Read Becker, chapter 4, sections 3-5 Program 2 design - - PowerPoint PPT Presentation

For Wednesday Read Becker, chapter 4, sections 3-5 Program 2 design due Program 2 Advantages of Stepwise Refinement Tends to make programs Easier to understand Free of errors Easier to test and debug Easier to modify


slide-1
SLIDE 1

For Wednesday

  • Read Becker, chapter 4, sections 3-5
  • Program 2 design due
slide-2
SLIDE 2

Program 2

slide-3
SLIDE 3

Advantages of Stepwise Refinement

  • Tends to make programs

– Easier to understand – Free of errors – Easier to test and debug – Easier to modify

  • Why?
slide-4
SLIDE 4

The Reasons

  • People can’t keep all the details in their

heads at once.

  • Helps to impose structure on the problem
  • The descriptive names allow us to ignore

details

slide-5
SLIDE 5

Pseudocode Algorithms

  • Let us focus on the algorithm, not the

details of Java, first

  • Combines natural language with

structure

  • Very important as we move on to writing

programs that make decisions next week.

  • Example:

deliver fliers to each house up to the corner turn the corner deliver fliers to each house up to the corner turn the corner deliver to the last house

slide-6
SLIDE 6

Advantages of Pseudocode

  • Pseudocode helps us think more

abstractly, allowing us to ignore many irrelevant details.

  • Pseudocode allows us to trace our

programs very early in development.

  • Pseudocode can provide a common

language on a development team, even with non-technical users.

  • Algorithms expressed in pseudocode

can be implemented in a variety of programming languages.

slide-7
SLIDE 7

Using Multiple Robots

  • How could we do the flyer problem

efficiently with multiple robots?

  • Would we have to change the DeliveryBot

code?

slide-8
SLIDE 8
slide-9
SLIDE 9

import becker.robots.*; public class DeliverFlyers { public static void main(String[ ] args) { Route route = new Route(); DeliveryBot db1 = new DeliveryBot(route, 0, 0, Direction.EAST, 6); DeliveryBot db2 = new DeliveryBot(route, 6, 0, Direction.EAST, 6); DeliveryBot db3 = new DeliveryBot(route, 5, 5, Direction.WEST, 6); DeliveryBot db4 = new DeliveryBot(route, 11, 5, Direction.WEST, 6); DeliveryBot db5 = new DeliveryBot(route, 0, 6, Direction.EAST, 6); DeliveryBot db6 = new DeliveryBot(route, 6, 6, Direction.EAST, 6); DeliveryBot db7 = new DeliveryBot(route, 5, 11, Direction.WEST, 6); DeliveryBot db8 = new DeliveryBot(route, 11, 11, Direction.WEST, 6); db1.deliverBlock(); db2.deliverBlock(); db3.deliverBlock(); db4.deliverBlock(); db5.deliverBlock(); db6.deliverBlock(); db7.deliverBlock(); db8.deliverBlock(); }

slide-10
SLIDE 10

Factoring out Differences

  • What if I want a CollectionBot?
  • What do I need to change?
  • How do I avoid duplicating code?
slide-11
SLIDE 11

Helper Methods

  • What is a helper method?
  • Who should call a helper method?
  • How do we enforce that?
slide-12
SLIDE 12

Access Modifiers

  • Public

– Access from anywhere – Use for the services a robot provides to others

  • Protected

– Access from methods in the class or its subclasses – Use for methods that may be overridden but don’t need to be public

  • Private

– Access from only methods in the class – Use for anything that doesn’t need to be public or protected

slide-13
SLIDE 13

Control Structures

  • Allow programs to make decisions
  • Why the name “control structure”?
slide-14
SLIDE 14

Motivation

  • So far, we’ve written programs that

– executed one statement after another (pattern?) – executed all statements in a method and returned

  • Some problems can’t be solved this way

– Have a robot move forward until it reaches a wall. – Have a robot pick up things from intersections when not all intersections have things on them

  • Other examples?

– Robot – Non-robot

slide-15
SLIDE 15

Different Kinds of Decisions

  • Should this group of statements be executed once, or not

at all? if (karel.canPickThing()) { karel.turnLeft(); } karel.move();

  • Should this group of statements be executed again?

while (karel.canPickThing()) { karel.turnLeft(); } karel.move();

slide-16
SLIDE 16

Tracing an if statement

if (karel.frontIsClear()) { karel.move(); } karel.turnLeft();

slide-17
SLIDE 17

Tracing a while statement

while (karel.frontIsClear()) { karel.move(); } karel.turnLeft();

slide-18
SLIDE 18

What’s the Pattern?

slide-19
SLIDE 19

Pseudocode and Decisions

slide-20
SLIDE 20

Questions Robots Can Ask

  • Can I pick up a Thing from this

intersection?

  • How many Things are in my backpack?
  • Is the path in front of me clear?
  • What avenue am I on?
  • What street am I on?
  • What direction am I facing?
  • What is my speed?
  • What is my current label?
slide-21
SLIDE 21
slide-22
SLIDE 22

Predicates

  • Questions with the answers yes or no

(actually true or false)

  • Can be directly used in if and while

statements.

  • Return values of type boolean.
slide-23
SLIDE 23

Negatives

  • Sometimes we want to test the opposite of

a predicate

  • Use the symbol !
slide-24
SLIDE 24

Integer Queries

if (karel.getStreet() == 1) { karel.turnAround(); } while (karel.countThingsInBackpack < 4) { karel.pickThing(); }

slide-25
SLIDE 25

Comparison Operators

  • ==
  • !=
  • <
  • >
  • <=
  • >=
slide-26
SLIDE 26

Practice

  • Write code to move two spaces forward,

picking up any things in any space the robot enters.

slide-27
SLIDE 27

Problem

  • Write a method to make a Robot go

completely around the inside of a box created by walls.