|
Spring Semester 2002
(Getting Back In Shape After Spring Break)
|
"Now, Andy did you hear about this one? Tell me, [...] are we losing touch?"
Consider this:
|
Will name be changed?
|
String name = "Alice";
name = name.toUpperCase();
System.out.println("Hello " + name +"!");
Very good question.
|
And I know the answer.
|
You look like you know the next question too.
|
Yes: what if the part in blue is taken away?
|
Things change then.
|
And name remains unchanged.
|
Now, consider this:
| Oh, boy. |
String vehicle = "snowmobile";
What's vehicle ?
|
A String variable.
|
What's "vehicle" ?
|
A String constant.
|
What's vehicle.substring(0, 4) ?
|
A (new) String gets created: "snow" .
|
What's vehicle.length() ?
|
A number.
|
What's vehicle + vehicle.length() ?
|
"snowmobile10"
|
Consider this:
(x <= y)
|
I could write it this way:
(y >= x)
|
How about this:
(x == y) || (x < y)
|
Same thing (only more verbose).
|
How about this, then:
(x - y) <= 0
|
Same thing, only more algebraic.
|
And this?
(x <= y) && (x == x)
|
That's
(x <= y) && true
|
How about this, then?
(x < y) && (x == y)
| That's just false . |
True. I think I heard you laughing...
|
I think you heard me try. Consider this:
(x > 3) || (x < 5)
|
Then this is always true .
|
I think I've said too much.
|
Consider this:
| Oh, no, curly braces, again! |
if (x > 3) {
if (x < 5) y = 1;
else if (x != 6) y = 2;
else y = 3;
} else y = 4;
Assume that x and y are integers.
|
Let's just imagine that.
|
And y is 4 at the end.
|
Then x must have been 3 or less.
|
Assume y ends up being 1.
|
Then x must have been exactly 4 .
|
What if y ends up being 3 .
|
Then x had a value of 6 .
|
For what x does y end up with a value of 2 ?
|
I can see that all branches modify y .
|
Indeed they do.
|
Then x was 7 or bigger for sure.
|
You're very good.
|
I think I heard you saying that.
|
Well, I haven't said enough.
|
Consider this:
(x > 3) && (x < 5)
|
Does it matter?
|
This time it does.
|
Then this is the same as saying
x == 4
|
For any value of x the two expressions
end up either both true or both false .
|
|
Which makes them equivalent.
|
Consider this:
'5' - '0'
|
That's just 53 - 48 .
|
char acters have codes.
|
In a number context their codes take over.
|
So this evaluates to (the number)
|
5
|
What's this:
(4 * 5 + 3) / 4
|
23/4
|
The result is an int .
|
I'm losing my composure.
|
The result is 5 .
|
What's this:
("01" + "0" + "12").length()
|
"01012".length()
|
Which is also, ... 5 .
|
And 8 - 1 - 2 , is it also 5 ?
|
No question about that.
|
What is '5' , then?
|
That is a char , and its code is 53 .
|
If you print it it looks like a 5 .
|
But does it act as 5 ?
|
Does it act like it?
|
Add 3 to it.
|
5 + 3 is 8 .
|
'5' + 3 is the code for '8' .
|
Well, then consider this:
|
Prints as a + b = 01
|
class One {
public static void main(String[] args) {
int a = 0;
int b = 1;
System.out.println("a + b = " + a + b);
}
}
The blueprint is empty.
|
There's only one static member, main .
|
It has two local variables inside it.
|
But they're not part of the blueprint.
|
Not part of the factory either.
|
They're just that: local to main .
|
Consider this:
|
Looks like a loop, when in fact it's not.
|
int x = 10;
while (x == 0) {
x = x + 1;
System.out.println(x);
x = x % x;
}
Then, consider this:
| This one never ends. |
int x = 10;
do {
x = x + 1;
System.out.println(x);
x = x % x;
} while (x == 0);
What does it print?
|
An infinite number of 1 's.
|
I don't understand x % x
|
I think I've heard you try.
|
If x is not 0 (zero) then I can divide
x by itself and there is no remainder.
|
I think that's very good.
|
Consider this:
| It won't compile. |
class One {
public static void main(String[] args) {
greet("Andy");
}
void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}
Why not?
|
greet is not a static method.
|
Consider this:
|
You're calling one constructor from the other.
|
class BankAccount {
double balance;
BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public BankAccount() {
this(0);
}
}
Yes, pp. 117-118 in the text.
|
|
java java kona java kona java
What would you want it to be?
|
A command-line invocation.
|
Line at the prompt?
|
Yes, what are those words, how can you name them to me?
|
If you could call your main class java
|
... then we'd have four arguments.
|
The first one is kona
|
... which also appears as args[2] .
|
But can you call a class java .
|
I think I'll let you try.
|
If you believe there's nothing up my sleeve
| ... then nothing is cool. |
Consider this:
| Prints 0 1 |
public class A {
public static void main(String[] args) {
int x = 1;
System.out.print(fun(x) + " ");
System.out.print(x + " ");
}
public static int fun(int x) {
x = x - 1;
return x;
}
}
public class A {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
System.out.print(fun(x) + " ");
System.out.print(x[2] + " ");
}
public static int fun(int[] x) {
x[2] = x[2] - 1;
return x[2];
}
}
This prints the same value twice.
|
Precisely.
|
Trying to keep up with you.
|
And I don't know if I can do it better.
|
public class A {
public static void main(String[] args) {
System.out.print(fun(5) + " ");
}
public static int fun(int x) {
System.out.print(x + " ");
return x + 1;
}
}
Tricky.
|
Yes, 5 is printed first.
|
So, consider this:
|
What's mix("aoteon", "mnnhmo") ?
|
public static String mix(String s, String t) {
String ans = "";
int slen = s.length();
int tlen = t.length();
for (int i = 0; i < slen && i < tlen; i++) {
ans = ans + t.charAt(i) + s.charAt(i);
}
return ans;
}
I'm losing my forbearance.
|
I think I said it first.
|
But listen have you heard about this one:
|
|
public class A {
public static void main(String[] args) {
int yo = 12;
System.out.println(yo(yo));
}
public static boolean yo (int yo) {
if (yo == 12) return true;
return false;
}
}
Functions and variables have different namespaces.
|
So the code above compiles.
|
That's all you need to know.
|
true
|
public class A {
public static void main(String[] args) {
System.out.println(fun(1, fun(fun(2, fun(3, 4)), 5)));
}
public static int fun(int a, int b) {
return a + b;
}
}
|
What if all these fantasies come flailing around?
|
I think you know the answer.
|
(1 + ((2 + (3 + 4)) + 5))
|
Exactly.
|
I think I've seen enough.
|
No.
|
Well, then, consider this:
a[a[a[a[0]]]]
|
I think I need an a . |
int[] a = { 1, 2, 3, 4, 5};
|
Then, it's a[a[a[1]]]
|
... which is a[a[2]]
|
... that is, a[3]
|
... and finally 4
|
I think I've seen the light.
|
a[1 + a[1 + a[0]]]
|
String[] name = { "One", "Two", "Three", "Four", "Five"};
boolean[] mark = { false, false, false, false, false};
|
... and the following definition:
|
public static String fun(String[] a, boolean[] b) {
for (int i = 0; i < b.length; i++)
if (b[i]) return a[i];
return "One";
}
Trying to keep up with you.
|
I haven't said enough.
|
I know.
|
What's fun(name, mark) ?
|
boolean[] mark = { false, true, false, false, false};
I think I know this, "Two" .
|
I think you're right.
|
Consider this:
| Now a is { 10, 9, 8, 7, 6} . |
int[] a = new int[5];
a[0] = 10;
for (int i = 1; i < a.length; i++)
a[i] = a[i-1]-1;
int value = a[0];
| A value of 10. |
Which gets smaller and smaller.
|
Until it gets to 6 .
|
for (int i = 0; i < a.length; i++)
if (value > a[i])
value = a[i];
System.out.println(value);
Oh, no, you've said too much.
|
I set it up.
|
So maybe we should do a final one.
|
Are we losing touch?
|
I thought that I heard you laughing.
|
I thought that I heard you sing.
|
int[][] a = { { 1, 2, 3, 4},
{ 2, 3, 4},
{ 3, 4},
{ 4}};
I thought that you heard me try.
|
This will print 4321 .
|
int sum = 0;
for (int i = 0; i < a.length; i++)
System.out.print(a[i].length);
What if you change this last one to:
|
This prints 30 .
|
int sum = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
sum += a[i][j];
System.out.println(sum);
And if you change it to:
|
It prints the number of even numbers in a
|
int sum = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
if (a[i][j] % 2 == 0)
sum += 1;
System.out.println(sum);
Well, then, that's me in the corner. |
No, it's just a man on the moon.
|
Last updated: Mar 7, 2002 by Adrian German for A201