Skip to content

Instantly share code, notes, and snippets.

@rshepherd
Created October 25, 2014 18:54
Show Gist options
  • Save rshepherd/af5c7df7a8215f7022a1 to your computer and use it in GitHub Desktop.
Save rshepherd/af5c7df7a8215f7022a1 to your computer and use it in GitHub Desktop.
Answers to PAC Midterm Pt 1 from 2013
// 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;
}
@karenpeng
Copy link

what does

for (; i < c.length; ++i) {
        c[i] = b[i - a.length];
    }

means?

@rshepherd
Copy link
Author

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