[8 pts]
If n is an integer variable whose value is 33, what
is the value of each of the following expressions?
| a. | 4 + n * 2 |
| b. | n / 5 + 1 |
| c. | n % 8 |
| a. | 4 + n * 2 | = 70 |
| b. | n / 5 + 1 | = 7 |
| c. | n % 8 | = 1 |
[8 pts]
List all values taken on by the variables x
and y as a computer executes the below Java
fragment.
int x;
int y;
x = 6;
y = 0;
while(x > 1) {
y = y + x;
if(x % 2 == 0) {
x = x / 2;
} else {
x = 3 * x + 1;
}
}
y = y + x;
x: | 6, | 3, | 10, | 5, | 16, | 8, | 4, | 2, | 1 | |
y: | 0, | 6, | 9, | 19, | 24, | 40, | 48, | 52, | 54, | 55 |
[6 pts]
What belongs in the while loop's condition so that the
program fragment continues to read numbers until either of the sums
is above 10?
int a = 0;
int b = 0;
while(/* condition to write */) {
a = a + this.readInt("Add to a? ");
b = b + this.readInt("Add to b? ");
}
a <= 10 && b <= 10
[12 pts]
Complete the program below so that it displays a target of
count concentric circles, as illustrated at right
imagining count to be 8. Each
circle's diameter should be 10 pixels more than the circle
within it; the smallest circle should have a diameter of 20.
import acm.graphics.*;
import acm.program.*;
public class Target extends GraphicsProgram {
public void run() {
int count = this.readInt("Rings in target? ");
int drawn = 0;
while(drawn < count) {
int diam = 20 + 10 * i;
int pos = 100 - diam / 2;
GOval ring = new GOval(pos, pos, diam, diam);
this.add(ring);
drawn = drawn + 1;
}
}
}
[8 pts] Rewrite the below program fragment so that it uses the conventions used in class for indentation and brace placement.
int n = 12;
int i = 2;
while(i * i < n)
{ if(n % i == 0)
{ this.println(i);
} i = i + 1; }
int n = 12;
int i = 2;
while(i * i < n) {
if(n % i == 0) {
this.println(i);
}
i = i + 1;
}
[12 pts]
Suppose we have a class RobotCommunicator whose objects
allow one to communicate with wheeled robots over a wireless
connection. The class provides the following constructor and
methods.
RobotCommunicator(String name)(Constructor) Creates a connection with a robot whose name is
name.
int readSensor()Tells the robot to send its sensor reading, returning the sensor reading received. The sensor reading will be an integer between 0 and 100, indicating how green the robot's environment is.
void start()Tells the robot to start its motors so that the robot goes forward. The method returns immediately, but the motors continue to go forward.
void stop()Tells the robot to stop its motors.
Below, complete the program so that it establishes a connection with a robot named Eve, then tells Eve to go forward, continues reading Eve's sensor until Eve reports a number greater than 80, and then tells Eve to stop.
import acm.program.*;
public class EveController extends Program {
public void run() {
RobotCommunicator connection = new RobotCommunicator("Eve");
connection.forward();
while(connection.readSensor() <= 80) {
/* Do nothing: Eve will move forward during this time. */
}
connection.stop();
}
}
[8 pts]
List all values taken on by the variables i
and j as a computer executes the below Java fragment, and
indicate what the fragment displays on the screen.
String s = "sasses";
int i = 0;
int j = 0;
while(i < s.length()) {
String t = s.substring(i, i + 1);
this.print(t);
if(t.equals("s")) {
i = i + 2;
j = j + 1;
} else {
i = i + 1;
}
}
this.println(j);
i: |
0, | 1, | 3, | 5, | 6 |
j: |
0, | 1, | 2, | 3 |
The program fragment displays sses3
.
[12 pts] Complete the program so that it reads 8 lines and then displays how many of them have 7 characters. Below is an example illustrating how your program should work, with user input in boldface.
? Hendrix ? College ? is ? a ? residential ? liberal ? arts ? institution 3 lines with 7 characters
import acm.program.*;
public class Count7 extends Program {
public void run() {
int linesRead = 0;
int count = 0;
while(linesRead < 8) {
String line = this.readLine("? ");
if(line.length() == 7) {
count = count + 1;
}
}
this.println(count + " lines with 7 characters");
}
}
[6 pts]
Rewrite the below program fragment using a for loop
rather than a while loop
to iterate through the values of i.
int i = 1;
while(i <= 10) {
this.println(i);
i = i + 2;
}
for(int i = 1; i <= 10; i = i + 2) {
this.println(i);
}