Prelim 2 Solution Guide
Median = 79
1.(a) 10 points
20 10 XX
-3 if concatenation wrong -7 for things like
20 10 0 XIXI
(b) 10 points
String c1 = s.substring(0,1); String c2 = s.substring(1,2); B = c1.equals(c2);
5points for the substrings and 5 for the boolean assignment. -3 for equals(c1,c2). -1 if indices off by 1. -2 for using uninitialized variable.
2. Output:
y = 11 3 points x = 11 y = 10 z = 30 3 + 3 + 1 points y = 2 2 points z = 4 2 points x = 1 y = 10 z = 4 2 + 2 + 2 points
3(a) 10 points
boolean twoOrThree = (n%2==0) || (n%3==0); boolean fiveOrSeven = (n%5==0) || (n%7==0); return twoOrthree && fiveOrSeven;
-5 if ands and ors mixed up, - 3 for syntax like "return nearlyPrime", -2 for == instead of ==, -3 if no return.
3(b) 10 points
int sum = 0; for(int k=1;k<=1000000;k++) { if(A.nearlyPrime(k)) { sum=sum+k; } } System.out.println(sum);
-2 for no A., -2 if sum not initialized, -3 if print inside loop, -6 if no if, -3 if wrong sum update, -6 if the nearlyPrime method is not used.
4(a) 8 points
if (m>D.m) return true; else if (D.m > m) return false; else if (d > D.d) return true; else return false;
Or this: return (m>D.m) || ((m==D.m) && (d>D.d));
-5 for (m>D.m) || (d>D.d). -3 if dont return a boolean, -2 if you solve the "earlier than" problem, -3 if you use mVal not m, or dVal instead of D.
4(b) 12 points
SimpleDate June15 = new SimpleDate(6,15); // Represent June 15th 4 points for this. -1 if no new SimpleDate D0 = SimpleDate.random(); // Represent the random date 4 points, -1 if include a new if ( D0.isLaterThan(June15)) // 4 points for using isLaterThan correctly system.out.println("Yes"); else System.out.println("No");
-5 if youdo not use isLaterThan, -7 if you don't use parameters in the method call.
5.
g.setColor(Color.black); g.fillOval(hc-r,vc-r,2*r,2*r); g.setColor(Color.yellow); int d; for(int v=vc-r;v<=vc+r;v++) { d = (int) math.sqrt(r*r - (v-vc)*(v-vc)); if (q==1) g.drawLine(hc,v,hc+d,v); else if (q==2) g.drawLine(hc-d,hc+d,v); else if (q==3) g.drawLine(hc-d,v,hc,v); }
-6 if the default does not draw a new moon, -5 if the d computation is wrong, -2 if drawLine is handed a type double value, -5 if missing hc in drawLine, -10 if some how try to draw the 1st and 3rd quarter with fillOval, -3 if drawOval instead of fillOval, -12 if just a skeletal solution