ObjectOriented Programming in Java using Karel - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

ObjectOriented Programming in Java using Karel

Description:

Robot john = new Robot(square, 6, 2, Directions.WEST, 0) ... NewKindOfRobot(City theCity, int avenue, int street, int direction, int numThings) ... – PowerPoint PPT presentation

Number of Views:194
Avg rating:3.0/5.0
Slides: 19
Provided by: sand233
Category:

less

Transcript and Presenter's Notes

Title: ObjectOriented Programming in Java using Karel


1
Object-Oriented Programmingin Java using Karel
Originally written by Sandy Graham, University of
Waterloo Edited by Jaye Herbert, Sir Frederick
Banting S.S.
2
Karel the Robot
3
Using UML in Design
  • every class has a box
  • class name
  • attributes/instance variables
  • behaviours/methods
  • Arrows indicate the relationships among classes
  • indicates a 0-many relationship
  • other symbols not shown here

4
Karel the Robot
http//www.student.math.uwaterloo.ca/cs132/Doc/Ka
rel/
5
Basic Commands
  • move moves forward 1 block
  • turnLeft pivots 90 counter clockwise
  • pickThing picks up object over which it is
    standing and places it in his backpack
  • putThing takes an object out of his backpack
    and puts in current corner
  • Ex. Move Karel 2 blocks and pick up thing
  • Robot Karel new Robot (1,1, Directions.east,0)
  • Karel.move()
  • Karel.move()
  • Karel.pickThing()

6
A First Program Using Karel
Import any libraries.
Create a class.
Include main method.
Create some objects.
Make the objects do something.
7
Second Program Using KarelMultiple Instances of
Robots
8
Multiple Objects
  • import becker.robots.
  • public class HarvestAroundSquare4 extends Object
  • public static void main(String args)
  • City square new City("HarvestAroundSquare.
    txt")
  • Robot karel new Robot(square, 1, 5,
    Directions.EAST, 0)
  • Robot mary new Robot(square, 2, 1,
    Directions.SOUTH, 0)
  • Robot john new Robot(square, 6, 2,
    Directions.WEST, 0)
  • Robot sue new Robot(square, 5, 6,
    Directions.NORTH, 0)
  • KarelFrame frame new KarelFrame(square)
  • for(int counter 0 counter lt 3
    counter)
  • karel.move()
  • karel.pickThing()
  • mary.move()
  • mary.pickThing()
  • john.move()
  • john.pickThing()
  • sue.move()
  • sue.pickThing()
  • // end for

9
Practice
  • Edit the preferences to add to your classpath
    x\apps\compsci\java\classes
  • Using the sample class, robot docs and the basic
    commands
  • Create a program which has 2 robots
  • Edit the city text file to place a thing in every
    other square
  • Start them in opposite corners and have them pick
    up the things
  • Have one of them move more slowly

10
Karel Problem 3
11
Making a Smarter Robot(Inheritance)
  • add functionality through new methods
  • inherit all instance variables and methods from
    the super class
  • new functionality
  • turnRight()
  • putThingOnNextCorner()
  • pickAllThings()

Robot
12
Constructors
  • a special method for creating an instance of the
    class
  • no return type
  • has the same name as the class
  • may be overloaded
  • major task is to initialize the instance variables

13
Karel Problem 4More Complex Methods
  • parameters
  • always passed by value
  • sometimes the value is a reference
  • all method calls include the implicit parameter
    (this)
  • two major types of variables primitive and
    object
  • overloading methods
  • unique method signatures
  • overriding methods
  • inheritance hierarchy
  • objects always know what they are

14
Overloading Overriding
import becker.robots. public class ShowSpin
extends Object public static void main
(String args) City aCity new
City() KarelFrame frame new
KarelFrame(aCity) SpinningRobot sandy
new SpinningRobot (aCity, 1, 5,
Directions.EAST, 0) for (int i 0 i lt
3 i) sandy.move()
sandy.turnLeft() sandy.move(3)
15
Passing Objects as Parameters
import becker.robots. public class ObjParam
extends Object public static void
main(String args) City myCity new
City("Slave.txt") Mover karel new
Mover(myCity, 1, 4, Directions.NORTH, 0)
Robot tubman new Robot(myCity, 2, 4,
Directions.NORTH, 0) KarelFrame f new
KarelFrame(myCity, 4, 5)
karel.move(tubman, 4) karel.move(tubman,
4)
public class Mover extends Robot ... public
void move(Robot slave, int dist) while(dist
gt 0) slave.move() dist--

16
Modifiers
  • public -gt visible to everyone
  • used for class definition, methods which are part
    of the interface of the object
  • private -gt visible only within the class file
  • used for instance variables and helper methods
  • also protected and blank visibility modifiers
  • static - class-wide (not instance)
  • final - constant

17
Making More Complex Objects
  • A CourierRobot will deliver packages to various
    locations. It has a home base, and will be
    supplied with the address and package (some
    number of things) for delivery. The robot must
    calculate the cost of delivery base rate
    distancecost/step weightcost/kg.

Create a new kind of robot to handle this task.
Develop a UML diagram, and implement the class.
18
Partial UML for CourierRobot
Write a Comment
User Comments (0)
About PowerShow.com