Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)

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 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
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.
Chapter 1: Computer Systems
Murphy’s Laws. Things are more complex than they seem to be. Things take longer than expected. Things cost more than expected. If something can go wrong,
Chapter 3 Extending the Robot Programming Language.
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.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
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.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
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)
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.
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.
© 2006 Pearson Education 1 Obj: cont 1.3 and 1.4, to become familiar with identifiers and to understand how programming languages work HW: p.51 #1.8 –
The Java Programming Language
Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming.
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 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
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.
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.
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.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Compiler Errors Syntax error Lexical Can not resolve/find symbol Can not be applied Execution error Oh wait, a run time error Intent error It ran, but.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
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.
Working with Java.
In the beginning… software
Eric Roberts and Jerry Cain
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.
CS 106A, Lecture 2 Programming with Karel
Karel the Robot – Making Decisions
Chapter 1: Computer Systems
slides courtesy of Eric Roberts
A Gentle Introduction to the Art of Object Oriented Programming
Nested If Statements While Loops
CSE 111 Karel the Robot.
Karel – Primitive Instructions
Presentation transcript:

Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc) –move() –turnLeft() –putBeeper() –pickBeeper()

Ch. 2 2 move() forward only, one block “Error Shutoff” if trying to move into a wall (a duh! Look first!) –Think divide by zero???

Ch. 2 3 turnLeft() stay at same corner turn 90 degrees to the left cannot cause an error shutoff

Ch. 2 4 pickBeeper() picks up beeper on current corner and places in beeper bag attempted on beeper-less corners causes an error shutoff

Ch. 2 5 putBeeper() take beeper from beeper bag and put down on current corner attempted with an empty beeper bag causes an error shutoff

Ch. 2 6 Description Robot class public class Robot { void move() {…} void turnLeft() {…} void pickBeeper() {…} void putBeeper() {…} } “primitive” You can’t/don’t need to define this class – it is in a library for you to use

Ch. 2 7 Error Classification 4-types lexical error (compiler catches) –word not in its dictionary syntax error (compiler catches) –incorrect grammar, punctuation, incorrect location of a statement execution error (run-time environment catches) –can’t perform what you ask (at run-time) intent error (logic - guess who catches this one!) –program terminates successfully – junk output, however Which is the hardest type of error to correct? Why?

Ch. 2 8 More on Error Classification See additional PowerPoint presentation: –FOP_compilerErrors

Ch. 2 9 Valid Symbol Names Must not be a reserve word (e.g. main, private,..) Must begin with a letter –Not totally true, but we’ll go with this definition to make our lives easier! The rest of the name can be either –An (upper case) letter: A, B, C, … Y or Z –A (lower case) letter: a, b, c, …, y or z –A number 0, 1, 2, 3, …, 9 –An underscore: _ NO SPACES ALLOWED NO other characters: +, *, -, /, “, #, %, etc.

Ch Examples: Symbol Names Valid iLove_FOP value1 w_a_19_05 HELP h_e_l_p Robot karel move NOT valid know why! 1win main void you win I-love-FOP HELP! win+win TroyHighSchool#1

Ch Symbol Names Conventions Identifiers, Method name –Begins with a lower case letter. Each new word in the name is capitalized. For example: turnleft becomes turnLeft() Class name/Type –Begins with a Upper case (Capital) letter. Each new word in the name is capitalized. For example: left spinning robot becomes LeftSpinningRobot CONSTANTS –All capital letters For example: pi becomes PI

Ch Now You Try a Short Program on Paper – 10 minutes Assume a robot is at position (1, 1) (a.k.a, the “origin”) facing North and having plenty of beepers Have the robot “print” (using beepers) the number E (5-beepers tall and a 3 beepers wide) when done, the robot should be back at the origin, facing North.

Ch Your Task You will copy the Karel02 Folder to your work area There are two MainDriver files in this project. We will focus on FirstMainDriver first. In this class, there are four different Robot classes. –HurdlerRobot –WallClimberRobot –UpAndUnderRobot –AnchorLegRobot You will implement the task() method for all four Robot classes

Ch MainDriver When you execute this class, you should see each Robot practice its leg of the Obstacle Course in MainDriver1. Once each Robot has successfully completed its leg, the entire Obstacle course can be tested by executing the MainDriver2.

Ch Stepwise Refinement Factoring out common code –If you find yourself writing the same lines of code, you should make a new method Give new methods appropriate names! –Method names should describe what task the method completing

Ch Factoring out common code Example: public void sampleMethod() { turnRight(); move(); turnRight(); // other lines of code turnRight(); move(); turnRight(); } Replace common lines with a method call

Ch Factoring out common code Example: public void sampleMethod() { rightU_Turn(); // other lines of code rightU_Turn(); } // helper method public void rightU_Turn() { turnRight(); move(); turnRight(); }