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.
r.0 Audio:
Consider this example:
Variables have four aspects to them:
About variables:
First, let's review mental execution and develop
some useful visualizations:
So far, so good. Next, let's look at:
There are four levels at which to "read" or understand
a for-loop:
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?
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:
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:
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:
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:
Objectives
R.0 Variables
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';
}
}
⇒ Variable names don't change in a program, they are fixed at compile time.
⇒ At any moment during execution, a variable has a value.
This value can change (often does) during execution.
R.1 Visualization of execution
public class SillyProgram {
public static void main (String[] argv)
{
int x = 1;
int y;
x = x + 1;
y = x*x;
System.out.println (y);
}
}
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);
}
}
⇒ Here, only x and i change.
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
⇒ This fails and goes to the else, which has an if.
⇒ This turns out to be true, and so, one enters the block inside.
⇒ The complex conditino turns out to be true.
⇒ Which turns out to be false.
⇒ The condition (c == 'b') turns out to be true.
⇒ Execution continues after the if-block (just past the
closing brace).
⇒ There's nothing there, so we come out of the else
corresponding to (c == 'a').
⇒ This means we jump past the corresponding else.
⇒ We jump out of the code block corresponding to (x > y).
R.2 Parameterless methods
R.3 Methods with parameters
R.4 The return statement
⇒ It's just that a return at the end is unnecessary.
⇒ This is why the return type is void
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.
R.5 Methods that return a value
int p = 3;
int q = incr( incr(p) + incr(2) );
int r = incr( incr(q) / p );
int p = 3;
int q = factorial (factorial(p)) * factorial(p-3) + factorial (-3);
Call your program
Factorial.java.
R.6 Scope
R.7 Arrays are different
3
[2, 3, 4]
Thus, the variable p is not affected by the call to incr
whereas the array A certainly is.
⇒ This means the invoked method has access to the array, and can modify its contents.
R.8 Why we use methods
For example, compare
On to Assignment 1