Created
October 25, 2014 18:54
-
-
Save rshepherd/af5c7df7a8215f7022a1 to your computer and use it in GitHub Desktop.
Answers to PAC Midterm Pt 1 from 2013
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Question 1 | |
public static boolean isVowell(char letter) { | |
switch (letter) { | |
case 'a': | |
case 'e': | |
case 'i': | |
case 'o': | |
case 'u': | |
return true; | |
case 'y': | |
return Math.random() >= .5; | |
default: | |
return false; | |
} | |
} | |
// Question 2 | |
public static int[] append(int[] a, int[] b) { | |
int[] c = new int[a.length + b.length]; | |
int i; | |
for (i = 0; i < a.length; ++i) { | |
c[i] = a[i]; | |
} | |
for (; i < c.length; ++i) { | |
c[i] = b[i - a.length]; | |
} | |
return c; | |
} |
Hi Karen, in a 'for' loop you can optionally omit any of the components of the loop declaration.
In fact, this is legal..
for(;;) {
// this is an infinite loop
}
Which may seem confusing at first, but is logically identical to..
while(true) {
// also an infinite loop
}
In this case, I am not including the declaration of the counter 'for' the loop. The reason is that I already have a counter 'i' that contains the right starting value for the loop.
I could have done the following instead...
for (int j = i; j < c.length; ++j) {
c[j] = b[j - a.length];
}
make sense?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what does
means?