Grading Guide for Assignment 4. Due 8 October 1998
1. (a) i= 1; s= 1; (b) i= 0; s= 0; ID _______________________ (c) i= 1; s= 1; (d) i= -1; s= 0; Sect. Instr. _______________ (e) i= x.length(); s= 0; Sect. Time _______________ 2. (a) i= i+1; s= s+i; (b) i= i+1; s= s+i; Sect. room _______________ (c) i= i+1; s= i*i; (d) i= i+1; if (x.charAt(i) == '_') s= s+1; Q1 (max: 5) _____________ (e) i= i-1; if (x.charAt(i) == '_') s= s+1; Q2 (max: 5) _____________ 3. (a) i == 100 (b) i == 200 Q3 (max: 5) _____________ (c) i == 25 (d) i == x.length()-1 Q4 (max: 4) _____________ (e) i == 0 Q5 (max: 4) _____________ 4. i= 0; x= 5; // Invariant: i rectangles have been drawn, Q6 (max: 4) _____________ // and next one to draw goes at pos. (x,x). while (i< 10) { Q7 (max: 0) ungraded g.drawRect(x,x,50,50); i= i+1; x= x+3; } Q8 (max: 4) _____________ 5. int r= 5; Q9 (max: 4) _____________ // Invariant: Next circle to draw has radius r, // and those with smaller radii have been drawn Total (35) _____________ while (r <= 40) { g.drawOval(75-r,100-r,2*r, 2*r); r= r+5;} 6. i= 0; Notes: // Invariant: First i circles have been drawn, On question 1,2, and 3: // and next one to draw has center (20,5+10*i). 1 point for each part. while (i != 10) { g.drawOval(5+10*i-5,20-5,2*5,2*5); On questions 4, 5, 6, 8, and 9: i= i+1; } 1 point for the initialization truthifying the invariant. 7. k= 0; x= 100; y= 100; 1 point for the condition. // Invariant: k lines have been drawn. Next 1 point for the body making // line to draw begins at position (x,y) progress toward termination. while (k != 54) { 1 point for the body maintaining k= k+1; int x1= x; int y1= y; the invariant. if (k % 4 == 1) x1= x1+2*k; if (k % 4 == 2) y1= y1+2*k; if (k % 4 == 3) x1= x1-2*k; if (k % 4 == 0) y1= y1-2*k; g.drawLine(x,y,x1,y1); x= x1; y= y1; 9. char cu= 'A'; char cl= 'a'; 8. char c= 'A'; // Inv: cl contains a lower-case char. or // invariant: All the upper-case chars // '{' and cu contains the corresponding // that precede c have been printed, // upper-case char. The part of output // and c is the next one to print. // that precedes cl has been printed. while (c<='Z') { while (cl<='z') { System.out.print(c); System.out.print(cl); cl++; c++; } System.out.print(cu); cu++; }