



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: LAB:Comp Program & Prob Solv; Subject: Computer Science; University: Wellesley College; Term: Fall 2006;
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Sample questions for CS111 Midterm Exam 1.
Problem 1: Buggle World Execution Consider the two Java classes in Fig. 1.
public class DoItWorld extends BuggleWorld {
public void run () { DoItBuggle dewey = new DoItBuggle(); // run statement 1 int n = 5; // run statement 2 dewey.setPosition(new Point(n,n-2)); // run statement 3 * dewey.brushUp(); // run statement 4 dewey.doit(Color.green, n-1); // run statement 5 * dewey.doit(Color.blue, n+1); // run statement 6 * dewey.forward(); // run statement 7 dewey.brushDown(); // run statement 8 dewey.forward(3); // run statement 9 * } }
class DoItBuggle extends Buggle {
public void doit (Color c, int n) { Color oldColor = this.getColor(); this.setColor(c); this.forward(n); this.brushDown(); this.backward(n-2); this.brushUp(); this.backward(2); this.left(); this.setColor(oldColor); }
}
Figure 1: Two Java classes.
Suppose that the run() method is invoked on an instance of DoItWorld which has a 10 × 10 grid of cells. In the four grids on the following page, show the state of the grid directly after the execution of each of the statements in the run() method body marked with a *. In each grid, you should show the following:
Problem 2: Writing Methods Suppose that LetterWorld is a subclass of PictureWorld that supplies you with a method named f with the following contract:
public Picture f (Color c) Returns a picture of the letter “F” in color c, as shown below.
The dotted lines indicate the boundaries of the unit square, and are not part of the picture. The letter is a solid color c and does not have any boundary line drawn in a separate color.
On the next page your task is to write two methods:
The “F” and “E” have the color of the first parameter, while the “A” and “M” have the color of the second parameter.
You may assume that both methods are defined within the LetterWorld class, and so may use the f method in addition to the methods in the PictureWorld contract (e.g., clockwise90, flipDiagonally, above, etc.). You may assume that the fourPics and fourSame methods defined in class and on the problem sets are also available. Your fame method may use your e method, which you may assume works correctly (even if your definition of e is actually incorrect or missing).
Put your definition of the e method here.
Put your definition of the fame method here.
Error # Line # Brief description of error (^) Corrected line