SSEA Computer Science: CS106A

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.
1 of 3 Karel Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
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.
Karel The Robot In the beginning… software. Karel the Robot  All robots are controlled by software  Artificially intelligent robots that can “think”
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.
Polymorphism Are there different ways to solve the Harvester problem? – Robot teams – instead of one robot to solve a problem, let’s get a team of robots.
Robot? What’s a Robot? Introducing Karel-the-Robot.
Karel JRobot Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
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 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.
Thanks to Dr. Kris Schindler for this (and all Karel the Robot slides)
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.
Karel the Robot A Gentle Introduction to the Art of Object- Oriented Programming in Java.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
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
Programming in Karel Eric Roberts CS 106A January 6, 2016.
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 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
CS 106A, Lecture 3 Problem-solving with Karel
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.
In the beginning… software
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
Karel the Robot – Making Decisions
slides courtesy of Eric Roberts
CH5 – Conditional Statements
Nested If Statements While Loops
Iteration (Loops) Loops repeat a set of instructions
Karel – Primitive Instructions
Presentation transcript:

SSEA Computer Science: CS106A Dr. Cynthia Lee Lecturer in Computer Science Stanford

Goals for this Course Learn how to harness computing power to solve problems. To that end: Explore fundamental techniques in computer programming Develop good software engineering techniques Gain familiarity with the Java programming language

Goals for today Learn how to harness computing power to solve problems. To that end: Look at a very simple model of computation (Karel), and see how we build from these basic building blocks to surprisingly elaborate results. Observe that in doing that building, we need to be organized in our design and encapsulate ideas into layers of units.

Meet Karel the Robot

Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5

Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5

Each row is called a street. Karel's World 3 . . . . . 2 . . . . . 1 . . . . . Each row is called a street. 1 2 3 4 5

Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5

Each column is called an avenue. Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5 Each column is called an avenue.

Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5

The intersection of a street and an avenue is a corner. Karel's World 3 . . . . . 2 . . . . . 1 . . . . . The intersection of a street and an avenue is a corner. 1 2 3 4 5

Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5

Karel cannot move through walls. Karel's World 3 . . . . . 2 . . . . . 1 . . . . . Karel cannot move through walls. 1 2 3 4 5

Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5

Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5

Beepers mark locations in Karel's world. 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5 Beepers mark locations in Karel's world.

Karel's World 3 . . . . . 2 . . . . . 1 . . . . . 1 2 3 4 5

. . . . . . . . . . . . . . . Karel's World Karel Commands 3 2 1 1 2 3 4 5

. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1 4 5

. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1 4 5

. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1 4 5

. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1 4 5

. . . . . . . . . . . . . . . Karel's World move Karel Commands 3 2 1 4 5

. . . . . . . . . . . . . . . Karel's World move pickBeeper Karel Commands 3 . . . . . move pickBeeper 2 . . . . . 1 . . . . . 1 2 3 4 5

. . . . . . . . . . . . . . . Karel's World move pickBeeper Karel Commands 3 . . . . . move pickBeeper 2 . . . . . 1 . . . . . 1 2 3 4 5

. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft 1 . . . . . 1 2 3 4 5

. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft 1 . . . . . 1 2 3 4 5

. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft 1 . . . . . 1 2 3 4 5

. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft putBeeper 1 . . . . . 1 2 3 4 5

. . . . . . . . . . . . . . . Karel's World move pickBeeper turnLeft Karel Commands 3 . . . . . move pickBeeper 2 . . . . . turnLeft putBeeper 1 . . . . . 1 2 3 4 5

Wait, why are we learning something with such limited rules? Weird mix of extreme scarcity and extreme abundance in the way we thinking about resources in computer science.

Nairobi, Kenya: Akirachix Mention Telerivet, Medic Mobile Source: http://www.npr.org/blogs/alltechconsidered/2012/12/24/167961947/kenyan-women-create-their-own-geek-cuture

This piece of the program's source code is called a method. import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } This piece of the program's source code is called a method.

This line of code gives the name of the method (here, run) import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } This line of code gives the name of the method (here, run)

import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } The inside of the method is is called the body of the method and tells Karel how to execute the method.

import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } This part of the program is called a class definition. You'll discuss classes later in CS106A.

This is called an import statement. It tells Java what Karel is. import stanford.karel.*; public class OurKarelProgram extends Karel { public void run() { move(); pickBeeper(); turnLeft(); putBeeper(); } This is called an import statement. It tells Java what Karel is.

Improving our Program At this point, do the following: 1. Go back and replace “turnLeft() x3” with a turnRight method. 2. Mention that the name is completely irrelevant by renaming turnRight to something like chewBubbleGum. (Joke about “you could tell a 3-year old that toasters are called republicans and they'd believe you) 2. Introduce SuperKarel, which already knows how to turnRight. 3. Introduce the next problem: Karel Runs Races in a 4x4 world. Solve it by doing runOneStretch() x4, and having runOneStretch be move() x3

The for loop

for (int i = 0; i < N; i++) { … statements to repeat N times … } 1. Go back and replace runOneSide x4 and move x3 with the appropriate for loops. People are often confused about why the turnLeft() is outside of the for loop – explain this. Also, try to deflect questions about what the parts of the for loop mean – we'll cover that later on. 2. Mention that Karel is totally oblivious to his world – put Karel into a 7 x 7 world and he crashes, or an empty 10 x 10 world and he still runs around. 3. Motivate: Karel needs to learn about his world.

The while loop

while (condition) { } … statements to repeat when condition holds … Some of Karel's Conditions: frontIsClear() frontIsBlocked() beepersPresent() beepersInBag() facingNorth() facingSouth() See the Karel reader (Page 18) for more details.

while (condition) { } … statements to repeat when condition holds … Some of Karel's Conditions: frontIsClear() frontIsBlocked() beepersPresent() beepersInBag() facingNorth() facingSouth() See the Karel reader (Page 18) for more details. 1. Mention that we're going to use frontIsClear here. 2. Switch Karel so that he runs until he hits a wall and proceeds from there. 3. Put two “move” statements into the while loop and note that Karel smacks into the wall on a 4 x 4 world. Why? Because the condition is only tested at the bottom, not continuously. 3. Try changing the for loop that says to run four times with a while loop that moves while front is clear. Talk about infinite loops. Joke about “great civilizations will rise and fall and Karel will still run. Archaeologists will say that Karel must have served some religious purpose.” 4. Hurdle Jumping Karel: Motivate the problem and solve it using a combination of while and for loops. 5. Note that Karel seems to “trip” over empty barriers.

Karel condition methods Karel has some commands that are not meant to be complete statements, but rather are used to ask questions: 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 course reader.

The while loop and the Fencepost Bug

Exercise: Sweeper Recall we wrote a “RoombaKarel" that picks up all beepers in front of Karel in a straight line. We started with a version with a slight bug… . . . . . ...

A tricky bug Our code fails when there is a beeper at the final corner. Changing the order of statements is likely to make it fail when there is a beeper at the first corner. It must work for both. Karel has to take six steps… 1 . . . . . . . 1 2 3 4 5 6 7 …but has to check/sweep seven corners.

Fencepost problem loop { place a post. place a post. loop { fencepost problem: One with repeated statements, where some statements should be repeated n times and some n-1 times. Like creating a fence with 5 posts and 4 wires between the posts. Incorrect: Correct: loop { place a post. place a post. loop { place some wire. place some wire. } place a post. }

Fencepost loop public void run() { safePickup(); // post To solve a fencepost problem, perform the task that needs to happen n times (the "post") once outside the loop. If necessary, invert the steps inside the loop ("wire", then "post"). public void run() { safePickup(); // post while (frontIsClear()) { move(); // wire safePickup(); // post } public void safePickup() { if (beepersPresent()) { pickBeeper();

Extra Topics

Private methods private method: An advanced concept. Public methods can be called by other classes/programs. Private ones cannot. Not really relevant for our Karel programs, which always have 1 class. The book examples always use private methods. (except run) Your methods on HW1 can be either public or private. (Up to you) public void run() { safePickup(); while (frontIsClear()) { move(); } private void safePickup() { if (beepersPresent()) { pickBeeper();

Advanced: Colors import stanford.karel.*; The SuperKarel class has an additional method paintCorner that sets Karel's current position to be a given color. Valid colors are BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW, and null (no color) import stanford.karel.*; public class RedStripe extends SuperKarel { public void run() { while (frontIsClear()) { paintCorner(RED); move(); }

Suggested reading The following code examples in the Karel reader may prove useful: Ch. 4: BeeperCollectingKarel; collects all beepers from towers of arbitrary heights Ch. 5: MazeRunningKarel, finds a beeper in an arbitrary maze by following the "right-hand rule" Ch. 5: DoubleBeepers, doubles the number of beepers in a corner (harder than it sounds!)