Title: Introduction to OOP in VB'NET using Robots
1Introduction to OOP in VB.NETusing Robots
- ACSE Conference, Nov 2004
- Michael Devoy
- Monsignor Doyle C.S.S., Cambridge
- mdevoy_at_look.ca
2Objectives
- Learn how to introduce OOP within VB.NET using
Karel the Robot - Understand the world of Karel the Robot
- Write simple programs to instruct a Robot to
accomplish a task that involve - creating a VB.NET application
- declare and create Robot object
- execute methods of a Robot Object
- use inheritance to create smarter types of Robots
- overload and override some of a Robots methods
3Why Use Robots
- Fun
- Visual
- Early introduction to Object Oriented concepts
- 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 - Free lesson plans and assignments at
http//csis.pace.edu/bergin/KarelJava2ed/KarelJ
avaEdition.htmltoc
4When to teach Robots
- Possible in grade 11
- After standard programming expectations have been
met (variables, if, loops, functions and
parameters) - Great introduction to OOP for grade 12
- Reinforces concepts that controls on a form are
objects with methods and properties - Answers the question of what should be in a
module for code re-use - Great introduction if teaching Java in grade 12
5The World of Robots
- Robots live in a World
- A World has intersections of streets and avenues
- Beepers may be on an intersection
- Robots may pick up or put down Beepers
- Walls may be next to an intersection
- Robots can not walk through Walls (they break)
6Getting Started
- You need
- Windows XP
- Visual Basic.NET (one copy provided free today)
- The kareltherobot.dll file downloaded free from
the Internet at http//www.acthompson.net/DotNet/K
arel.htm (click on the here link and copy to
anywhere you wish on your hard drive) - The vjslib.dll file downloaded free from the
Internet at http//msdn.microsoft.com/vjsharp/down
loads/howtoget/default.aspx (use the 1.1 version)
and installed into Windows
7Assignment 1 Using Robot methods
- The teacher designs the world layout and poses
the problem - The student writes a program that instructs a
robot (or robots) to complete the task - For example Write a program that will create a
robot facing north at corner 1, 6. The robot
will live in the world found in the file
fetch.txt. The robot should walk around the
walls, pick up the Beeper, and return to the
starting location, facing north.
8Starting Visual Studio.Net
- Start Visual Studio.NET
- Click New Project
- Ensure Visual Basic Project is selected
- Ensure Windows Application is selected
- Browse to a folder to save work in, optionally
make a new folder, e.g. Robot Assignments - Enter a name for the project (a new folder will
be created) e.g. Assignment1
9Starting a VB Program for Robots
- Use Windows Explorer, copy the World file
fetch.txt and the Robot gifs to the bin
folder inside your project folder - Click Project
- Click Add References
- Add references for
- Vjslib.dll
- Kareltherobot.dll
10Add Code Behind the Form
- Access the code editor for the form by double
clicking the form surface - Add imports kareltherobot to top of code, to
allow access to code in kareltherobot.dll - Add a call to a task sub, inside Form1_Load event
- Start a task sub, by typing private sub task()
11Add Code to Display the World
- Inside the task sub, add the following lines of
code - World.readWorld("fetch.txt")
- World.setDelay(20)
- Click the Run button!
12Pattern for Creating a Robot Object
- Dim karel as ur_Robot
- karel new ur_Robot (1, 6, Directions.North, 0)
13Pattern for Calling a Robot Objects Method
The objects name always begins the statement
(karel)
Dot between object name and method name
Method name always followed by ( ), which may or
may not contain parameters
Other methods a Robot can perform
include turnLeft(), pickBeeper(), putBeeper(),
frontIsClear()
14Your Turn Now
- Complete the code to have the Robot perform its
task - Run your program to ensure it functions correctly
15Assignment 2 - Creating a Smarter Robot using
Inheritance
- Inheritance is an important OOP concept
- A new Class is created, based on an existing
Class - The existing Class is the parent or base Class
- The new Class is the child or sub Class
- The Child inherits all the Parents attributes
and methods (except the constructor) - benefits
- reduces the amount of code we need to write
- makes our program easier to understand
- facilitates the re-use of code
- aids in debugging our program
16A Smarter Robot Problem
- Create a new type of Robot that has the abilities
to turn right and turn around - Use this new Robot to solve the same problem as
Assignment 1 - Our task code should become simpler to write and
easier to understand
17Starting a new Assignment
- Remain in the same solution
- Close the form and code editing windows
- Click File Add Project New Project
- Ensure Visual Basic Project is selected
- Ensure Windows Application is selected
- Type a name in the name box e.g. Assignment 2
- Click OK
- Right click Assignment 2 in the solution explorer
window and select set as startup project - Use Windows Explorer and copy the world file and
robot gifs to Assignment2s bin folder - Add the references for vjslib.dll and
kareltherobot.dll
18Starting a new Class
- Click Project Add Class
- Ensure class is highlighted
- Enter a name for the new class e.g.
RightTurnerRobot - Click open
19Pattern to define a Robot child class
RightTurnerRobot is the name of the new Robot
class, a name we choose
- Imports kareltherobot
- Public Class RightTurnerRobot
- Inherits ur_Robot
- Public Sub New (ByVal street As Integer, _
- ByVal avenue As Integer, _
- ByVal direction As Integer, _
- ByVal beepers As Integer)
- MyBase.New(street, avenue, direction,
beepers) - End Sub
- ltlt other new methodsgtgt
- End Class
Inherits is the VB keyword to indicate this class
is a child of the ur_Robot class
The constructor method must be named New
MyBase refers to the parent, in this situation
the parents constructor is called
20Pattern for new Method
Public Sub turnRight() turnLeft()
turnLeft() turnLeft() End Sub
Access may be public (available to any class in
your program) or private (available only to
objects of this class)
Parenthesis required after method name, may
contain parameters or be empty
21Your Turn Now
- Complete the turnAround method in the
TurnRightRobots class - Edit the code behind the form (refer back to
Assignment1) - Make the following changes to the forms code
- Change all ur_Robot to RightTurnerRobot
- Replace multiple calls to turnLeft to turnRight
and turnAround, as appropriate - Run your program to ensure it functions correctly
22Assignment 3 Overloading Methods
- To overload a method, means to create a method
with an existing methods name, but a different
list of parameters (type or order) - This is often done with constructor methods, to
provide a variety of ways of creating an object
(e.g. specifying beepers in bag or not) - karel new ur_Robot (1, 6, Direction.North)
- karel new ur_Robot (1, 6, Direction.North, 10)
- move() is a method in the ur_Robot class
- define a new method move(steps) that has a
parameter that allows us to dictate how many
steps to move - we say the move method is now overloaded
- note we can invoke either of the move methods we
wish, because VB distinguishes between them by
the parameter list
23A More Flexible Robot Problem
- Create a new type of robot that is a child class
of the ur_Robot - This new type of robot can walk multiple numbers
of intersections with its move method - This robot will live in the world, as found in
the file miles.txt - The robot starts at intersection 2,2
- Use this robot to clean the Beepers in front of
it - The robot should stop one intersection past the
last Beeper - Our task method should be very easy to read and
understand
24Starting a new Assignment
- Remain in the same solution
- Close the form and code editing windows
- Click File Add Project New Project
- Ensure Visual Basic Project is selected
- Ensure Windows Application is selected
- Type a name in the name box e.g. Assignment 3
- Click OK
- Right click Assignment 3 in the solution explorer
window and select set as startup project - Use Windows Explorer and copy the world file
(miles.txt) and the robot gifs to Assignment3s
bin folder - Add the references for vjslib.dll and
kareltherobot.dll
25Starting a new Class
- Click Project Add Class
- Ensure class is highlighted
- Enter a name for the new class e.g.
MileWalkerRobot - Click open
- Add imports kareltherobot
- Add the constructor
26Pattern for Overloaded Methods
Public Overloads Sub move(byval steps as
Integer) dim x as Integer for x 1 to
steps myBase.move() next End
Sub
Overloads is a VB keyword indicating the
ur_Robots move method is being overloaded a
new move method is being defined with a different
parameter list
myBase.move() executes the move method in the
base class (ur_Robot)
27Your Turn Now
- Complete the move method in the MileWalkerRobots
class - Edit the code behind the form (refer back to
Assignment1) - Make the following changes to the forms code
- Change all ur_Robot to MileWalkerRobot
- Add the line of code World.setSize(15,15), after
setting the speed of the animation with the
World.setDelay(20) method - Use the new move method to move multiple
intersections at once - Move to the first intersection with a Beeper,
then pick up the Beeper. - In a similar fashion, move and pick up the second
Beeper - Run your program to ensure it functions correctly
28Assignment 4 - Overriding Methods
- To override a method means to replace the
definition of an existing method, which would be
inherited from a parent (base) class - This is accomplished by writing a method
definition with an identical method signature to
the method signature in the parent class - method signature is the method name and parameter
list - You can call the parents method, if needed, by
use of the MyBase keyword
For Example Public Overrides Sub move()
turnLeft() turnRight() myBase.move()
29A Dizzy Robot Problem
- Create a new type of robot that is a child class
of the ur_Robot - This new type of robot overrides the move method,
so that it first turns 3600, then moves forward
one intersection - This robot will live in the world, as found in
the file miles.txt - The robot starts at intersection 2,2
- Use this robot to clean the Beepers in front of
it - The robot should stop one intersection past the
last Beeper
30Starting a new Assignment
- Remain in the same solution
- Close the form and code editing windows
- Click File Add Project New Project
- Ensure Visual Basic Project is selected
- Ensure Windows Application is selected
- Type a name in the name box e.g. Assignment 4
- Click OK
- Right click Assignment 4 in the solution explorer
window and select set as startup project - Use Windows Explorer and copy the world file
(miles.txt) and the robot gifs to Assignment4s
bin folder - Add the references for vjslib.dll and
kareltherobot.dll
31Starting a new Class
- Click Project Add Class
- Ensure class is highlighted
- Enter a name for the new class e.g.
DizzyWalkerRobot - Click open
- Add imports kareltherobot
- Add the constructor
32Pattern for Overriding Methods
Public Overrides Sub move() Dim i As
Integer For i 1 To 4
turnLeft() Next MyBase.move()
End Sub
Overrides is a VB keyword indicating the
ur_Robots move method is being overridden a
new move method is being defined in the
DizzyWalkerRobot to replace the original move
myBase.move() executes the move method in the
base class (ur_Robot)
33Your Turn Now
- Complete the move method in the
DizzyWalkerRobots class - Edit the code behind the form (refer back to
Assignment3) - Make the following changes to the forms code
- Change all ur_Robot to DizzyWalkerRobot
- Use multiple move methods to move to the first
Beeper - Pick up the Beeper.
- In a similar fashion, move and pick up the second
Beeper - Move one more intersection forward
- Run your program to ensure it functions correctly