Joseph Haugh
University of New Mexico
int max(int a, int b) {
if (a > b) return a;
else return b;
}
int max(int a, int b) {
if (a > b) return a;
else return b;
}
void main() {
IO.println(max(3, 7)); // expect 7
IO.println(max(10, 4)); // expect 10
IO.println(max(5, 5)); // expect 5
}
max(3, 7), max(10, 4)max(5, 5): what if both values are equal?max(-1, -5): what about negatives?int countVowels(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
return count;
}
void main() {
IO.println(countVowels("hello")); // expect 2
IO.println(countVowels("java")); // expect 2
IO.println(countVowels("rhythm")); // expect 0
}
void main() {
IO.println(countVowels("hello")); // expect 2 -- PASS
IO.println(countVowels("java")); // expect 2 -- PASS
IO.println(countVowels("rhythm")); // expect 0 -- PASS
IO.println(countVowels("")); // expect 0 -- PASS
IO.println(countVowels("HELLO")); // expect 2 -- FAIL: prints 0!
}
int countVowels(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = Character.toLowerCase(s.charAt(i));
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
return count;
}
'H' becomes 'h' and is correctly counteddouble average(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
return sum / nums.length;
}
void main() {
int[] a = {2, 4, 6};
IO.println(average(a)); // expect 4.0
int[] b = {10, 20, 30, 40};
IO.println(average(b)); // expect 25.0
}
void main() {
int[] a = {2, 4, 6};
IO.println(average(a)); // expect 4.0 -- PASS
int[] b = {10, 20, 30, 40};
IO.println(average(b)); // expect 25.0 -- PASS
int[] c = {1, 2};
IO.println(average(c)); // expect 1.5 -- FAIL: prints 1.0!
}
1.0 instead of 1.5double average(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
IO.println("sum = " + sum);
IO.println("length = " + nums.length);
IO.println("sum / length = " + (sum / nums.length));
return sum / nums.length;
}
double average(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
IO.println("sum = " + sum); // sum = 3
IO.println("length = " + nums.length); // length = 2
IO.println("sum / length = " + (sum / nums.length)); // sum / length = 1
return sum / nums.length;
}
sum is 3 and length is 2, both correct3 / 2 in Java is integer division: it truncates the decimal, giving 1double, but the division happens with two int values first1 is then converted to 1.0, which is too late to get 1.5double average(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
return (double) sum / nums.length;
}
sum to double before dividing(double) 3 / 2 is 3.0 / 2, which gives 1.5(double)(sum / nums.length) would not fix it; that still divides as ints first, then castsdouble average(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
return (double) sum / nums.length;
}
average(new int[]{}) ?void main() {
int[] a = {2, 4, 6};
IO.println(average(a)); // expect 4.0 -- PASS
int[] b = {10, 20, 30, 40};
IO.println(average(b)); // expect 25.0 -- PASS
int[] c = {1, 2};
IO.println(average(c)); // expect 1.5 -- PASS
int[] d = {};
IO.println(average(d)); // expect ??? -- CRASH: divide by zero!
}
nums.length is 0, so sum / 0 throws ArithmeticExceptiondouble average(int[] nums) {
if (nums.length == 0) {
return 0.0;
}
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
return (double) sum / nums.length;
}
0.0 is a reasonable sentinel; some designs throw an exception insteadLogic
< vs <=)if= instead of ==== instead of equalsArithmetic
intEdge cases that often reveal bugs