170 likes | 284 Vues
Learn to code workshop lesson 3: Dive deep into logic statements and how robots survey the world, guiding their decisions. Understand the grammatical structure of logic statements and learn to implement them in Java for robot functions.
E N D
LEARN TO CODE WORKSHOP LESSON 3: Robots that Survey the WorldGeorge Kramer, george.Kramer@uconn.edu + +
Administrative • No workshop will be held on 10/16/2014 • Remaining workshops will be pushed back by one week • Look out for additional details in weekly ACM newsletter
Motivation • Develop dynamic thought processes using logic statements • Want robots to survey their world to determine what to do next
Translating Thought Processes • English language is ambiguous • First interpretations are difficult to change • Want to make informal logic statements formal
Logic Statements • Are true or false but not both • Valid statements • 2 + 2 = 4 • 2 + 2 = 5 • Invalid statements • He is a college student • Statements may be combined using symbols and operators
Logic Operators • Parenthesis can be used to override operator precedence
Compound Logic Statements • English translations for given statements p and q • ~p = not p (Negation of p) • p ∧ q = p and q (Conjunction of p and q) • p ∨ q = p or q (Disjunction of p and q) • Symbolic translations for given English sentences • Let h = “It is hot” and s = “It is sunny” • It is not hot but it is sunny = ~h ∧ s • It is neither hot nor sunny = ~h ∧ ~s
Truth Values • Recall statements must be either true or false • The truth or falsity of statements is determined by their truth value • Truth values can be determined from truth tables
Truth Table: AND Operator • Both statements must be true for the overall statement to be true
Truth Table: OR Operator • Only one of the statements needs to be true for the overall statement to be true
Truth Table: NOT Operator • Consider the opposite value of the statement to be true
Logic Statements in Java • Recall • Objects can do things • Objects can remember things… and tell you what they remember! • Objects can tell you true and false statements • Logic statements must be written to be grammatically correct
Grammar of Logic Statements in Java if (statement1) { // Instructions to execute when statement1 is true } else if (statement2) { // Instructions to execute when statement1 is false but // statement2 is true } else { // Instructions to execute when all above statements are false }
Example of Robot that Surveys the World SuperRobotkarel = new SuperRobot(1, 1, East, 0); if (!karel.frontIsClear() && !karel.rightIsClear()) { karel.turnLeft(); } else { karel.move(); }
Now It’s Your Turn!!! • Download and work on first two problems of lesson 3 problem set