GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 08: Methods, encapsulation, and scope


Objectives

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




Why are functions useful in computation?

Writing and using Methods that use Input and/or return Output

In the previous module with Strings, we practiced using methods that resolve (or return) something and that might need input parameters to run.
In this segment we'll practice writing and invoking such methods.

First, two definitions:

Look at this program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18


  public class IOMethods1
  {
    public static void main(String args[])
    {
      int num1 = 5;
      int num2 = 3;
      // Invoke the myOperation method and save result in the num3 variable
      int num3 = myOperation(num1, num2);  // myOperation(num1, num2) resolves into the resulting value
      System.out.println ("myOperation on " + num1 + " and " + num2 +  " is: " + num3);
    }

    public static int myOperation(int localNum1, int localNum2)
    {
      int localNum3 = (localNum1 * localNum2) - (localNum1 - localNum2);
      // this causes the method invocation to resolve into localNum3
      return localNum3;
    }
  }
  

Note the following:

Activity 1: In IOMethods1.java write the above program and see what gets printed.
Now, let us run it inside the Java Visualizer to understand the way these parameters are passed around.

Activity 2: In IOMethods1.java inside the method myOperation, modify the values of localNum1 and localNum2 before returning localNum3 by using these two lines:


localNum1 = localNum1 + 99;
localNum2 = localNum2 + 99;


then, inside the main, add the following statement:


System.out.println ("num1 after modifying localNum1 is: " + num1 );
System.out.println ("num2 after modifying localNum2 is: " + num2 );


What do you print? why?



Activity 3: Write a method that takes no parameters but returns a value. Then invoke that method from main and print the return value.

Activity 4: Run the code below where the name of at least one variable is the same between the two methods. What happens?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18


  public class IOMethods1
  {
    public static void main(String args[])
    {
      int num1 = 5;
      int num2 = 3;
      // Invoke the myOperation method and save result in the num3 variable
      int num3 = myOperation(num1, num2);  // myOperation(num1, num2) resolves into the resulting value
      System.out.println ("myOperation on " + num1 + " and " + num2 +  " is: " + num3);
    }

    public static int myOperation(int num1, int localNum2)
    {
      num1 = num1 + 1;
      int num2 = (num1 * localNum2) - (num1 - localNum2);
      return num2;
    }
  }
  




Variables declared outside of methods

You may have seen variables that were declared outside methods in our homework (for example, turning a test case on or off) -- these were used to control whether or not a problem gets tested. You may have observed that our main method was able to see these variables, even though they were declared outside of the method. Let's formally explore what's going on now with some live examples showing how variables can be shadowed when a method declares an variable with the same name and type as a field that lives outside all of the methods.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18


  public class IOMethods1
  {
     // ignore the static keyword for now (we'll learn more about static next class)
     static int num1 = 11;
     static int num4 = 3;

    public static void main(String args[])
    {
      int num2 = 3;
      // Invoke the myOperation method and save result in the num3 variable
      int num3 = myOperation(num1, num2);  // myOperation(num1, num2) resolves into the resulting value
      System.out.println ("myOperation on " + num1 + " and " + num2 +  " is: " + num3);
      num4++;
    }

    public static int myOperation(int num1, int localNum2)
    {
      num1 = num1 + 1;
      int num2 = (num1 * localNum2) - (num1 - localNum2);
      int num4 = 8;
      return num2;
    }
  }
  



Next class:

We'll go through some tracing of code together to understand methods, return statements, memory, and scope.

Assignments for next lecture:

Work on Quiz5 problems before coming to class.