Reading: Chapter 4, sections 4.4-4.5

4.4. New Method Definitions
4.4.1-4.4.5 User-Defined Methods, Parameters, Return Values, Method Invocation
  • Write a method called computeSum() that receives two integers from its calling program and returns the sum of those two integers back to the calling program. Use the computeSequenceSum() method from the text as a model. You should be able to compute 1+1 by calling your method using the invocation computeSum(1, 1) and get the correct answer (which happens to be 2 by the way!).
  • Write a method the receives an x and a y coordinate from its calling program and draws a 50-pixel X 50-pixel square at those coordinates. This method doesn't return anything.
  • Write a method called lfmi() that receives three String arguments (a first, middle and last name) and returns a string representing the name in "last, first middle initial" format. i.e. lmfi("Kinsey", "Henrietta", "Callen") should return the string "Callen, Kinsey H." Note that you can use the charAt() method to get the first character in a string.
4.4.6 Testing
  • If you haven’t already done so, write (on paper) test cases for the computeSum(), drawSquare() and lfmi() methods you wrote above and then use driver programs to run your new methods on your test cases. Consider the boundary cases (e.g., adding 0, negative or really huge numbers; drawing squares at the edge or off the canvas; rendering a name with a one-character middle name). Testing is an art, a destructive art perhaps, for which some people have particular gifts - perhaps you’re one of those people.
  • As a challenge, look at the test cases in Section 4.4.6 in the text and see if you can find a new test case that breaks the current definition of computeSequenceSum(). Be devious; good test engineers do the unexpected; they push the limits; they find errors in code that others considered unbreakable.
4.4.7 Scope
  • Consider the following program. Study it first before running it and try to predict what it will print on the console:

    int a = 1;
    
    void setup() {
      int a = 2;
      println(a);
    }
    
    
    • Try running this code now to see if you were right. How’d you do?
    • Now add the following definition for the draw() method to the end of the given program:
      void draw() {
        println(a);
        stop();
      }
      
      What happens now (and why!)?
    • Now, remove the int i = 2; statement from the setup() method. What happens now?

    By the way, this is a terrible piece of code that we’ve written for demonstration purposes only.

  • Consider the following code segment. Study it first before running it and try to predict what it will print out:

    int a = 1;
    
    void setup() {
      int a = 2;
      println(a);
      myMethod(a);
      println(a);
    }
    
    void myMethod(int a) {
      a = a + 1;
      println(a);
    }
    
    • Try running this code now to see if you were right. How’d you do?
    • How many different variables and parameters does the identifier a refer to in this program? And what is the scope of each?
    • When the program calls myMethod(a), does the execution of the method change the value of the a declared in setup()?

    This also is a terrible piece of code that we’ve written for demonstration purposes only. (-: We forbid you to ever write code that looks like this! :-)