Title: Introduction to Java using Robots
1Introduction to Java using Robots
- ACSE 2003 Presentation
- Michael Devoy, St. David C.S.S., Waterloo
- mdevoy_at_look.ca
2Objectives
- Understand the world of Karel the Robot
- Understand how Karel the Robot can be used to
teach object oriented programming in Java - write simple programs to instruct a Robot to
accomplish a task that involve - creating a Java application
- instantiating objects, including Robots
- invoking methods of a Robot
3Why Use Robots
- Fun
- Visual
- Early introduction to Object Oriented concepts
- Teach important concepts without getting stuck in
too much Java syntax - Students start by using objects before needing to
learn how to build classes - Teachers dont have to build their own classes
for students to use - Availability of resources, both free and
purchasable
4The World of Robots
- Robots live in a City
- A City is displayed in a CityFrame
- A City has intersections of streets and avenues
- Things may be on an intersection
- Robots may pick up or put down Things
- Walls may be next to an intersection
- Robots can not walk through Walls (they break)
5The World of Robots UML
Wall
Thing
6A Typical Robot Problem
- The teacher designs the city layout and poses the
problem - The student writes a program that instructs a
robot (or robots) to complete the task - For example walk around the square and pick up
all Things
7Getting Started
- An environment to create Java programs in
- Holts Ready to Program
- OR
- Suns SDK
- Any editor (such as notepad or text pad) or IDE
(such as BlueJ or JBuilder) - JAR file containing the required Robot classes,
available free at http//www.math.uwaterloo.ca/7E
bwbecker/robots/
8Java Background
Source File (MyClass.java)
- Java was created by Sun Microsystems and is free
(http//java.sun.com/) - The SDK (system developers kit) is used to
compile and run Java applications and applets - A Java program is composed of a number of classes
you write, that work together - Most programmers use an IDE, such as Ready
Java Compiler
Byte Code File (MyClass.class)
JVM
9Pattern for a Java Application
- import ltltimportedPackagegtgt
- public class ltltClassNamegtgt extends Object
-
- public static void main(String args)
-
- ltlt list of statements to be executedgtgt
-
For Example import becker.robots. public class
HarvestAroundSquare extends Object public
static void main(String args)
10Pattern for Instantiating an Object
- ltltvariableTypegtgt ltltobjectNamegtgt
- ltltobjectNamegtgt new ltltClassNamegtgt(ltltparametersgtgt)
runs the constructor method to assign values to
object variables and assign a value to the
reference
Or, on one line ltltvariableTypegtgt ltltobjectNamegtgt
new ltltClassNamegtgt(ltltparametersgtgt)
For Example Robot karel karel new Robot
(waterloo, 1, 5, Directions.EAST, 0) Or Robot
karel new Robot (waterloo, 1, 5,
Directions.EAST, 0)
11Pattern for Calling an Objects Method
Dot between object and method
- ltltobjectNamegtgt.ltltmethodNamegtgt(ltltparametersgtgt)
Method always followed by ( ), which may or may
not contain parameters
For Example karel.move()
Other methods a Robot can perform
include turnLeft(), pickThing(), putThing(),
frontIsClear() http//www.math.uwaterloo.ca/bwbe
cker/robots/doc/becker/robots/Robot.html
12Putting the Patterns Together
- import becker.robots.
- public class HarvestAroundSquare extends Object
-
- public static void main (String args)
-
- City waterloo new City ("HarvestAroundSqua
re.txt") - CityFrame frame new CityFrame (waterloo)
- Robot karel new Robot (waterloo, 1, 5,
Directions.EAST, 0) - int side 0
- while (side lt 4)
-
- while (karel.frontIsClear ())
-
- karel.move ()
- karel.pickThing ()
- //end while
13Hands on Application Modification
- Start Ready to Program
- open HarvestAroundSquare.java
- predict what the program will do
- run the program to verify your prediction
- identify the logic error
- modify the program to correct the logic error
- run your modification to ensure it is correct
- open HarvestAroundSquare4.java, to see how a team
of robots can work together
14Hands on Application Creation
- Use HarvestAroundSquare.java as a resource
- Use the file relayCity.txt to setup Walls and
Things - set the number of streets and avenues in the
CityFrame using parameters in the constructor,
such as - CityFrame frame new CityFrame(waterloo, 20,
10)
15Hands on Application Creation
- Make the first robot pick up all the Things on
the stairs and then drop them on the corner where
the second robot is standing - Make the second robot pick up all the Things and
distribute them on its stairs
16Resources
- Becker Robots (Documentation Downloads)
- http//www.math.uwaterloo.ca/7Ebwbecker/robo
ts/ - Introductory Course using Robots for Students
- http//csis.pace.edu/bergin/KarelJava2ed/Kar
elJavaEdition.html - Sun Java Class Documentation
- http//java.sun.com/j2se/1.4.2/docs/api/index
.html - Sun Java Tutorials
- http//developer.java.sun.com/developer/onlin
eTraining/