33
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge [email protected]

Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge [email protected]

Embed Size (px)

Citation preview

Page 1: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

Introduction to OOP in VB.NET

using RobotsACSE Conference, Nov 2004

Michael Devoy

Monsignor Doyle C.S.S., Cambridge

[email protected]

Page 2: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 3: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 4: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 5: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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)

Page 6: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

Getting StartedYou 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)

The vjslib.dll file downloaded free from the Internet at http://msdn.microsoft.com/vjsharp/downloads/howtoget/default.aspx (use the 1.1 version) and installed into Windows

Page 7: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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.

Page 8: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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’

Page 9: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 10: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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()’

Page 11: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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!

Page 12: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

Pattern for Creating a Robot ObjectDim 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

Page 13: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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)

Page 14: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

Your Turn Now

Complete the code to have the Robot perform its task

Run your program to ensure it functions correctly

Page 15: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 16: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 17: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 18: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

Starting a new Class

Click Project | Add Class

Ensure ‘class’ is highlighted

Enter a name for the new class e.g. RightTurnerRobot

Click open

Page 19: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

Pattern to define a Robot child classImports 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

<< other new methods>>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

Page 20: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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() turnLeft() turnLeft() End Sub

Parenthesis required after method name, may contain parameters or be empty

Page 21: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 22: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 23: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 24: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 25: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 26: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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)

Page 27: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 28: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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()}

Page 29: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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 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

Page 30: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 31: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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

Page 32: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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)

Page 33: Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge mdevoy@look.ca

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