Introduction to Software Development
GWU Computer Science
By the end of this module, for simple HelloWorld-like programs, you will be able to:
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:
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:
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; } } |
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; } } |