The goal of this module is to review some key ideas in programming:
For this purpose, we will use some contrived but simplified examples to drive home important points.
Most of our focus will be on methods.
Consider this example:
public class VariableExample { public static void main (String[] argv) { int x = 5; double myFavoriteRealNumber = 3.141; char c; c = 'a'; boolean thisIsFun = false; int[] odd = {1, 3, 5, 7}; x = odd[0] + odd[3]; c = 'b'; } }
Variables have four aspects to them:
About variables:
First, let's review mental execution and develop some useful visualizations:
public class SillyProgram { public static void main (String[] argv) { int x = 1; int y; x = x + 1; y = x*x; System.out.println (y); } }
So far, so good. Next, let's look at:
public class ForLoopExample { public static void main (String[] argv) { int x = 0; for (int i=1; i<=10; i++) { x = x + i; } System.out.println (x); } }
There are four levels at which to "read" or understand a for-loop:
i x 0. Before loop starts 0 1. End of 1st iteration 1 1 2. 2nd 2 3 3. 3rd 3 6 4. 4th 4 10 5. 5th 5 15 6. 6th 6 21 7. 7th 7 28 8. 8th 8 36 9. 9th 9 45 10. last iteration 10 55
Now let's look at a conditional:
r.1 Exercise: Add println's at places numbered 6-9 above and call your program Conditional.java. Make these print the numbers 6, 7, 8, and 9. Then, add println's just after the conditions corresponding to 1, 2, and 3 (to print these numbers). Did the output correspond to the flow of execution?
Consider this simple example:
r.2 Exercise: Without executing the above program, what is the output?
Methods can call other methods:
r.3 Exercise: What is the output of the above program? Where is System.out.print called for the 5th time?
Here's an example of defining and invoking a method with a parameter:
Once the method starts executing there will be a value in n.
or can use the value from an int variable
r.4 Exercise: What is the output of the above program?
First, let's look at methods that don't return a value.
r.5 Exercise: Without executing it, what is the output of the following program?
public class ReturnExercise { public static void main (String[] argv) { printXsAndYs (3, 4); printXsAndYs (4, 1); printXsAndYs (-1, 1); printXsAndYs (4, -1); } static void printXsAndYs (int m, int n) { System.out.print ("start "); if (m < 0) { // 1. return; } else if (m > 10) { // 2. return; } for (int i=0; i<m; i++) { System.out.print ("X"); printYs (n - i); } System.out.println (" end."); } static void printYs (int k) { if (k < 0) { // 3. return; } else if (k > 10) { // 4. return; } for (int i=0; i<k; i++) { System.out.print ("Y"); } } }Write some println's where the numbered comments are to help you see how the program executes.
Here's a simple method that returns an int:
A method call that returns something can be used in expressions:
r.6 Exercise: Insert a println into the incr method to see the order of execution, calling your program Increment.java.
r.7 Exercise: Without execution, evaluate the output of the code below:
int p = 3; int q = incr( incr(p) + incr(2) ); int r = incr( incr(q) / p );
One can have multiple return statements in a method. Only one will actually execute in a particular invocation:
r.8 Exercise: Which return statement gets executed above? Add one println before each return in factorial. Then, try this code in main:
int p = 3; int q = factorial (factorial(p)) * factorial(p-3) + factorial (-3);Call your program Factorial.java.
r.9 Exercise: The program below has three errors. Can you see them without compiling the program? Fix the errors (in Decrement.java).
Consider this program:
Consider this program:
3 [2, 3, 4]Thus, the variable p is not affected by the call to incr whereas the array A certainly is.
Methods are very useful for four different reasons:
with
Example: you have used methods in DrawTool, and Java library methods like Math.random().
How do you know when to create methods vs. writing long code?
r.10 Audio:
On to Assignment 1