Week 3 Summary

  • Review of inheritance.
    • When you write a new class by extending an existing class, you inherit all of the public properties and methods of the class you are extending.
  • DefN. and Review of writing your own classes and methods.
    • NOTE: it’s hard to know where to start with this subject because you need to know A, before B and B before C, but it helps to know C before A, etc. So just stick with it…
    • When you write a new class (whether you’re extending an existing class or not) you are writing the definition for a new Object. That means if you write a new class called ‘Foo’ - if you can compile it, then you could instantiate objects of type Foo.
    • When you write a new method in a class it will take the general form:
      public <return-type> <method-name>( <parameter-list> ){
          <method contents>
      }
    • Example:
      public int addTwo(int a, int b){
          return a+b;
      }
    • Example (from Karel J. Robot):
      public void turnRight(){ //empty parameter list
          turnLeft();
          turnLeft();
          turnLeft();
      }
    • Once you write a method in a class, other methods from within the same class can make a call to it. FOR EXAMPLE: in the Karel J. Robot example above, the turnRight() method just calls turnLeft(). Up to this point if we wanted something to turn left we would have to say something.turnLeft().
    • When a class makes calls to other methods within the class, in a way it is a self-referential call. That is to say, the method will be called on whatever instance of the class happens to be running the method.
    • I don’t want to belabor the point since it’s not really that difficult a concept - but it is a concept that if you don’t get it, you will really be in trouble.
  • Demo: StairSweeper (incl. how to read a world file)
  • Demo: Other?
  • Solving more complicated tasks and the technique of Stepwise Refinement (Bergin Ch. 3.8)
  • Solving HarvesterBot
  • Introduction to Polymorphism

Leave a Reply