Creating object methods

 

Creating a method, in programming terminology, is to define it.  A method is always defined within a class.  But which class?  If we’re writing a method to have a Board object fill a circle within its boundaries, the method should naturally be defined in the Board class.  A method definition has two parts: 1) the method header (its signature), and 2) the method body.  In the method signature, we specify things such as the method’s name and its parameters, while the body of the method contains the Java statements executed when the method is invoked.  Since a method defines a code block, the Java statements are enclosed in a pair of curly braces.  A method’s signature in Java has to follow a particular format.  For an object method (a non-static method), the format is:

 

      public returnType methodName(parameterList)

 

The keyword ‘public’, when associated with a method, means that any other class may invoke the method.  For now, we will limit ourselves to defining methods as public.  The next item that appears in the signature is the type of the return value of the method.  As we’ve seen before, if the method does not return any values, the keyword void is used as return type.  Next comes the method name, which of course should be reflective of the task that the method executes.  After all, we wouldn’t want to name a method that draws an circle drawTriangle.  Finally comes the parameter list, containing the parameter declarations, each separated by a comma.  In this respect, a parameter declaration is no different from a variable declaration.  A method is also allowed to have no parameters, in which case the parameter list is blank (nothing between the parentheses).  We’ll start with an example of a method with no parameters.  We’d like to have a Board object fill a red circle with radius of 5 in its center.  We’ll call this method fillCircle.

 

Since the method fillCircle pertains to Board, its header and implementation need to be inserted inside the implementation of the Board class.  This can be found towards the end of the BaseBoard.java file.   Look for:

 

// file: BaseBoard.java starting at line 520 or so

 

...

class Board extends BaseBoard

{

   // this is a special method (the constructor)

   public Board(int c, int r)

   {

      super(c, r);

   }

...

 

    // the code for new Board methods should be inserted here

   

Let’s look at how we’re going to implement the fillCircle method.  In the previous unit, we have developed the code that fills a circle by applying its formula as an inequality and checking if a cell satisfies the inequality or not.  The method does not require any parameters, and its return value is void since we don’t expect it to return a result (It’s just coloring cells within the circle).  The nested for loops that examine all the cells require the dimensions of the board.   fillCircle invokes the getRows and getCols methods to obtain the dimensions.  Here’s what the method would look like:

 

 

// this code would go in the Board class definition in BaseBoard.java

     public void fillCircle()

  {

      // obtain the board object’s dimensions

      int rows = this.getRows();

      int cols = this.getCols();

 

      for (int y = 0; y < rows; y++)

      {

         for (int x = 0; x < cols; x++)

         {

            if ((y – rows/2)*(y - rows/2)+(x – cols/2)*(x – cols/2) <= 25)

               this.fillCell(x, y, Color.red);

         }

      }

   }

 

After inserting the method into the Board class, you can compile it to make sure there are no errors, but we’re not yet ready to test this method.  To do that, we would need to create a new class with a main method that invokes fillCircle.  The code would be:

 

      // file CircleTest.java

            public class CircleTest

      {

            public static void main(String[] args)

            {

                  Board b = new Board(50,50);

                  b.fillCircle();

            }

      }

 

A board object should appear with a red circle drawn in its center. 

 

One important detail still requires an explanation.  In fillCircle, you will notice that the keyword ‘this’ (boldfaced in the code) precedes each method invocation instead of an object reference.   To understand why this is the case, suppose you have two Board objects, b1 and b2, and fillCircle is invoked on both of them as:

 

      b1.fillCircle();

      b2.fillCircle();

 

This means that for both objects the method invocations will execute the same statements in the fillCircle method.  The trouble is that, inside fillCircle, there’s no way to tell on which object is the method performed (is it b1 or b2?).  Also, as variables, b1 and b2 are only recognized inside main since that’s where they are declared.  In effect, ‘this’ takes on the role of whatever object for which the method executes.