printable version
Exam 2
[
1]
[
2]
[
3]
[
4]
[
5]
[
6]
[
7]
[
8]
[
9]
Problem X2.1.
[12 pts]
Complete the program so that it displays a triangle of
asterisks as wide as the user indicates. The below example
illustrates how the program should work when the user
enters 5; if the user entered 2, the program would
display just three asterisks across two lines.
(You can assume the user enters an integer between 1 and 80.)
How big a triangle? 5
*****
****
***
**
*
public class StarTriangle extends Program {
public void run() {
int size = this.readInt("How big a triangle? ");
for(int len = size; len > 0; len--) {
for(int i = 0; i < len; i++) {
this.print("*");
}
this.println();
}
}
}
Problem X2.2.
[8 pts]
What will the below program display when executed?
public class PrintLocations extends GraphicsProgram {
public void run() {
GRect a = new GRect(50, 50, 50, 50);
GRect b = new GRect(60, 60, 60, 60);
this.moveRects(a, b);
this.println(a.getX()); // Note: getX return's x-coordinate
this.println(b.getX()); // of upper left corner
}
public void moveRects(GRect x, GRect y) {
x = new GRect(70, 70, 70, 70);
y.move(80, 80);
}
}
Problem X2.3.
[6 pts]
Suppose we have the three classes Mammal,
Dog, and Beagle, where Dog is a
subclass of Mammal, and Beagle is a subclass
of Dog. We have three variable declarations:
Mammal pet;
Dog spot;
Beagle snoopy;
Which of the following assignment statements are valid?
(More than one are valid.)
| a. | pet = spot; |
|
c. | spot = pet; |
|
e. | snoopy = pet; |
| b. | pet = snoopy; |
|
d. | spot = snoopy; |
|
f. | snoopy = spot; |
a., b., and d. are valid.
Problem X2.4.
[8 pts]
Complete the mousePressed method in the program below
so that the program displays
box if the user has clicked on a rectangle (a GRect),
other if the user has clicked on some other shape,
and none if the user has clicked somewhere with no shape at all.
You should display the word using the println method.
public class Clicker extends GraphicsProgram {
public void run() {
// code omitted to add shapes into window
this.addMouseListeners();
}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
GObject at = this.getElementAt(x, y);
// your code here
}
}
if(at instanceof GRect) {
this.println("box");
} else if(at != null) {
this.println("other");
} else {
this.println("none");
}
Problem X2.5.
[8 pts]
The following grammar is for the language of strings of
a's and b's with more a's than b's.
Draw a syntax tree showing that abbaaaab is included by
this grammar.
S → T a T | T a S
T → a T b T | b T a T | λ
Problem X2.6.
[10 pts]
Write a context-free grammar describing the language containing
all strings of a's and b's for which the number of
a's is even. Examples include aa, abbababa,
and bbbb (since 0 is an even number).
S → T a T a S | λ
T → b T | λ
Problem X2.7.
[8 pts]
Complete the Citizen class below so that
the getId method returns the id parameter provided
when the Citizen is constructed. The below program
illustrates how a program might interact with the
Citizen class.
class CitizenTest extends Program {
public void run() {
Citizen b = new Citizen(2);
Citizen d = new Citizen(4);
this.println(b.getId()); // displays 2
this.println(d.getId()); // displays 4
}
}
class Citizen {
public Citizen(int id) {
}
public int getId() {
}
}
class Citizen {
private int ident;
public Citizen(int id) {
ident = id;
}
public int getId() {
return ident;
}
}
Problem X2.8.
[12 pts]
Write a class that supports the following constructor
and methods.
Racecar(double vel)
(Constructor) Constructs a racecar whose odometer reading is at
0 and which travels at a velocity of vel miles an
hour.
void go(double time)
Updates the odometer to reflect that this racecar has traveled
for time hours at its current velocity.
double getOdometer()
Returns the current odometer reading of this
racecar.
The below program illustrates how a program might interact with this
class.
class RacecarTest extends Program {
public void run() {
Racecar mcq = new Racecar(100);
mcq.go(2); // goes 2 hours at 100 mph, for 200 miles
this.println(mcq.getOdometer()); // displays 200
mcq.go(1.5); // goes 150 more miles
this.println(mcq.getOdemeter()); // displays 350
}
}
class Racecar {
private double velocity;
private double odometer;
public Racecar(double vel) {
velocity = vel;
odometer = 0.0;
}
public void go(double time) {
odometer = odometer + time * velocity;
}
public double getOdometer() {
return odometer;
}
}
Problem X2.9.
[8 pts]
Compute the greatest common divisor of 88 and 68 using Euclid's
algorithm. Show your intermediate work, and be sure to clearly
indicate your final answer.
| 88 | % | 68 | = | 20 |
| 68 | % | 20 | = | 8 |
| 20 | % | 8 | = | 4 |
| 8 | % | 4 | = | 0 |
The GCD is therefore 4.