From blocks to methods
|
|
void main() {
final int agePeter = 26, ageCarmen = 30;
final int maxAge = max(agePeter, ageCarmen);
final int heightDenali = 6910, heightCerroTorre = 3128;
final int maxHeight = max(heightDenali, heightCerroTorre);
}
int max (int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
} No. 61
Computing the absolute value
|
Q: |
Implement a method
|
||||
|
A: |
|
No. 62
Absolute value, flawed implementation
|
Q: |
We consider an alternate solution of Computing the absolute value : This yields a compile time “Missing return statement” error. What's wrong here? Provide a solution differing from Computing the absolute value . |
|
A: |
The abs(int value) {...} must return a value for
arbitrary values. This is not the case for
Reason: A Java compiler is simply not “clever”
enough to realize, that indeed all possible values are being
handled here. We require an unconditional This is actually a minor regression in comparison to Computing the absolute value : In case of |
