Compare MileWalker to the basic UrRobot: Compare MileWalker to the basic UrRobot



Yüklə 461 b.
tarix24.12.2017
ölçüsü461 b.
#17027



Compare MileWalker to the basic UrRobot:

  • Compare MileWalker to the basic UrRobot:

  • What is different?

  • What is the same?



STEP 1 Define a new class of robot (see next slide):

  • STEP 1 Define a new class of robot (see next slide):

  • When designing a new class (whether it’s a type of robot, car, bank account, etc.), the first question we are asking ourselves is “What can I steal?” !! In other words, one big benefit of OOD is the concept of code-reuse. There is no need to reinvent the wheel. (by the way, that doesn’t mean to copy your friend’s lab!! )

  • Also, what is different? We need to create new methods to reflect the differences.





import kareltherobot.*;

  • import kareltherobot.*;

  • public class MileWalker extends UrRobot

  • {

  • public MileWalker (int st, int av, Direction dir, int beeps)

  • {

  • super(st, av, dir, beeps);

  • }

  • public void moveMile( )

  • {

  • move(); move(); move(); move();

  • move(); move(); move(); move();

  • }

  • }



import kareltherobot.*;

  • import kareltherobot.*;

  • public class MileWalkerTester implements Directions

  • {

  • public static void main(String args[])

  • {

  • MileWalker bob = new MileWalker(2, 1, East, 0);

  • bob.moveMile(); // new instruction

  • bob.move(); // inherited instruction

  • bob.turnOff(); // inherited instruction

  • }

  • }



The following lines of code may be necessary and may be placed first within the main() method of the client (driver) program:

  • The following lines of code may be necessary and may be placed first within the main() method of the client (driver) program:

    • World.reset();
    • World.readWorld(“MountainPeakWorld.txt");
    • World.setDelay(50);
    • World.setVisible(true);
    • World.showSpeedControl(true);
    • Alternatively, you can place them in a static block (no need to understand what a “static block” is) within your driver class. A static block runs before the main() method runs.


You should end up with two classes:

  • You should end up with two classes:

    • an object class that defines the new type of robot
    • a client class which is used to test the new robot
  • The client program should test several of the methods in the new robot class:

    • Make the robot move a single block, a mile (8 blocks), turn left, etc.
  • Convention: class names begin with a Capital Letter, method names begin with a lowercase letter – if an identifier combines several words, use the “camelCase” technique. It is important to follow convention.)

  • We’ll now demo the whole process in eclipse and look at MileWalker and MileWalkerTester.



Create a BetterRobot class

  • Create a BetterRobot class

    • Create methods to:
      • turnRight(), - turns right (stays on same corner)
      • turnAround(), - turns backwards (stays on same corner)
      • moveBackward(), - moves backwards and remains in same direction
      • moveMile(), - moves 1 mile (8 corners)
      • moveDecaMile() - moves 10 miles (80 corners)
      • Any other methods that might be helpful??
  • Create a small client program named BetterRobotTester to test the BetterRobot class

    • Construct the new robot
    • Move the robot around the screen using inherited methods or the new methods created above


Let’s get different students to write out these methods on the board:

  • Let’s get different students to write out these methods on the board:

    • turnRight()
    • turnAround()
    • stepBackward()
    • moveMile()
    • moveDecaMile()
  • What other methods did you create?

  • How did you test them with a client program?



Encapsulation is the bundling of data with the methods that act on the data.

  • Encapsulation is the bundling of data with the methods that act on the data.

  • The mechanism for encapsulation in Java is the class definition.

  • The data are kept in variables. Methods are defined to do something with the variables

  • Why is putting repetitive code into its one class (encapsulation) better than just leaving it in the client/driver – i.e., what do we gain?



Encapsulation is the bundling of data with the methods that act on the data. The mechanism for encapsulation in Java is the class definition.

  • Encapsulation is the bundling of data with the methods that act on the data. The mechanism for encapsulation in Java is the class definition.

  • Even though we don’t see it, the robot keeps track of data such as the street and avenue it is on, the direction it is facing, and the number of beepers it has. The methods that change this data are defined in the same class (or an inherited class) as the data.

  • For example, the move() method changes the street and address data stored for this robot. The turnLeft() method changes the direction data of the robot.



While creating BetterRobot, we learned that encapsulation promotes Code Reuse. By putting the code into a method, we no longer need to write that code again. In addition, from the client’s point of view, it is no longer concerned with how to turn right – it’s the beneficiary of a concept called Abstraction (we can focus on WHAT, not HOW).

  • While creating BetterRobot, we learned that encapsulation promotes Code Reuse. By putting the code into a method, we no longer need to write that code again. In addition, from the client’s point of view, it is no longer concerned with how to turn right – it’s the beneficiary of a concept called Abstraction (we can focus on WHAT, not HOW).

  • In general, if you find yourself doing a cut-and-paste, then there is a better way to do things – i.e., use procedural abstraction and abstract out the common code, putting it into a method.

  • How did we use abstraction as we created methods in the BetterRobot class?



Technique for writing modules which are concise, correct, easy to read/modify/understand

  • Technique for writing modules which are concise, correct, easy to read/modify/understand

    • Would a general contractor just start building a house – or would he/she break up the task into foundation, frame, electrical, plumbing, etc.? Makes sense, doesn’t it. Explain why from the contractor’s view – use our cs terms we’ve been learning.
  • Write main task first, breaking up the BIG task into smaller tasks (using methods) – then take each method one at a time and also break it up --- continue until each method is compact and singular in focus (cohesion)

  • Look back at what we just did – do you see this re-factoring?



Let’s write a class called DiamondPlanter together using stepwise refinement. It’s like Harvester except the field is diamond shaped and the robot is putting down beepers instead of picking them up. The robot should always place 4 beepers on a diagonal. Assume the robot is facing North to begin with, has 16 beepers, and is standing on the corner just below where the bottom of the diamond is to be.

  • Let’s write a class called DiamondPlanter together using stepwise refinement. It’s like Harvester except the field is diamond shaped and the robot is putting down beepers instead of picking them up. The robot should always place 4 beepers on a diagonal. Assume the robot is facing North to begin with, has 16 beepers, and is standing on the corner just below where the bottom of the diamond is to be.

    • What is common to another robot we have already created?
    • What is the main thing that it does? Name this method and it will be the one we call in the client program.
    • Now, break it down and identify new methods
    • While writing it, identify any helper methods you’d like to use. Abstract out any common code into their own unique methods (abstraction).
    • We’ll take each helper method in turn and repeat this stepwise-refinement process until we have cohesion (we finally get to the primitive methods).




So, we wrote DiamondPlanter. More than likely we extended BetterRobot so that we could use its methods (turnRight() and maybe even a turnAround() to help us plant).

  • So, we wrote DiamondPlanter. More than likely we extended BetterRobot so that we could use its methods (turnRight() and maybe even a turnAround() to help us plant).

  • Let’s look at the Inheritance Hierarchy and see what methods we were able to take advantage of.







You want to do something that is not already defined in another class

  • You want to do something that is not already defined in another class

  • And you also might want to use functionality already programmed in other classes. If so, use inheritance. Why reinvent the wheel?

  • Use Abstraction when designing your class – free your mind from the irrelevant and work on the relevant!

    • Ex. If I’m going to write a program to have a robot climb stairs in several buildings, I’m going to use the StairClimber class so I can call climbStair() – I can work on a bigger/better/harder problem and free my mind from the irrelevant details of taking a step and picking beepers


You get stuff for free! You can be lazy (but smart)!

  • You get stuff for free! You can be lazy (but smart)!

  • Use things without knowing/caring how they work!

  • Localize changes to one class/method (encapsulation – data and methods are in the same class)



Remember MileWalker? Suppose a MileWalker can only move one mile. Every time you send it the move() command, you want it to move a mile. How would you do that?

  • Remember MileWalker? Suppose a MileWalker can only move one mile. Every time you send it the move() command, you want it to move a mile. How would you do that?

  • In this case you can redefine what the move() method does.



import kareltherobot.*;

  • import kareltherobot.*;

  • public class MileWalker extends UrRobot

  • {

  • public MileWalker (int st, int av, Direction dir, int beeps)

  • {

  • super(st, av, dir, beeps);

  • }

  • public void move( )

  • {

  • super.move();

  • super.move();

  • super.move();

  • super.move();

  • super.move();

  • super.move();

  • super.move();

  • super.move();

  • }

  • }



public class MysteryBot1 extends UrRobot

  • public class MysteryBot1 extends UrRobot

  • { /* constructor not shown */ public void step1() {

  • move();

  • } public void move() {

  • super.move(); super.move(); }

  • }



public class ABetterBot extends UrRobot

  • public class ABetterBot extends UrRobot

  • { /* constructor not shown */ public void step1() {

  • move();

  • } public void step2() {

  • turnLeft();

  • }

  • }



public class StepperBot extends UrRobot

  • public class StepperBot extends UrRobot

  • { /* constructor not shown */ public void step1() { move();

  • step2();

  • } public void step2() {

  • turnLeft(); }

  • }



“A” is-a Letter

  • “A” is-a Letter

  • Letter is-a Symbol

  • Semicolon is-a PunctuationMark

  • “@’ is-a Symbol

  • PunctuationMark is-a Symbol

  • LetterRobot is-a BetterRobot

  • BetterRobot is-a UrRobot





Design a new LetterBot robot class on paper right now that draws an E:

  • Design a new LetterBot robot class on paper right now that draws an E:

    • Create a drawE() method to draw a single letter E
    • This method should take advantage of any methods we created in BetterRobot (this is code reuse)
    • Create a client program that draws three E’s on the screen
    • Where does LetterBot go in the Inheritance Hierarchy?
  • Why is putting the same code into its one class (encapsulation) better than just leaving it in the client/driver – i.e., what do we gain?



Find the common code and create other methods:

  • Find the common code and create other methods:

  • drawLine() and move3() might be what you choose – you might choose others depending on how you see the problem being decomposed. How did we get access to turnRight() without recoding it?

  • Should the methods within LetterRobot have public or private visibility?

    • First explain public/private by showing methods in the BetterRobot class.
    • The answer to the question depends on whether we believe a client program would be calling methods like drawLine() and move3()
    • My opinion is that these methods (except drawE()) should be private. I’ll argue like this: the name of the class is LetterRobot and its sole purpose is to draw letters. How that object gets it done is of no concern of mine (abstraction), so I don’t need (and shouldn’t be allowed) to see the other helper/auxiliary methods. Therefore, they should be private, helper-like methods for only the LetterRobot class to us.


Yüklə 461 b.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə