CS 106A, Lecture 3 Problem-solving with Karel

Slides:



Advertisements
Similar presentations
1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
Advertisements

Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when.
Copyright, Joseph Bergin
Nested If Statements While Loops
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Karel’s Sensory Equipment This PPT originated with Dr. Untch and Dr. Hankins Modifications have been made by Dr. Cripps.
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.
CMPUT 101 Lab # 2 September 17, :00 – 16:50.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
Karel the Robot -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright.
Conditionals How do we solve tasks in which every particular of a task is not specifically known? – A robot needs the ability to survey its immediate environment.
Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3.
Robot? What’s a Robot? Introducing Karel-the-Robot.
Chapter 5 Conditionally Executing Instructions
1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
Karel the Robot A Gentle Introduction to the Art of Programming.
Programming Errors Lexical errors – occur whenever Karel reads a word that is not in his vocabulary. Example in English: We are asking directions and instead.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
15-100: Introduction to Programming w/ Java * Ananda Gunawardena -- Lecture – School of Computer Science – Phone : (x81559) – Office: Wean Hall.
Chapter 5.  We’ve been using UrRobot – knows only 5 things  Let’s now use a new type of Robot: Robot!  i.e. – public class MileMover extends Robot.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Karel J. Robot Chapter 6 Instructions That Repeat.
1 Chapter 5 - IF CH5 – Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Karel J Robot Chapter 5.
1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)
Karel J. Robot Chapter 6 Instructions That Repeat.
Boğaziçi Ünv Koç Ünv Darüşşafaka Lisesi
Karel – Primitive Instructions
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Eric Roberts and Jerry Cain
Administrative Matters
Copyright © 2008 by Helene G. Kershner
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Copyright © 2008 by Helene G. Kershner
Karel J Robot.
karel_part4_functions_2
Karel – Primitive Instructions
Karel J Robot.
Karel J Robot Chapter 6.
CS 106A, Lecture 2 Programming with Karel
CS 106A, Lecture 6 Control Flow and Parameters
Design and Technology Academic Year 2017/2018 Grade 7 First Semester.
Stepwise Refinement Eric Roberts CS 106A January 8, 2010.
Karel the Robot – Making Decisions
SSEA Computer Science: CS106A
slides courtesy of Eric Roberts
A Gentle Introduction to the Art of Object Oriented Programming
CH5 – Conditional Statements
Nested If Statements While Loops
Iteration (Loops) Loops repeat a set of instructions
Karel – Primitive Instructions
Presentation transcript:

CS 106A, Lecture 3 Problem-solving with Karel suggested reading: Karel, Ch. 5-6

Plan For Today Announcements Recap: Control Flow Demo: HurdleJumper Decomposition Practice: Roomba Debugging

Plan For Today Announcements Recap: Control Flow Demo: HurdleJumper Decomposition Practice: Roomba Debugging

Plan For Today Announcements Recap: Control Flow Demo: HurdleJumper Decomposition Practice: Roomba Debugging

Karel Knows 4 Commands move turnLeft putBeeper pickBeeper

Karel Knows 4 Commands move turnLeft “methods” putBeeper pickBeeper

Defining New Commands We can make new commands (or methods) for Karel. This lets us decompose our program into smaller pieces that are easier to understand. private void name() { statement; ... } For example: private void turnRight() { turnLeft(); }

Control Flow: For Loops for (int i = 0; i < max; i++) { statement; ... } Repeats the statements in the body max times. Executes statements in order – NOT repeating each one individually

Control Flow: While Loops while (condition) { statement; ... } Repeats the statements in the body until condition is no longer true. Each time, Karel executes all statements, and then checks the condition. Karel can ask some questions about its world / surroundings

This is Table 1 on page 18 of the Karel coursereader. Possible Conditions Test Opposite What it checks frontIsClear() frontIsBlocked() Is there a wall in front of Karel? leftIsClear() leftIsBlocked() Is there a wall to Karel’s left? rightIsClear() rightIsBlocked() Is there a wall to Karel’s right? beepersPresent() noBeepersPresent() Are there beepers on this corner? beepersInBag() noBeepersInBag() Any there beepers in Karel’s bag? facingNorth() notFacingNorth() Is Karel facing north? facingEast() notFacingEast() Is Karel facing east? facingSouth() notFacingSouth() Is Karel facing south? facingWest() notFacingWest() Is Karel facing west? This is Table 1 on page 18 of the Karel coursereader.

Loops Overview for loop while loop I want Karel to repeat some commands! Know how many times Don’t know how many times for loop while loop E.g. “pick up all the beepers on this square” E.g. “turn until you’re facing west” E.g. “put down 5 beepers”

Fencepost Structure The fencepost structure is useful when you want to loop a set of statements, but do one part of that set 1 additional time. putBeeper(); // post while (frontIsClear()) { move(); // fence putBeeper(); // post }

If/Else Statements if (condition) { statement; ... } else { } Runs the first group of statements if condition is true; otherwise, runs the second group of statements. Karel can ask some questions about its world / surroundings

Infinite Loops

Infinite Loops Rinse Lather Repeat

Infinite Loops private void turnToWall() { while(leftIsClear()) { turnLeft(); }

Infinite Loops private void turnToWall() { while(leftIsClear()) { turnLeft(); }

Infinite Loops private void turnToWall() { while(leftIsClear()) { turnLeft(); }

Infinite Loops private void turnToWall() { while(leftIsClear()) { turnLeft(); }

Infinite Loops // Karel will keep turning left forever! private void turnToWall() { while(leftIsClear()) { turnLeft(); }

Infinite Loops private void turnToWall() { while(leftIsClear()) { if (frontIsBlocked()) { turnLeft(); }

Infinite Loops // Karel will be stuck in this loop forever! private void turnToWall() { while(leftIsClear()) { if (frontIsBlocked()) { turnLeft(); }

Plan For Today Announcements Recap: Control Flow Demo: HurdleJumper Decomposition Practice: Roomba Debugging

HurdleJumper We want to write a Karel program that hops hurdles. Karel starts at (1,1) facing East, and should end up at the end of row 1 facing east. The world has 9 columns. There are an unknown number of ”hurdles” (walls) of varying heights that Karel must ascend and descend to get to the other side.

HurdleJumper Demo

Pre/post comments precondition: Something you assume is true at the start of a method. postcondition: Something you promise is true at the end of a method. pre/post conditions should be documented using comments. /* * Jumps Karel over one hurdle of arbitrary height. * Pre: Karel is facing east, next to a hurdle. * Post: Karel is facing east at the bottom of the other * side of the hurdle. */ public void jumpHurdle() { ascendHurdle(); move(); descendHurdle(); }

Plan For Today Announcements Recap: Control Flow Demo: HurdleJumper Decomposition Practice: Roomba Debugging

Decomposition Breaking down problems into smaller, more approachable sub-problems (e.g. our own Karel commands) Each piece should solve one problem/task (< ~ 20 lines of code) Descriptively-named Well-commented! E.g. getting up in the morning: Wake up Brush teeth Put toothpaste on toothbrush Insert toothbrush into mouth Move toothbrush against teeth …

Top-Down Design Start from a large task and break it up into smaller pieces Ok to write your program in terms of commands that don’t exist yet E.g. HurdleJumper

Plan For Today Announcements Recap: Control Flow Demo: HurdleJumper Decomposition Practice: Roomba Debugging

Practice: Roomba Write a Roomba Karel that sweeps the entire world of all beepers. Karel starts at (1,1) facing East. The world is rectangular, and some squares contain beepers. There are no interior walls. When the program is done, the world should contain 0 beepers. Karel's ending location does not matter. How should we approach this tricky problem? . 1 2 3 4 5 6 7 8

Possible algorithm 1 . 1 2 3 4 5 6 7 8

Possible algorithm 2 . 1 2 3 4 5 6 7 8

Possible algorithm 3 . 1 2 3 4 5 6 7 8

Possible algorithm 4 8 . . . . . . . . 7 . . . . . . . . 6 . . . . . . . . 5 . . . . . . . . 4 . . . . . . . . 3 . . . . . . . . 2 . . . . . . . . 1 . . . . . . . . 1 2 3 4 5 6 7 8

Roomba Demo

Plan For Today Announcements Recap: Control Flow Demo: HurdleJumper Decomposition Practice: Roomba Debugging

Debugging Finding and fixing unintended behavior in your programs. Try to narrow down where in your code you think the bug is occurring. (E.g. what command or set of commands) We can use Eclipse to help us figure out what our program is doing.

BuggyRoomba Demo

Recap Announcements Recap: Control Flow Demo: HurdleJumper Decomposition Practice: Roomba Debugging Next time: An introduction to Java