slides courtesy of Eric Roberts

Slides:



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

1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
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
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.
Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The.
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.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
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 C Programming
Robot? What’s a Robot? Introducing Karel-the-Robot.
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have.
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
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 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
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)
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.
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.
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.
Alice and Java Unit 7 1. Day 1  Objective: Gain an introduction to Java and Eclipse  Essential skill: DM-1: Use technology to advance critical thinking.
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.
Eric Roberts and Jerry Cain
Boğaziçi Ünv Koç Ünv Darüşşafaka Lisesi
CS 106A, Lecture 3 Problem-solving with Karel
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.
Vocabulary Algorithm - A precise sequence of instructions for processes that can be executed by a computer Low level programming language: A programming.
In the beginning… software
Eric Roberts and Jerry Cain
Chapter 2 - Introduction to C Programming
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_part4_functions_2
Karel J Robot.
CS 106A, Lecture 2 Programming with Karel
The Need for Algorithms 2 days
Chapter 2 - Introduction to C Programming
Stepwise Refinement Eric Roberts CS 106A January 8, 2010.
SSEA Computer Science: CS106A
UNIT 3 CHAPTER 1 LESSON 4 Using Simple Commands.
T. Jumana Abu Shmais – AOU - Riyadh
slides courtesy of Eric Roberts
Algorithm and Ambiguity
CH5 – Conditional Statements
Nested If Statements While Loops
slides courtesy of Eric Roberts
Presentation transcript:

slides courtesy of Eric Roberts Programming in Karel Jerry Cain CS 106AJ September 26, 2018 slides courtesy of Eric Roberts

Once upon a time . . .

Rich Pattis and Karel the Robot Karel the Robot was developed by Rich Pattis in the 1970s when he was a graduate student at Stanford. In 1981, Pattis published Karel the Robot: A Gentle Introduction to the Art of Programming, which became a best-selling introductory text. Pattis chose the name Karel in honor of the Czech playwright Karel Capek, who introduced the word robot in his 1921 play R.U.R. In 2006, Pattis received the annual award for Outstanding Contributions to Computer Science Education given by the ACM professional society. ˇ Rich Pattis

Review: Primitive Karel Commands On Monday, you learned that Karel understands the following commands: move() Move forward one square turnLeft() Turn 90 degrees to the left pickBeeper() Pick up a beeper from the current square putBeeper() Put down a beeper on the current square At the end of class, we designed a Karel program to solve the following problem: + 1 2 3 4 5

The MoveBeeperToLedge Program /* * File: MoveBeeperToLedge.k * ------------------------- * This program moves a beeper to a ledge. */ function moveBeeperToLedge() { move(); pickBeeper(); turnLeft(); putBeeper(); }

Syntactic Rules and Patterns The definition of moveBeeperToLedge on the preceding slide includes various symbols (such as curly braces, parentheses, and semicolons) and special keywords (such as function) whose meaning may not be immediately clear. These symbols and keywords are required by the syntactic rules of the Karel programming language, in much the same way that syntactic rules govern human languages. When you are learning a programming language, it is often wise to ignore the details of the language syntax and focus instead on learning a few general patterns. Karel programs, for example, fit a common pattern in that they define one or more functions that describe the steps Karel must perform in order to solve a particular problem.

Defining New Functions In Karel—and in JavaScript as you will see beginning next week—a function is a sequence of statements that has been collected together and given a name. All functions in Karel have the following form: function name() { statements that implement the desired operation } The first function in a Karel program is the main function, which is called when you press the Run button at the bottom of the screen. Most Karel programs define additional helper functions that implement individual steps in the complete solution.

The turnRight Function As a simple example, the following function definition allows Karel to turn right by executing three turnLeft operations: function turnRight() { turnLeft(); } Once you have made this definition, you can use turnRight in your programs in exactly the same way you use turnLeft. In a sense, defining a new function is analogous to teaching Karel a new word. The name of the function becomes part of Karel’s vocabulary and extends the set of operations the robot can perform.

Helper Functions in a Program function moveBeeperToLedge() { move(); pickBeeper(); turnLeft(); turnRight(); putBeeper(); } /* Turns Karel right 90 degrees */ function turnRight() {

Exercise: Defining functions Define a function turnAround that turns Karel around 180°. function turnAround() { turnLeft(); } The turnRight and turnAround functions are so important that they are included in a library called "turns". Define a function backup that moves Karel backward one square, leaving Karel facing in the same direction. function backup() { turnAround(); move(); }

Control Statements In addition to allowing you to define new functions, Karel also includes statement forms that allow you to change the order in which statements are executed. Such statements are called control statements. The control statements available in Karel are: The repeat statement, which repeats a set of statements a predetermined number of times. The while statement, which repeats a set of statements as long as some condition holds. The if statement, which applies a conditional test to determine whether a set of statements should be executed at all. The if-else statement, which uses a conditional test to choose between two possible actions.

The repeat Statement In Karel, the repeat statement has the following form: repeat (count) { statements to be repeated } repeat (count) { statements to be repeated } As with the other control statements, the repeat statement consists of two parts: The header line, which specifies the number of repetitions The body, which is the set of statements to be repeated The keyword repeat and the various punctuation marks appear in boldface, which means that they are part of the repeat statement pattern. The things you can change appear in italics: the number of repetitions and the statements in the body.

Using the repeat Statement You can use repeat to redefine turnRight as follows: function turnRight() { repeat (3) { turnLeft(); } The following function creates a square of four beepers, leaving Karel in its original position: function makeBeeperSquare() { repeat (4) { putBeeper(); move(); turnLeft(); }

Conditions in Karel Karel can test the following conditions: positive condition negative condition frontIsClear() frontIsBlocked() leftIsClear() leftIsBlocked() rightIsClear() rightIsBlocked() beepersPresent() noBeepersPresent() beepersInBag() noBeepersInBag() facingNorth() notFacingNorth() facingEast() notFacingEast() facingSouth() notFacingSouth() facingWest() notFacingWest()

The while Statement The general form of the while statement looks like this: while (condition) { statements to be repeated } The simplest example of the while statement is the function moveToWall, which comes in handy in lots of programs: function moveToWall() { while (frontIsClear()) { move(); }

The if and if-else Statements The if statement in Karel comes in two forms: A simple if statement for situations in which you may or may not want to perform an action: if (condition) { statements to be executed if the condition is true } if (condition) { statements to be executed if the condition is true } else { statements to be executed if the condition is false } An if-else statement for situations in which you must choose between two different actions:

Exercise: Creating a Beeper Line Write a function putBeeperLine that adds one beeper to every intersection up to the next wall. Your function should operate correctly no matter how far Karel is from the wall or what direction Karel is facing. Consider, for example, the following function called test: function test() { putBeeperLine(); turnLeft(); } 1 2 3 4 5 2

Climbing Mountains For the rest of today, we’ll explore the use of functions and control statements in the context of teaching Karel to climb stair-step mountains that look something like this: 4 3 2 1 1 2 3 4 5 6 7 8 9 10 11 The initial version will work only in this world, but later examples will be able to climb mountains of any height.

The End