1 / 9

Karel J Robot

Karel J Robot. Chapter 6. What we have learned so far:. Classes vs objects (e.g. UrRobot vs. karel ) Parts of a class (constructor, method) Classifying errors Polymorphism (extending classes) Abstract classes and interfaces Conditionals Booleans, logical operators. For Loop.

mahdis
Télécharger la présentation

Karel J Robot

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Karel J Robot Chapter 6

  2. What we have learned so far: • Classes vs objects (e.g. UrRobot vs. karel) • Parts of a class (constructor, method) • Classifying errors • Polymorphism (extending classes) • Abstract classes and interfaces • Conditionals • Booleans, logical operators

  3. For Loop • Loops – code that repeats some action either a finite or infinite amount of times • Also called iteratives • 2 types of loops • Definate loops (for) perform instructions an explicit number of times • Indefinate loops (while) perform instructions until a certain test fails

  4. For Loop • Example: public void move(intnumMoves){..} • Which loop do we want? • Structure of a for loop: • For(int x = 0; x<10; x++) • { <repeated code>} • For loop needs 3 things: • An initializer – can be initialized in for loop, or before • A boolean test – loop while run until the test is false • An operation – typically an incrementer

  5. For Loop • How many times will each for-loop run? • for(x=0; x<10; x++) { …} • for(x=0; x<=10; x++){…} • for(x=10; x>0; x--){…} • for(x=2; x>=14; x+=2) {…} • Feel free to program the code and test it • Let’s keep to a standard form: • Start: x=0; • Test: x<#; //don’t use <=; the # will represent how many times the loop runs • Increment by x++;

  6. While Loop • Aside: the For-Loop takes on different forms, but the previous slides demonstrate its simplest form • For Loop – good for looping when you know how many times you need to repeat • When you need to program a loop that runs until a certain event happens…. Use a while loop

  7. While Loop • Form: • while(<test>) • { <do instructions>} • Examples: • while(!nextToABeeper()) {..} • while(x>10){…} • while(2>=4){…} • The test can be any boolean outcome • The actions in the loop should make progress to change the boolean test (to avoid an infinite loop)

  8. Relational Operators • Review • ->Logical operators: &&, ||, ! • Relational Operators are used for comparison • >, >=, < <=, ==, != • Note: = and == are not the same thing! = is the assignment operator… == is ‘equal to’

  9. Student work • Read chapter 6 (up to page 161) • Program the Guard robot on page 162 to the end of the chapter • Problem set 6 • #1,2,3,8,14,16,24,34

More Related