Tabulate the values taken on by each variable as the program fragment below executes.
int n;
int k;
int i;
n = 32;
k = n + 1;
i = 2;
while(i * i <= n) {
if(n % i == 0) {
k = k + i + (n / i);
}
i = i + 1;
}
n |
32 | ||||
k |
33, | 51, | 63 | ||
i |
2, | 3, | 4, | 5, | 6 |
Suppose we have a vertical line segment from
(lx,ly0) to
(lx,ly1) (where
ly0 < ly1).
What should go into an if statement's condition
to test whether a point
(px,py) lies on that line?
if(
) {
print("point on line");
}
px == lx && py >= ly0 && py <= ly1
Complete the below program fragment so that the ball changes its
color between
red and green every second. Your program should use an if
statement (and a while loop) to accomplish this.
public class AlternateColor extends GraphicsProgram {
public void run() {
GOval ball = new GOval(25, 25, 50, 50);
ball.setFilled(true);
add(ball)
boolean isRed = true;
while(true) {
if(isRed) {
ball.setFillColor(new Color(0, 255, 0));
isRed = false;
} else {
ball.setFillColor(new Color(255, 0, 0));
isRed = true;
}
pause(1000);
}
}
}
The java.awt package contains a class called
Point for representing points on the
two-dimensional plane. It contains the following methods.
double getX()double getY()void translate(double dx, double dy)dx horizontally
and by a distance of dy vertically.Suppose that the Point variable
query already refers to a point somewhere on the plane.
Write a program fragment that reflects the point so that its
x-coordinate is positive. That is, if the point's
x-coordinate is negative, then
its x-coordinate should become |x| (its
absolute value). The
only Point methods you should use are those listed
above.
if(query.getX() < 0) {
query.translate(2 * -query.getX(), 0.0);
}
Each variable declared within Java has a scope. Explain what scope means, and explain how the Java compiler determines a variable's scope from a program.
A variable's scope is the region of the program where the variable can be referenced. In a Java program, a variable's scope is any part of the program between the line containing its declaration down to the closing of the set of braces that contains the declaration.
Complete the following program fragment so that it repeatedly reads values from the user until the user enters an integer between 0 and 100. (Both 0 and 100 should be accepted.) The program should then display A if the number is 90 or above, B if the number is 80 or above (but less than 90), and worse if the number is below 80.
import acm.program.*;
public class GetScore extends Program {
public void run() {
int score = readInt("Grade: ");
while(score < 0 || score > 100) {
score = readInt("Grade: ");
}
if(score >= 90) {
println("A");
} else if(score >= 80) {
println("B");
} else {
println("worse");
}
}
}
What does the below program fragment display?
int n = 5;
String sum = "";
while(n > 0) {
sum = sum + n;
n--;
}
println(sum);
What are the first and last lines displayed by the below program fragment?
int i = 1234;
while(i < 4321) {
String s = "" + i;
if(s.substring(1, 2).equals("5")) {
println(s);
}
i++;
}
The first is 1500
and the last is 3599
.
The below fragment is meant to continue reading and echoing strings typed by the user until the user enters the word stop. However, the program never actually stops. Explain why, and describe how to repair the program to work as intended.
String line = this.readLine();
while(line != "stop") {
this.println(line);
line = this.readLine();
}
Using == or != to compare strings
tests whether the both strings reference the same location of
memory. As a result, the operators often indicate the strings
are different — because they are at different locations in
memory — even when they happen to contain identical
characters.
The solution is to use the String class's
equals method, as in
.while(!line.equals("stop"))
Suppose a user executes the following code fragment.
String str = "arboreteum";
int j = 0;
int i = 0;
while(i < str.length()) {
if(str.substring(i, i + 1).equals("r")) {
print(str.substring(j, i));
j = i + 1;
}
i++;
}
println(str.substring(j));
Show all the values taken on by the variables i and
j as this fragment executes.
Also, what does the fragment print?
i 0 1 2 3 4 5 6 7 8 9 10 j 0 2 5It prints
aboeteum.
Tabulate the values taken on by the variables i and
j as the
program fragment below executes.
Also, what will the program print?
String str = "barber";
int i = 0;
while(i < str.length()) {
String c = str.substring(i, i + 1);
int j = 0;
while(j < i && !str.substring(j, j + 1).equals(c)) {
j++;
}
if(j == i) {
print(c);
}
i++;
}
The program displays bare
.
i |
0, | 1, | 2, | 3, | 4, | 5, | 6 | |||||||||
j |
0, | 0, | 1, | 0, | 1, | 2, | 0, | 0, | 1, | 2, | 3, | 4, | 0, | 1, | 2 |
Complete the following program so that it displays the number of lower-case r's in the string entered by the user.
import acm.program.*;
public class CountRs extends Program {
public void run() {
String line = readLine("Line of text: ");
int count = 0;
int i = 0;
while(i < line.length()) {
if(line.substring(i, i + 1).equals("r")) {
count++;
}
i++:
}
println(count);
}
}
Complete the program below so that it computes another string containing every other letter from the string typed by the user. For example, if the user enters blackberry, the program should compute baker, and if the user enters steadfastness, the program should compute sedates.
Note that your program should build up its result in the
out variable rather than display letters as it goes
through the string.
public class ShowEveryOther extends Program {
public void run() {
String in = this.readLine("Give me a line: ");
String out = "";
// your solution here
this.println("Result is: " + out);
}
}
int i = 0;
while(i < in.length()) {
out = out + in.substring(i, i + 1);
i += 2;
}
Complete the below program so that it reads a line from the
user; but as long as the user types an empty line, it displays
Please enter
your name.
and reads another line from the user. But when the user
enters a line that isn't empty,
where name is the line typed by the user,
the program should display Hello,
name.
and then exit.
public class Solution extends Program {
public void run() {
String name = readLine("Please enter your name. ");
while(name.equals("")) {
name = readLine("Please enter your name. ");
}
println("Hello, " + name + ".");
}
}
Complete the following program so that after reading an integer
n from the integer, it displays a square of
asterisks with n rows and n columns.
You may assume that n is not negative.
Square size? 4 **** **** **** ****
import acm.program.*;
public class StarSquare {
public void run() {
int n = readInt("Square size? ");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
print("*");
}
println();
}
}
}
Translate the following code fragment into an equivalent fragment
without any for loop.
int sum = 1;
for(int i = n; i > 1; i = i / 2) {
sum = sum + i;
}
int sum = 1;
int i = n;
while(i > 1) {
sum = sum + i;
i = i / 2;
}