Java using Robots II

Uploaded from authorPOINTLite
Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Inheritance in Java using Robots: 

Inheritance in Java using Robots ACSE 2003 Presentation Michael Devoy, St. David C.S.S., Waterloo mdevoy@look.ca

Objectives: 

Objectives Use Robots to: understand inheritance in Java use instance variables overload methods (including the constructor) override inherited methods use arrays

Robot Inheritance: 

Robot Inheritance Use inheritance to create a new type of robot with additional abilities (smarter robots) the child class inherits all the attributes and behaviours of the parent class, except the constructor Java uses the keyword ‘extends’ to indicate inheritance benefits: reduce the amount of code we need to write make our program easier to understand facilitate the re-use of code aid in debugging our program

The Problem - the long relay: 

The Problem - the long relay Similar to yesterday’s problem, but longer, would be tedious to code

Pattern to define a Robot child class: 

Pattern to define a Robot child class import becker.robots.*; public class <<ClassName>> extends Robot { // define the new Robot’s constructor method public <<ClassName>> (City city, int ave, int str, int dir, int numThings) { super(city, ave, str, dir, numThings); } << other new methods>> } ClassName is the name of the new Robot class Super refers to the parent, in this situation the parent’s constructor is called

Pattern for new Method: 

Pattern for new Method public <<returnType>> <<methodName>>(<<parameterList>>) { << list of statements to execute>>; } Access may be public (available to any class in your program) or private (available only to objects of this class) returnType may be any Java primitive variable type (int, boolean, etc) or class, which is returned by the method, or void For Example public int countThings() { int count = 0; while (this.isBesideThing()) { this.pickThing(); count++; } for (int i=0; i < count; i++) this.putThing(); return count; }

Overloading Methods: 

Overloading Methods Overloading a method means to create a new method with the same name as an existing method, but with a different list of parameters. Example: move() is a method in the Robot class define a new method move(int 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 Java distinguishes between them by the parameter list

Hands on Inheritance: 

Hands on Inheritance Start Ready to Program Open RobotRelay2.java this is the application class with the main method Open SmarterRobot.java this is the child class it already contains a constructor and a helper (private) method named turnRight() Create the methods: stepUp(), stepDown() and move(int steps) Run the RobotRelay2 program Overload the constructor, to set the number of Things in the SmarterRobot’s backpack to 0 Use this constructor in RobotRelay2.java

Pattern for Instance Variables: 

Pattern for Instance Variables An instance variable is a variable defined in a class which is global to all methods in the class Each object (instance) created from the class has its own copy of the instance variables Instance variables should be declared ‘private’ private <<variableType>> <<variableName>> = <<initialValue>> variableType may be any primitive type of variable or a class initialValue is optional instance variables are often initialized in the constructor method instance variables’ values are controlled by calls to public methods, ensuring encapsulation

Overriding Methods: 

Overriding Methods Overriding methods replace the definition of an existing method, inherited from a parent 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 super keyword For Example: public void move() { stepsTaken++; // stepsTaken is a private instance variable super.move(); }

Hands on Instance Variables: 

Hands on Instance Variables Start Ready to Program Open WanderingRobot.java this is your application class with a main method Create a new class which extends RobotSE create an instance variable named stepsTaken, as an int create a constructor (refer back to SmarterRobot) override the move method to add 1 to stepsTaken add an accessor method called getStepsTaken() which returns the value stored in stepsTaken create a method named resetStepsTaken() which sets the value of stepsTaken to 0 Run the program

Arrays: 

Arrays Java supports arrays of any number of dimensions declare a reference to an array so Java knows its type, name and dimensions: int age [ ]; String name[ ]; Robot karel [ ]; double distances[ ] [ ]; Instantiate the array, so Java allocates RAM for the number of entries age = new int[12];; name = new String[5]; karel = new Robot [2]; distances = new double[5] [7];// note this is an // array of arrays //and need not be square Continued 

Arrays: 

Arrays Initialize each element of the array In the case of primitive variable types, assign values In the case of objects, invoke the constructor note: array elements are numbered starting at zero for (int x=0; x < age.length; x++) age[x] = 0; karel[0] = new Robot (waterloo, 1, 5, Directions.EAST, 0); karel[1] = new Robot (waterloo, 2, 5, Directions.WEST, 0); My Notes on arrays Sun’s Notes on Arrays

Hands on Arrays: 

Hands on Arrays Start Ready to Program Open WanderingRobot.java this is your application class with a main method create an array of 5 SmarterRobots instantiate each SmarterRobot at a different location modify the program so each different SmarterRobot makes a move in turn Run the program

Resources: 

Resources Becker Robots (Documentation & Downloads) http://www.math.uwaterloo.ca/%7Ebwbecker/robots/ Introductory Course using Robots for Students http://csis.pace.edu/~bergin/KarelJava2ed/Karel++JavaEdition.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/onlineTraining/ My ICS4M course http://webhome.idirect.com/~mdevoy/ics4mi