{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Unit2 - GridWorld Summer 2007 Unit 2 Special Bugs Unit 2...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
GridWorld Summer 2007 Unit 2 ---- Special Bugs Unit 2 ---- Special Bugs To the teacher In Part 2 of GridWorld, the Bug and BoxBug code will be “opened” and modified to create specialized bugs. Students can develop a basic understanding of inheritance by extending an existing class. New bugs will be created by overriding the act() method using the methods specified in the Bug class. New methods are not defined. The exercises are targeted to help students understand inheritance as they strengthen their understanding of problem specification and algorithm development. Objectives Materials in this chapter can reinforce the following concepts: 1. Use of instance variables 2. Extending another class 3. Overriding methods 4. Understanding the relationship of superclass and subclass 5. Using parameters effectively 6. Creating a runner or driver class to execute your own class 7. Using overloaded methods (in the runner) 8. Understanding the function of constructors 9. Developing algorithms to meet different specifications 10. Use of arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
To the Student We will explore the powerful notion of inheritance. We, as humans, inherit from our parents but imagine if we inherited everything from one of our parents; the exact looks and actions. (ooh…my poor children!) In Java, a class that extends another does inherit everything from that class. We must first understand the relationship between a superclass and a subclass . We know from Unit 1 that Bug, Flower and Rock are Actors. These three classes extended the Actor class and so these subclasses inherited all the instance variables and methods from Actor. So now we ask: Why did the bug act differently than the rock or the flower? The act method was invoked when we clicked Step. Let us look at the act() method for the rock. There is no code thus there were no instructions for the rock. What about the act() method for the flower and the act() method for the bug? Each of the classes, Rock, Flower and Bug, inherits everything from the superclass Actor and overrides the act() method to create its own behavior in the simulation. The Bug class provides three additional methods that specify how a bug moves and turns. Overriding the act method using these specific methods creates a “special “ Actor. In this unit we will create classes that extend the Bug class. Various behaviors will be created by overriding the act() method using only the methods specified in the Bug class. For any Java program we must have a class that starts the program; for Java applications the”driver class” uses the “public static void main(String[] args)” method. In this case study we call the driver classes “Runner” as in “BugRunner” and “BoxBugRunner.” Whenever you want to run the GridWorld with a new class that extends Bug (or Actor), you will need to write a new runner class. You can simply follow the pattern of the BugRunner and BoxBugRunner classes. You can add an instance of an actor to your simulation using the ActorWorld add method, as shown in the example runner classes.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}