15-100 Lecture 5 (Wednesday, September 7, 2005)

Quick Quiz

  // Define the "main method"
  public static void main (String[] args) {
    
    // Declare variables: x, y capable of holding numbers w/fractions
    double x; // float x;
    double y; // float y;


    // Assign 3.14159265 to x and 6.3 to y
    x = 3.14159265; // Or, if float, x = 3.14159265f;
    y = 6.3;        // Or, if float, y = 6.3;


    // Compute the sum of x and y, assigning the result to z
    // BUT, PLEASE TRUNCATE THE REMAINDER AFTER THE COMPUTATION
    int z = (int) (x + y);


    // Print out the value of z
    System.out.println (z);
  }
  

One Note On the Quiz

I had originally intended to ask for the sum of 6.9 and 3.14159265. But, it skipped my mind when I actually went to write it up, so you got off a bit easier than anticipated.

The reason for intended values was to encourage you to observe that the result, was 10, not 9. Since the addition is in the domain of doubles, the result is a double, which is then truncated to an integer via the cast and assignment.

So, just in case this would have thrown you, please take note. Also, please note, if the cast had been applied to each variable, individually, the result would have been 9. The reason is that the fractions would have been truncated before the addition and never had the chance to influence the integer component.

  int z = (int) x + (int) y;
  

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 x = 3.14159265;
      double y = 6.9;
      double z;

      z = sum(x, y);
    }
  }
  

Notice the assignment, "z = sum (x, y);". This is possible because z is declared as a "double", which is compatible with the return type we declared for sum(), also a double. When sum() 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, "sum(x,y)" is replaced with the value 10.04159265.

One more example: addInt()

  public int addInt (int number1, int number2) {
 
    // In this case, we shortened the code by removing the temporary variable.
    return number1 + number2;
  }