Module 3: Methods: a first look


Objectives

 

By the end of this module, for simple HelloWorld-like programs, you will be able to:

 

3.0 Audio:
 


3.0    An example with method calls

 

Consider the following program:

 

3.1 Exercise: Type, compile and execute the above program.
 

Now let's point out a few things:

  • First, the structure of the overall program: a class with three methods inside:

    • The three are: main, printBigM, and printBigO.

  • Next, observe that the word printBigM appears twice:

    • It's essential to distinguish between the two.
    • The single-line use is a method call or method invocation.
    • The bigger structure with lines of code is called the method declaration (or definition).

  • There is similarly, a declaration for printBigO.

  • But there are two calls for printBigO.

  • A method can have only a single declaration, but can be called any number of times.
           ⇒ In fact, that is the whole point of methods.

  • Jargon alert: in other programming languages, methods are referred to as functions, procedures or subroutines.
 

3.2 Video:

 

3.3 Exercise: Write your own animal sound that uses one method at least thrice with an exclamation mark at the end, e.g., print the big version of BAAA!. Write your code in MyPetSound.java. Once your program is working, say the sound out aloud to celebrate. (Why not?)
 

Next, let's see how methods "work" by drawing an analogy:

  • Think of the class as a building.

  • Think of the method main as the entry point
           ⇒ Lobby or main office.

  • A Java program will always start executing with the first statement in main.

  • Each method corresponds to a room in the building.

  • Here, a method call is a command to go to that particular room.

  • Each room has its own commands (Java statements).
           ⇒ These are executed upon entry to the "room".

  • Important: When you complete the statements in a room, you go back to the place in main just after where the "room" was invoked.
 

Using numbers, let's trace each step in sequence:

 

3.4 Video:

 

3.5 Exercise: Try these questions for the above program:

  • What is the 12-th statement to be executed?
  • What step-number corresponds to the second method call to printBigO?
  • What is the 15-th statement to be executed?
Write all text/drawing answers to module exercises (such as this one) in module3.pdf.
 

3.6 Video:

 


3.1    Calling methods from other methods

 

Consider this program:

  • Yes, we can write our methods that call our own methods.
  • Incidentally, did you notice the escape sequences?
           ⇒ We improved the output.
 

3.7 Exercise: Use this idea to rewrite your own animal sound in a file called MyPetSound2.java. That is, use a single method for the letter that repeats, then call a single method from main that then has the repetitive method calls.
 

Once again, let's trace through some steps:

 

3.8 Exercise: Which step corresponds to the question mark above? Where in the program would we be at the 15 step?
 

3.9 Video:

 

Mental execution:

  • We will use the term mental execution for the above exercise of tracing through the execution without actually compiling and running the program.

  • Note: Mental execution is extremely important in developing programming skill
           ⇒ Please be sure to practice this with every program you read or write.

  • We can't emphasisize this enough. Really.
 


3.2    Reading and Writing

 

First, let's talk about writing:

  • As usual, when writing methods, type in both parens and both braces before typing what's in between.

  • Generally, use the style we have been using:
    • Proper alignment of braces under the p of public.
    • A space between the method name and the parens.

  • You can make up method names, but follow the preferred style:
    • Start with a lowercase letter.
    • Capitalize the start of different parts of the name.

  • Note: class names (like AnimalSounds) should start with a capital letter.
 

An important thing to understand about identifiers:

  • Consider the method name printBigM.

  • This is a name (identifier) we chose.

  • We could just as easily have used myCrazyMethod:

  • In other words, the compiler does not look into the English meaning of identifiers.

  • We choose identifiers to help us read programs.
 

A few more writing issues:

  • It is possible to write a method declaration before or after any invocation. For example, we could have written:

  • There are three conventions typically used:
    • Always declare methods before their first invocation.
    • Try to declare methods near their first invocation (just before or just after).
    • Or, group method declarations according to common theme.
      (For large classes with many methods.)

  • For main, there are also two conventions:
    • main is the first method in the class.
    • main is the last method in the class.

  • What cannot be done: write a method declaration outside a class:

  • Also, we cannot declare a method inside another method, as in:

  • Not that you'd want to, but you can declare a method but not invoke it anywhere.

  • You can declare any number of methods inside a class.
 

3.10 Exercise: What would happen if we changed the order of method invocation? For example:

 

About name clashes:

  • Java allows two methods to have the same, provided they are different in some ways defined by the language.

  • This is a somewhat complex issue at this point, so we will simply give different method names.

  • One can also use the class name as a method name, but this is considered poor stylistic practice.
 


3.3    When things go wrong

 

 

3.11 Exercise: What is the compiler error you get when a method is accidentally declared outside a class?
 

3.12 Exercise: What is the compiler error you get when a method is accidentally declared inside another method?
 

3.13 Exercise: What is the compiler error if we misspell printBigM in the first AnimalSounds program?
 

3.14 Video:

 


3.4    And now for something strange

 

Consider the following program:

 

3.15 Exercise: Mentally execute the above program. Which statement gets executed in the 10-th step of execution? What about the 15-th step?
 

3.16 Exercise: Compile and execute the above program. What do you notice?
 

The term recursion is used when method calls itself:

  • In the above example, it's an obvious error
           ⇒ Nothing useful is accomplished.

  • Later, we'll learn to use recursion to solve problems.

  • In fact, recursion is one of the most powerful computational problem-solving paradigms.
 

Stopping something at the command-line with control-c:

  • When something executes for too long, and you want to stop it, type control-c (press the control key and the c key simultaneously.

  • control-c (sometimes abbreviated in discussion to ctrl-c) is the standard way of stopping something at the command-line.

  • control-c works in all three systems: Windows, Mac, and Unix.
 

3.17 Exercise: Download DrawTool.java and TestDraw.java. Then compile and execute TestDraw, which will bring up a window. Now go to the command-line window and type control-c to see how it ends the program's execution.
 

3.18 Exercise: Execute the recursive program above and stop it during execution using control-c.
 

3.19 Audio:


On to Module 4



© 2017, Rahul Simha