Suppose the computer executes the below program fragment, with the user typing 50 when prompted. Sketch what path will be left in the window when the program reaches its end.
double x; x = this.readDouble(); Turtle t; t = new Turtle(10, 100); t.forward(x); t.left(90); t.forward(20); t.right(90); t.forward(x); t.hide();
Suppose we have an int variable named k
with a value of 31. What
is the value of each of the following expressions?
| a. | 64 - k * 2 |
| b. | k % 8 |
| c. | 8 * (k / 8) |
| a. | 64 - k * 2 | = 2 |
| b. | k % 8 | = 7 |
| c. | 8 * (k / 8) | = 24 |
Suppose we have two variables — an int variable
named k with the value of 18, and a double
variable named x with the value of 3.5. What is the value
of each of the following Java expressions?
| a. | k + x |
| b. | 22/7 * k |
| c. | k % 7 |
| d. | 1 + x * 2 |
| a. | k + x | = 21.5 |
| b. | 22/7 * k | = 54 |
| c. | k % 7 | = 4 |
| d. | 1 + x * 2 | = 8 |
List the values of a, b, and c
following the execution of the below fragment.
int a = 10; int b = 20; int c = 40; a = b; a = c;
a: | 40 |
b: | 20 |
c: | 40 |
List the values of a, b, and c
following the execution of the below fragment.
int a = 10; int b = 20; int c = 40; a = b; b = c; c = a;
a: | 20 |
b: | 40 |
c: | 20 |
Give two examples seen in class of each of the following.
a. primitive types
b. classes
a. int, double, boolean
b. Turtle, Color, GOval,
GCompound, Random
Name one way in which programs work differently with
primitive types (such as int, double,
and boolean) than with classes (basically all
other types). Give an example of one type in each category.
There are two distingishing characteristics.
Primitive values are created by naming them directly in a
Java program, whereas objects (class data) are created
using the new keyword.
(Later, we'll see that there's a sole exception to this rule, theint i = 5; // int is a primitive type Rectangle box = new Rectangle(10, 10, 50, 50); // Rectangle is a class
String class.)Programs interact with primitive values via operators
(such as + or <), whereas programs
interact with objects primarily via instance methods.
i = i + 3; // i is an int, which is a primitive type yertle.forward(100); // yertle is a Turtle, which is a class
(Again, we'll later see the String class, which is a
slight exception, since the '+' operator can be applied
to it.)
In the below program fragment, draw a circle around each variable initialization, and draw a rectangle around each variable declaration.
int a;
int b;
a = 95;
b = 40;
while(b != 0) {
int r;
r = a % b;
a = b;
b = r;
}
int a;,
int b;, and
int r;.
The variable initializations are
a = 95;,
b = 40;, and
r = a % b;.
List the values taken on by the variables a and
b as the computer
executes the below program fragment.
int a;
int b;
a = 95;
b = 40;
while(b != 0) {
int r;
r = a % b;
a = b;
b = r;
}
a: |
95, | 40, | 15, | 10, | 5 |
b: |
40, | 15, | 10, | 5, | 0 |
[Incidentally, this fragment computes the greatest common divisor of a and b.]
Sketch the final appearance of the window when the computer completes the program below.
Turtle t;
t = new Turtle(20, 75);
int drawn;
drawn = 0;
while(drawn < 5) {
t.forward(65);
drawn = drawn + 1;
t.left(72);
t.forward(65);
t.right(144);
}
t.hide();
Suppose we wish to repeat the below while loop until
the square of x is between 99 and 101. What
condition belongs in the parentheses?
while( ) {
x = (x + 1.0 / x) / 2.0;
}
x * x < 99 || x * x > 101
The below program fragment creates a circle that moves right continually. Modify it so that the circle moves right for 100 frames and then stops.
GOval circle = new GOval(100, 100, 20, 20):
this.add(circle);
while(true) {
this.pause(40);
circle.move(1, 0);
}
GOval circle = new GOval(100, 100, 20, 20):
this.add(circle);
int frames = 0;
while(true frames < 100) {
frames++;
this.pause(40);
circle.move(1, 0);
}
Using the GOval class, complete the program fragment
below so that
it draws n circles of diameter 10 stacked on top of
each other. For example, if the user enters the number 8, the
program would display the following.

(The circles' coordinates are not important, as long as the circles are stacked atop one another.)
int n; n = this.readInt();
int drawn;
drawn = 0;
while(drawn != n) {
GOval circle;
circle = new GOval(50, 10 + 10 * drawn, 10, 10);
this.add(circle);
drawn = drawn + 1;
}
Below is a program that draws a circle which slowly falls down the window. Indicate the program so that the circle accelerates as it falls. That is, it should fall by 1 pixel for the first frame, 2 pixels for the second, 3 pixels for the third, and so on. (Don't worry about what happens when it reaches the window's bottom: It should keep falling forever.)
public class DropBall extends GraphicsProgram {
public void run() {
GOval ball = new GOval(250, 0, 50, 50);
this.add(ball);
while(true) {
this.pause(20);
ball.move(0, 1);
}
}
}
public class DropBall extends GraphicsProgram {
public void run() {
GOval ball = new GOval(250, 0, 50, 50);
this.add(ball);
int dy = 1;
while(true) {
this.pause(20);
ball.move(0, 1 dy);
dy++;
}
}
}