15-100 Lecture 10 (Wednesday, September 20, 2006)

Today's Quiz

Today's quiz was a quick quiz on continue and break.

Methods
In passing, I've discussed the existence of methods. A method in Java is a construct that describes an action. It is somewhat like a receipe: Do this, then this, then this, &c.

We mentioned that, in fact, main() is just a special method, whose instructions Java follows when it first loads a program into memory. It basically gets things started.

We have, ourselves, made use of methods. Consider, "System.out.println ("Hello world");", which is a method invocation. It asks Java to print the line of text, "Hello world", hence the name "println".

Actually, notice the "System.out". The "." is called the "scope" operator. More specifically than asking "Java" to print the line, we are asking "System.out" to do it. System.out is an Object, a "think" that exists within the program. In this case a "thing" that knows how to print a line. More specifically, we are asking the "out[put subsystem]" of the "System" to print the line. The "System" also has an input subsystem, known as "System.in".

Some methods we'll use and write will be methods like println, associated with particular types of objects. Others, like main(), will truly be floating, available for use by any object. These floating methods are known as "static methods" or "class methods" and have the "static" qualifier, as did the declaration of main.

Writing our Own Methods

What if we'd like to create a method to encapulate addition. This way, we can write, "z = add (6.3, 3.14159265);" instead of "z = 6.3 + 3.14159265;"? This type of method is a "static" method. It doesn't require any state from an object. It takes everything it needs, the numbers to add, as input and returns its only output, the sum.

The general form for a static method is as follows:

  public static return-type method-name (argument-list) {
    method-body...

  }
  

For now, our methods will all be "public". We'll talk more about what this means later. But, for now it means that they are "1st class" methods, not "helper methods" designed to further decompose other methods.

In this case, our method will add double-precision numbers, so we'll call it "addDouble":


  public static return-type addDouble(argument-list) {
    method-body...
  }
  

It will add two double-precision numbers, we'll call them "number1" and "number2", so we'll list these within the "formal argument list", within the ()-parenthesis:

  public static return-type addDouble(double number1, double number2) {
    method-body...

  }
  

And, once it is done, it returns a double-precision number:

  public static double addDouble(double number1, double number2) {
    method-body...

  }
  

Ultimately, the method will add the two numbers and return the result.

  public static double addDouble(double number1, double number2) {
  

    double sum;

    sum = number1 + number2; 
   
    return sum; // This "sends back" the value of sum
  
  }
  

In the example above, please notice that the type of what is being returned (result is a double) matches the return-type we declared, also a double. This is absolutely necessary. What we do must match the specification we provide.

Using our addDouble() method

Okay, let's take a look at a complete example that makes use of the method we just wrote:

  public class MethodExample {
  
    public static double addDouble (double number1, double number2) {
      double sum;

      sum = number1 + number2;

      return sum;
    }

    
    public static void main (String[] args) {
      double number1 = 3.14159265;
      double number2 = 6.9;
      double z;

      z = addDouble(number1, number2);
    }
  }
  

Notice the assignment, "z = addDouble (x, y);". This is possible because z is declared as a "double", which is compatible with the return type we declared for addDouble(), also a double. When addDouble() is called, the directions we provided within the method are followed, and the return value is used in place of the method call. In this case, "addDouble(x,y)" is replaced with the value 10.04159265.

One more example:

import java.io.*;
import java.util.*;

public class MathStuff {

  public static double paintNeededSqFt (double l, double w, double h) {
  
    return (2*w*h + 2*l*h);
  
  }
  


  public static void main (String[] args) {

    Scanner s = new Scanner (System.in);
    
    System.out.print ("Enter the dimensions of the room: ");
    double length = s.nextDouble();
    double width = s.nextDouble();
    double height = s.nextDouble();
    
    Double sqFt = paintNeededSqFt(length, width, height);
            
    System.out.println ("For a room of that size, you'll need " + sqFt + 
                        "square feet of paint.");
      
  
  }
}

  

Calls the method paintNeededSqFt(double l, double w, double h), making sure we pass in the correct arguments and that they match up to the arguments specified (i.e. length first, width second, height third), also making sure we are passing in only doubles

Scope

Scope is something we talked about briefly earlier involving loops. Now we can extend this concept to methods. When we create a variable in one method, that variables exists only inside that method. Take a look at the addDouble method above. Notice how we have a number1 in both methods. These are two different variables, and don't necessarily have any connection between them.