Kebot: Robot Simulator

Similar documents
1007 Imperative Programming Part II

CS 142 Style Guide Grading and Details

If Statements, For Loops, Functions

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

the NXT-G programming environment

PROGRAMMING FUNDAMENTALS

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

The first program: Little Crab

CS112 Lecture: Repetition Statements

CS112 Lecture: Loops

3 The L oop Control Structure

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

AP Computer Science Unit 3. Programs

LAB C Translating Utility Classes

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Flow of Control. 2.1 The if Statement

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Object-Oriented Programming

CT 229 Java Syntax Continued

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

LOOPS. Repetition using the while statement

CS50 Supersection (for those less comfortable)

REPETITION CONTROL STRUCTURE LOGO

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Introduction to Java & Fundamental Data Types

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

G. Tardiani RoboCup Rescue. EV3 Workshop Part 1 Introduction to RobotC

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

The viewer works in Chrome, Edge, Firefox, and Safari. The web address for the workshop is

The for Loop. Lesson 11

Intro. Scheme Basics. scm> 5 5. scm>

Software and Programming 1

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

Mr. Monroe s Guide to Mastering Java Syntax

CITS2401 Computer Analysis & Visualisation

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

1 Getting started with Processing

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Introduction to the Java Basics: Control Flow Statements

egrapher Language Reference Manual

CS 231 Data Structures and Algorithms, Fall 2016

Introduction to Programming using C++

Introduction. C provides two styles of flow control:

More Programming Constructs -- Introduction

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

C++ for Java Programmers

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

5 The Control Structure Diagram (CSD)

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

VARIABLES AND TYPES CITS1001

Language Reference Manual

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

COMS 469: Interactive Media II

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Sri Vidya College of Engineering & Technology

My First iphone App (for Xcode version 6.4)

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Procedures: Algorithms and Abstraction

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

SCRATCH MODULE 3: NUMBER CONVERSIONS

YOLOP Language Reference Manual

Unit 1 Lesson 4. Introduction to Control Statements

Text Input and Conditionals

Design Programming DECO2011

>print "hello" [a command in the Python programming language]

Lab 1 Implementing a Simon Says Game

Learning to use the drawing tools

Accelerating Information Technology Innovation

Ticket Machine Project(s)

Java is an objet-oriented programming language providing features that support

Repetition Through Recursion

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

The compiler is spewing error messages.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

EdPy worksheets Student worksheets and activity sheets

Scheme of work Cambridge International AS & A Level Computing (9691)

On the Web sun.com/aboutsun/comm_invest STAROFFICE 8 DRAW

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CS112 Lecture: Primitive Types, Operators, Strings

Transcription:

Kebot: Robot Simulator

» Completion of consent forms» Introduction to the research project» Structure of the workshop session» Advantages of taking part» Workshop has been designed for complete novices but if you need any help just ask!

» Most people don t know how a computer works and take for granted the programs they encounter everyday» Programming is like writing down the instructions it takes to do something - a process, a procedure» But programming is also creative. Some argue that writing a program is comparable to composing music, designing a house, or creating things» Why learn to program?

» Is going to be hands on you will learn by doing» Concepts will be introduced but you ll quickly move onto coding for yourselves» You ll have all the support you need to progress and the sessions have been designed to gradually build up knowledge» Code will be made available to help anyone who runs into difficulty» You will be using the JAVA programming language

» Methods used to teach programming are dull» They do little to encourage an interest in the subject» And can give programming a bad name» Hello World is a prime example

» Research found how little work has considered the use of simulated robots as programming learning tools» Simulators offer a more accessible method for learning programming than physical robots» To try and make things better» Fun!

» The Gates robot can be issued with several commands: forward() backward() left() right() stop()» These commands are called methods in programming. Think of them as sub-programs which contain instructions. These instructions have already been coded so all that is required is for the method to be called in your code (method invocation)

» The Gates robot already contains code instructing the robot to move forward and then turn left» Challenge: Place the Gates robot in the arena and then start the simulator. Observe how the robot follows these instructions.

» Open the Gates class. You will see the code: forward () ; left () ;» Two things must be noted: The double brackets after the method name and the semi-colon after the bracket.» Challenge: Expand your code so that the robot performs a square.

» To instruct the robot to perform a square your code will look like:

» Code Errors (Syntax Errors) occur when code does not conform to the conventions of the programming language» Syntax Errors must be corrected before code can be compiled» Whilst typing code the Undo function can be useful at reverting code back to its previous state if you make a mistake

» Kebot comes with drawing tools» A 2D Object is a flat drawing on the arena floor which a robot can travel over» A 3D object is a raised object (like a wall) which the robot bangs into» These features will be used more and more as the workshop progresses

» Arenas can be loaded by going to the File Menu and selecting Load Arena» Select the Square Drawer arena» It is possible to pass values to forward() and backward(). This allows greater control of the robot. Do this by putting a value in the brackets of the method name (e.g. forward (1) ; ). This value represents time (in seconds). Passing a value to a method is known as parameter passing» Challenge: Pass parameters to the methods in your existing code so that the robot remains within the confines of the Square Drawer arena. Call the stop() method at the end of your code to instruct the robot to stop moving.

» To instruct the robot to complete the Square Drawer task your code will look something like:

» Select the Line Tracer arena» Challenge: Using the following methods to instruct Gates to follow the line: forward() left() right() stop()» Some values you must pass to forward() are provided whilst others are not.» Tip: Write down and think about what you want to achieve before coding!

» Challenge: How does your robot fare when you introduce 3D objects into the arena? Can Gates navigate the walls you have drawn?

» To complete the task your code will look something like:» However, this code has 3 syntax errors. Can you spot them?

» The JAVA language has a number of formatting conventions. These help to improve the readability of code and make it easier to manage» Indentation helps to convey a programs structure. BlueJ automatically helps to indent code:» camelcase helps to differentiate between different programming concepts» Neither are compulsory for a program to compile, however, it is best to learn good habits early

» Methods ( e.g. forward() ) are like sub-programs which contain instructions. They can be called over and over» Syntax errors occur when code does not conform to the conventions of the programming language» Passing a value to a method ( e.g. forward (5); ) is known as parameter passing» Sequence is an important programming concept. This is when actions are executed in the order in which they appear what you have been doing so far!

Kebot: Robot Simulator

» No matter how simple or complex a program is, data has to be stored and managed in some way Variables and Constants are used to do this.» These are like shoeboxes, they are containers that store something» Shoeboxes come in different sizes and types depending on what shoe is being put in them. You wouldn t have one box for every shoe» This is similar to Variables and Constants - you have different types depending on the size and type of the data being stored

» Variables are used to store and manage data that is able to change while the program runs» Constants are used when you want to assign a value to a an object once and make it so this value can t change» Both Variables and Constants have their own naming conventions and must be given an initial value when declared

» Characteristics of Variables: They store data in computer memory for later use Their lifespan is limited by the duration of the program Data stored in the variable will be lost when the program ends

1. To use a Variable the type must first be declared 2. The Variable must then be given a name 3. The Variable must then be given an initial value 1. Type 2. Variable Name 3. Initial Value» Challenge: Clear your own code from the Gates class. Declare a integer Variable called duration with an initial value of 3

» Challenge: Create a short program which uses the forward() and backward() methods. However, instead of passing a literal value to these methods (e.g. forward (3) ; ) pass the value of the variable you created (e.g. forward (duration); )» Note how this Variable contains the value you assigned it when you declared the Variable» Challenge: Observe what happens when the robot tries to leave the bounds of the arena. Declare the duration Variable to a higher value whilst instructing the robot to move

» As already stated, Variables store data in computer memory for later use are limited by the duration of a program lose stored data when the program ends» Challenge: Select Display Information Panel from the Display Menu then run Kebot

» Note how Values of Variables can change as a program runs How when Kebot is reloaded the value of the Distance Travelled Variable is never stored and is limited by the duration of the program A small amount of additional code puts variables into a window like this. You ll be using the Distance Variable shortly

» Occasionally you may only want to assign a value to a an object once and make it so this value can t change» Values like 90 degrees for a right turn or the value of Pi never need to be modified» Constants are used to store such information

» Constants are also useful when you want to modify code that is repeated many times.» For instance, with the following code 4 changes have to be made if you want to change the amount the robot rotates:

» However, when a constant is used only 1 change to the code has to made:

1. To use a Constant you must first use the final keyword 2. The Constant must then be given a type 3. The Constant must be given a name 4. The Constant must be given an initial value 1. Final Keyword 2. Constant Type 3. Constant Name 4. Constant Value

Kebot: Robot Simulator

» Expressions are used when you want to evaluate whether one or more conditions is true or false» Expressions can range from simple mathematical calculations to something more complex» Expressions are made up of Variables, Methods and Operators» Operators allow you to better interact with the Variables in your code

» Differs from Gates in several ways: The information pane automatically loads when Berners is selected Berners starts from the same position each time in the arena Code is continually executed in a loop» Challenge: Load Kebot and select the Pauser Arena. Select the Berners Robot. Start the simulator

» The Berners class comes with the following code: x is equal to 1 if x is greater than 0 then go forward» A Variable called distance has also been declared for you. This is of type double which is the standard data type when you use decimal point numbers. This variable contains how far the robot has travelled (in m)

» Challenge: Delete the variable declaration int x = 1 from your code. Now instruct Berners to move forward if distance is less than 1 metre by using the distance Variable which has been declared for you.» Tip: To do this only three changes have to be made to your existing code all of these in the condition of the if statement (e.g. if ( make changes here ) )» Comparison Operators: Greater Than > Less Than <

» The if statement you have used only executes code if a condition is true» The if else if statement, however, can be used to select which block of code to execute: New Code

» The if else if else statements allow for even more conditions to be evaluated» Else statements will always be executed if the preceding conditions are not true. Hence conditions have to be ordered logically.

» The if statement can also compare two values: if distance is greater than 1.0 AND distance is less than 2.0 variable name operator value comparison

» Challenge: Follow the template and instruct Berners as follows: if distance is less than 1.0 move forward else if distance is greater than 1.0 AND less than 1.2 pause for 5 seconds then move forward for 10 seconds else stop

» Challenge: Modify your code as follows: if distance is less than 1.0 move forward for 1 second else if distance is greater than 1.0 and distance is less than 1.2 pause for 5 seconds then move forward 4 seconds else if distance is greater than 1.5 and distance is less than 1.7 pause for 5 seconds then move forward 6 seconds else stop

» Challenge: Load the Shapes arena. Attempt to program Berners to follow the instructions that are provided. You will need to use a if else if else if else statement to solve the task

» Expressions are used when you want to evaluate whether one or more conditions is true or false» Double is the standard data type when you use decimal point numbers» if else if statements allow the program to select which block of code to execute if a condition is true» if statements can also compare values: e.g. if ( distance > 1.0 && distance < 2.0 )

Kebot: Robot Simulator

» As you will see during the workshop, programs are very good at performing repetitive tasks. Counting objects is a good example» A common programming task is to increase a variable's value by 1, or to decrease by 1, each time a certain condition is met» Increasing by 1 is called incrementing.» Decreasing by 1 is called decrementing.

» Challenge: Declare an class variable called count with the value 0. To do this declare the variable here:» Scroll to the bottom of the Berners class. Copy the Line Counter Example Code and place this in robotcommands()» Run Kebot and Load the Line Counter Arena from the File Menu

» Your code should compile but Berners will not perform an action» First you must instruct your robot to move forward» Second you must use the count variable to count the number of times a 2D object is encountered. Do this by declaring: or» Challenge: Instruct the robot to move forward and to add 1 to the count variable each time a 2D object is detected. Where do you think these two pieces of code should go?

» Challenge: Launch Kebot. Draw several 2D freehand lines within the designated space that is drawn. Observe the result

» It is possible to nest if statements (as well as if Else, if else if statements etc). Nesting an if statement just means putting one if statement inside another:» Challenge: Declare another if statement (in the body of the if you already have) which calls the endcount() method if the count is greater than a number of your choosing.

» A Boolean variable is used in the Line Counter example» Boolean variables have only two values: true or false» During the Line Counter task the if would only execute if an object is detected (i.e. the condition is true)» Booleans are declared like the other variables: 1. Type 2. Variable Name 3. Initial Value

» Is the standard output function used in JAVA. It is very useful for testing and development purposes and prints textual output to the terminal window similar to a text box» Challenge: Declare a Boolean called status with an initial value of false. In your existing if statement remove your previous code and declare status to be true.» Below this if statement type:

» Challenge: Load the Line Counter Arena. Observe how the status of the robots sensors are constantly evaluated and that this value is printed to the terminal window

» Nested if statements simply involves putting one if statement inside another» Boolean variable have only two values: true or false» System.out.println is the standard output function used in JAVA and allows information (such as the value of variables) to be printed to the terminal window. This can be useful for testing and development purposes.

Kebot: Robot Simulator

» So far you have used several methods in your code - forward(), backward(), left(), right() etc.» During this part of the workshop you ll be using a set of more advanced methods. These allow greater flexibility when programming your robots» You ll also be using a new robot Jobs

Method move() turn() Details Used to move forward or backward Speed is passed to method and can range 0 0.3 m/s To move forward pass a positive value - e.g. move (0.1) To move backward pass a negative value - e.g. move (- 0.1) Used to turn left or right Rotation speed is passed to method (up to 180 degrees per second) To turn left pass a positive value - e.g. turn (90) To turn right pass a negative value - e.g. turn(- 90) Instructs how long to perform the previous command for: duration() e.g. move forward at 0.1m/s for 10 seconds rotate 90 degrees for 1 second

» The robotcommands method where you will be coding continually loops around. If you have the code» The robot won t stop moving forward after the 10 seconds have passed due to this code returning to the beginning of the code again» The pause() or stop() methods must be used at the end of your code if you do not want the code to run again

» The Jobs robot comes pre-loaded with some partially complete code. Expand this code so that it resembles:» Challenge: Can you modify this code so that you robot performs a figure of 8?

» allow greater interaction with surroundings. For instance, robots can be programmed to seek or avoid objects» Challenge: Load Kebot, select Jobs. Select the Sensor Coverage option from the Display menu» The leftsensorvalue() and rightsensorvalue() methods are used to calculate the distance between the robot and 3D objects. 3D objects are like walls - they cannot be climbed and are brick coloured

» Challenge: Using the System.out.println command print the value of the left or right sensor to the terminal window. Variables (of type double) called leftsensor and rightsensor have already been declared for you» What is the maximum range of these sensors?» Introduce 3D objects into the arena and move Jobs closer to these. Observe how when the simulator runs the values stored in these variables change

» This is the value you will be working with when using the proximity sensors i.e. if an object is within 0.1m pause, if an object is within 0.3m of the right sensor turn etc

» The following code instructs the robot to move forward if nothing is within 0.5m, otherwise to stay still:» Challenge: Modify this code so that your robot exhibits scared behaviour (i.e. the robot moves back if a 2D object is too close). Test your code by moving a 2D object near to your robot while the simulator is running

» Challenge: Program an if else if else statement that enables the robot to simulate curious behaviour: If the robot is within a certain distance of an object move back Else if the robot is a certain distance away from an object move forward Else stay still» Challenge: Test your code works as expected when a 2D object is introduced in the arena. Use the Move Environment tool to move objects you have drawn.

» Challenge: Program an if else if else statement that enables the robot to simulate avoiding behaviour: If the left sensor detects an object stop moving and turn right 45 degrees for one second Else if [Your Code Here] Else move forward but don t rotate» Challenge: Test your code works as expected when a 2D object is introduced and moved in the arena

» Challenge: Load Kebot. Draw a large square in the arena:» Challenge: Attempt to program your robot so that it is able to follow the walls within this square

» The solution will probably be a if else if else statement (although there is more than one way to solve the task) If nothing is detected close move forward Else if something is close turn for 1 second then stop turning Else stop turning and move forward» This task bears similarities to a maze solving one

Kebot: Robot Simulator

» You have already encountered 3 data types» However, there are 5 additional types that need to be briefly considered» The Data Types Helper Program allows you to explore different aspects of these data types

» Is a stand alone program with several features» It can be used to explore several aspects of programming» Load the Helper Application» The program comes with a Help Sheet which contains information about some of the data types you have already used

» When you enter in the Helper Application the optimal type for that data is provided» Information relevant to the data type is also provided including information of other types that could be used» There is also the option to hide the Help Sheet

» Two new data types are listed on the Help Sheet: Strings and Char:» Strings are used to display/contain textual information e.g. Hi Hello» Char is used to display single characters e.g. a b c

» As you know, int is the data type that is normally used to declare whole numbers» However, there are three other numeric data types that can be used: byte, short, long» Different data types exist as each one allocates a different amount of memory when a program starts. When memory saving really matters (e.g. on a spaceship s computer, mobile phone applications) this can be crucial remember the shoe boxes» However, for our purposes we will only be using the int data type during the workshop for whole number values

» Challenge: Using the Helper Application to enter different kinds of data, experiment to find out the following What must you do to text you want to declare as a String? What must you do to a character you want to declare as a Char? What are the value ranges of the byte and short data types? What are the value ranges of the int and long data type? There is an eighth data type that can be used with floating point numbers. Can anybody find the name of this and how to declare it?

What must you do to text you want to declare as a String? What must you do to a character you want to declare as a Char? What are the value ranges of the byte and short data types? Byte: -128 to 127 Short: -32,768 to 32, 767 What are the value ranges of the int and long data type? Int: -2,147,483,648 to 2,147,483,647 Long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 There is an eighth data type that can be used with floating point numbers. Can anybody find the name of this and how to declare it? Floats are used instead of double to save memory. We do not use this data type for the exact values such as currency.

Data Type Used For Range Byte Whole Numbers -128 to 127 Short Whole Numbers -32,768 to 32,767 Int Whole Numbers -2,147,483,648 to 2,147,483,647 Long Whole Numbers -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Float Decimal Numbers Up to 2 100 Double Decimal Numbers Up to 2 1000 or even more Char Characters Individual Characters String Strings String of Characters Boolean True/False True/False

Kebot: Robot Simulator

» Are useful when we want to execute a single block of code several (or possibly hundreds) of times» Examples include constantly checking the state of a robots sensors or waiting until valid input is entered by the user» The robotcommands() method where you have been coding is an example of a loop. The loop has been designed so the robots sensors are constantly interrogated» Challenge: Print line the value of the secondssincestartof 2012() method in Jobs in order to observe first hand how quickly loops execute

» Challenge: Load the Gosling robot and you will find the following example of a while loop Power initially 5 While power is not equal to 0 End the program once it breaks out of the loop

» Challenge: Change the program so that each time the loop is executed the power variable decreases by one» The previous example highlights how a program can not proceed until the condition in the loop has been satisfied. The program is essentially stuck in a loop until this happens» Endless loops can cause severe problems. As the robotcommands() loop shows, however, not all endless loops are bad

» Challenge: What do you think would happen if the System.exit(0) command was deleted?

» Do While Loops are largely similar to While Loops with one major difference the place in the Loop where the condition is tested» While Loops test the condition is true before looping. In theory the code in the loop may never be executed.» Do While Loops execute the body of the loop at least once as the condition is tested at the end of the loop

» Challenge: Adapt your existing code so that it becomes a Do While Loop according to the template» Challenge: Add a second pop up message which informs the user the program will end once the condition in the loop has been met

» Loops are used to execute a single block of code several (or possibly hundreds) of times» While Loops test the condition is true before looping. In theory the code in the loop may never be executed.» Do While Loops execute the body of the loop at least once as the condition is tested at the end of the loop» Endless Loops can be both a good and bad thing

Kebot: Robot Simulator

» have been used extensively during the workshop thus far. However, you have yet to create your own methods from scratch» Challenge: Using the above template create a method called circledrawer. Your method will be placed above the robotcommands() method in the Jobs class. In this method code your robot to perform a Circle. Call this method from within robotcommands().

» Methods are particularly useful when you need to use the same piece of code again and again but don t want to keep repeating your code» Challenge: Create the two following methods:» Challenge: Refering only to these two methods in robotcommands(), instruct Jobs to perform a Square

This is exactly how almost all of the methods you have used were created. They simply contain a set of instructions that can be called over and over and vastly simplifies programs

» Challenge: Create a method called robotdance(). Using move(), turn() and duration() program your robot so that behaves in an unusual manner» Discuss amongst yourselves whose robot exhibits the most unusual behaviour

Kebot: Robot Simulator

» You have already encountered two types of Loop: While and Do While» The final JAVA loop, the For Loop, is probably the most powerful looping mechanism and has three parts: initialisation test increment/decrement

» The For Loop provides a way to iterate over a range of values. It is called a For Loop due to it repeatedly looping until a particular condition is satisfied.» The initialisation expression initialises the loop» When the test expression evaluates to false, the loop ends.» The increment/decrement expression is invoked after each iteration through the loop

» Challenge: Copy the below code into the Gates Class. Run the program» Loops help to make a programmer s life easier. The alternative to using the above loop would be: Challenge: Modify your code so that it counts UP (from 0) to 100,000. Display the count using System.out.println

» In the same way that the value of i is passed to System.out.println, this value can also be passed to the movement methods that you have used previously:» Remember, the value of i changes each time the loop is executed. In the previous example i increased each time the loop executed until 100,000 was reached:

» Challenge: Load the Black Hole arena. Using a For Loop instruct Gates to perform a spiral and simulate falling into a Black Hole. Code to get you started

» The key to solving this task is to gradually increase the amount that the robot rotates each time the loop runs» The robot need not rotate by more than 100 Degrees» The robot should move forward at a constant speed» Don t worry about trying to follow the Black Hole line precisely concentrate on trying to simulate the falling behaviour

» To write out each line of code manually» Filled 308 lines of code» 15 pages in Microsoft Word

Kebot: Robot Simulator

» To this point you have dealt with a relatively small number of variables in your code» Whilst it is possible to manage small numbers of variables, as programs expand and more information needs to be dealt with declaring individual variables becomes cumbersome» Arrays provide a neat and simple way of storing information of the same type

» Like declarations for variables, an array declaration has two components: the Array's type and the Array's name:» An array must then be created (by using the new operator) and the size of the array (how many Variables to create) must be declared:» An Array therefore declared as follows:

» During this task the robot will read a Barcode which you will drawn on screen» The robot will move from left to right storing the precise location at which it finds each line of the barcode in an Array» The Array will store the location (distance) each time it detects a line of the barcode

» Each element in the Array must then be given a value. One way to do this is:» Challenge: Declare an Array of type double called locationofbarcode. The Array should have 10 elements. Declare this array as a class Array. Use the Berners robot.» Declare an int called count with an initial value of 0 as a class Variable. Some of you may already have this code.

» Challenge: In the if (barcodedetected()) section of your code you need to program the following : Set the value of the Array you declared at the count position. Assign the array to store distance. Remember how to refer to array elements: Increment the count variable by one

» At the moment your code does not contain a way to report the contents of the array once the barcode has been scanned.» Challenge: Below your existing code declare an If statement which is executed if distance is greater than 2.9. Within this If use System.out.println to print each element of the Array. Using the previous example you would refer to each array element as follows:» Below where you print each Array element use the System.exit(0) command to end the program

» Accessing the contents of Arrays each element at a time can quickly become tiresome. Imagine accessing an Array of 100 or 1000 elements in such a manner!» For Loops are particularly helpful (and powerful) when used in conjunction with Arrays.» Remember your previous code:

» Challenge: Modify your code so that you use a For Loop to access and print each element of the array. The previous example should help:» You still need to use System.exit(0) to end the program and this should come after your For Loop.» You may want to add a String to your code to make it more informative.

Bill Gates: A founder of Microsoft Tim Berners-Lee: Father of the web and champion of IT freedom Steve Jobs: A founder of Apple and creator of imac, ipod, iphone and ipad James Gosling: Known as the father of JAVA Larry Page: One of the co-founders of Google