






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Spring Semester 2002 Thursday, February 28/Friday, March 1
YOUR NAME: Exam 1 Solutions
This exam has five problems. Each problem has several parts. The number of points for each problem is shown in square brackets next to the problem. There are 100 total points on the exam.
Write all your answers on the exam itself. Whenever possible, show your work so that partial credit can be awarded.
The exam is open book. You may refer to your notes, handouts from this semester (including the Fall’01 CS111 text), problem sets and solutions, and whatever additional materials would be helpful. However, you may not use another person’s notes, or any materials from previous semesters (excluding the Fall’01 CS111 text).
By the Honor Code, you are not allowed to talk to anyone about the details of this exam before or after taking it, until after all members of the class have taken the exam.
Please keep in mind the following tips:
Problem Topic Points Score
1 Java Syntax 15
2 BuggleWorld Execution 20
3 Writing Methods 25
4 Invocation Trees 20
5 Debugging 20
Total 100
This page intentially left blank (except for this self-referential sentence).
Problem 2 [20]: 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 * } }
public 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:
DoItWorld grid after the DoItWorld grid after the execution of run() statement 3 execution of run() statement 5
0 1 2 3 4 5 6 7 8 9 10
0
1
2
3
4
5
6
7
8
9
10
0 1 2 3 4 5 6 7 8 9 10
0
1
2
3
4
5
6
7
8
9
10
DoItWorld grid after the DoItWorld grid after the execution of run() statement 6 execution of run() statement 9
0 1 2 3 4 5 6 7 8 9 10
0
1
2
3
4
5
6
7
8
9
10
0 1 2 3 4 5 6 7 8 9 10
0
1
2
3
4
5
6
7
8
9
10
Put your definition of the e method here.
Here is one solution, which does not name any intermediate pictures:
Here is another solution, which names some intermediate pictures:
A surprising number of students tried to draw the “E” shape with buggles. As we have seen on problem sets, buggles can indeed draw letters in BuggleWorld, but there is no way for a buggle to somehow turn what it has drawn into a Picture instance within PictureWorld.
Put your definition of the fame method here.
Here is one solution, which does not name any intermediate pictures:
Here is another solution, which names some intermediate pictures:
Problem 4 [20]: Invocation Trees
public class ExamPictureWorld extends PictureWorld {
public Picture meth1 (Picture a) { Picture b = beside(a, empty()); return overlay(meth2(b), b); }
public Picture meth2 (Picture c) { return clockwise90(above(c, empty(), 0.75)); } }
Figure 2: A sublcass of PictureWorld.
Consider the subclass of PictureWorld shown in Fig. 2. Supose that: EPW is an instance
of ExamPictureWorld, P0 is a Picture instance denoting the empty picture, P1 is a Picture instance denoting the rightmost picture below:
ExamPictureWorld EPW
Picture P0 Picture P
red blue
The dashed grid lines are not part of the pictures. They indicate coordinates within pictures. The colors names are not part of picture P1. They indicate the color of the two rectangles. Each of the two rectangles is a solid color without any separately colored border. On the next page, you are to draw an invocation tree that models the instance method invocation EPW .meth1( P1 ). In the area labeled Execution Land, you should draw an invocation tree that contains the following eight nodes, arranged appropriately into a tree. You should use each node exactly once.
above( , , ): clockwise90( ): empty(): meth2( ):
beside( , ): empty(): meth1( ): overlay( , ):
The empty circles in the nodes are skeletons for object references that you should fill in with one of the labels P0, P1, P2, P3, P4, or P5 to refer to the appropriate Picture instance in Object Land (see below). A circle enclosed by parentheses is a reference to an actual argument of the method invocation. A circle appearing after a colon is a reference to the result of the method invocation. The root of the invocation tree is the meth1() node, which has already been drawn for you, and whose actual argument has been filled in (you need to fill in its result). In the area labeled Object Land are the skeletons for the six Picture instances that are used during the execution. The pictures labeled P0 and P1 have already been drawn for you; you
should draw the pictures for P2 , P3 , P4 , and P5. In each picture, you should label red areas with the letter R and blue areas with the letter B. All other areas are presumed to be white. (Note: for simplicity, the receiver object EPW for each of the method invocations has been omitted. This instance has also been omitted from Object Land.)
Figure 3: In the table on the next page, for each of 10 errors in different lines of the above program give:
You may list the errors in any order. You do not have to list them in the order in which they occur
Error # Line # Brief description of error (^) Corrected line
1 4 Color.cyan()^ is not a method invocation
Color.cyan
The local variable declaration int n = 4 is missing a semi-colon at the end
int n = 4;
There is a missing new in the constructor method invocation that creates an ExamBuggle
ExamBuggle emma = new ExamBuggle()
The two arguments of the instance method invocation emma.mystery1(3,Color.red) are in the wrong order
emma.mystery1(Color.red,3)
In this.mystery3(), this stands for an instance of ExamBuggleWorld, which does not understand the mystery3 message; the recipient should be an instance of ExamBuggle
emma.mystery3;
The local variable declaration n2 = n1 + 1; is missing a type for the contents of the variable
int n2 = n1 + 1;
Color.c attempts to reference a non-existent class constant rather than the parameter c
this.setColor(c);
The instance method declaration for mystery1 is missing a close squiggly brace.
The non-void method mystery2 is missing a return statement.
return this.isOverBagel();
The method header for mystery3 is missing the return type, void
public void mystery3()
Notes: