Download presentation
Presentation is loading. Please wait.
Published byKevin Wilkerson Modified over 9 years ago
1
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca
2
Objectives 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 Robot’s methods
3
Why Use Robots Fun Visual Early introduction to Object Oriented concepts Students start by using objects before needing to learn how to build classes Teachers don’t have to build their own classes for students to use Free lesson plans and assignments at http://csis.pace.edu/~bergin/KarelJava2ed/Karel++JavaEdition.html#toc http://csis.pace.edu/~bergin/KarelJava2ed/Karel++JavaEdition.html#toc
4
When 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
5
The 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)
6
Getting 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/Karel.htm (click on the ‘here’ link and copy to anywhere you wish on your hard drive)http://www.acthompson.net/DotNet/Karel.htm The vjslib.dll file downloaded free from the Internet at http://msdn.microsoft.com/vjsharp/downloads/howtoget/d efault.aspx (use the 1.1 version) and installed into Windows http://msdn.microsoft.com/vjsharp/downloads/howtoget/d efault.aspx
7
Assignment 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.
8
Starting 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’
9
Starting 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
10
Add 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()’
11
Add 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!
12
Pattern for Creating a Robot Object Dim karel as ur_Robot karel = new ur_Robot (1, 6, Directions.North, 0) Declare a variable named karel as a reference to a Robot kind of Object run the constructor method of the Robot, to assign values to Robot Object and assign a value to the karel reference
13
Pattern for Calling a Robot Object’s Method karel.move() 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() The object’s name always begins the statement (karel)
14
Your Turn Now Complete the code to have the Robot perform its task Run your program to ensure it functions correctly
15
Assignment 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 Parent’s 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
16
A 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
17
Starting 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 Assignment2’s bin folder Add the references for vjslib.dll and kareltherobot.dll
18
Starting a new Class Click Project | Add Class Ensure ‘class’ is highlighted Enter a name for the new class e.g. RightTurnerRobot Click open
19
Pattern to define a Robot child class 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 > End Class RightTurnerRobot is the name of the new Robot class, a name we choose MyBase refers to the parent, in this situation the parent’s constructor is called Inherits is the VB keyword to indicate this class is a child of the ur_Robot class The constructor method must be named New
20
Pattern for new Method Access may be public (available to any class in your program) or private (available only to objects of this class) Public Sub turnRight() turnLeft() End Sub Parenthesis required after method name, may contain parameters or be empty
21
Your Turn Now Complete the turnAround method in the TurnRightRobot’s class Edit the code behind the form (refer back to Assignment1) Make the following changes to the form’s 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
22
Assignment 3 – Overloading Methods To overload a method, means to create a method with an existing method’s 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
23
A 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
24
Starting 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 Assignment3’s bin folder Add the references for vjslib.dll and kareltherobot.dll
25
Starting 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
26
Pattern for Overloaded Methods Overloads is a VB keyword indicating the ur_Robot’s move method is being overloaded – a new move method is being defined with a different parameter list Public Overloads Sub move(byval steps as Integer) dim x as Integer for x = 1 to steps myBase.move() next End Sub myBase.move() executes the move method in the base class (ur_Robot)
27
Your Turn Now Complete the move method in the MileWalkerRobot’s class Edit the code behind the form (refer back to Assignment1) Make the following changes to the form’s 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
28
Assignment 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 parent’s method, if needed, by use of the MyBase keyword For Example: Public Overrides Sub move() { turnLeft() turnRight() myBase.move() }
29
A 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 360 0, 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
30
Starting 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 Assignment4’s bin folder Add the references for vjslib.dll and kareltherobot.dll
31
Starting 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
32
Pattern for Overriding Methods Overrides is a VB keyword indicating the ur_Robot’s move method is being overridden – a new move method is being defined in the DizzyWalkerRobot to replace the original move Public Overrides Sub move() Dim i As Integer For i = 1 To 4 turnLeft() Next MyBase.move() End Sub myBase.move() executes the move method in the base class (ur_Robot)
33
Your Turn Now Complete the move method in the DizzyWalkerRobot’s class Edit the code behind the form (refer back to Assignment3) Make the following changes to the form’s 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.