9:40 Quiz 1

One-page version suitable for printing.

Statistics:

mean     23.767 (713.000/30)
stddev   3.303
median   24.000
midrange 22.000-26.000

# avg
1 6.9  / 10
2 9.3  / 10
3 7.57 / 10

Question 1-1

Suppose a user executes the following run() method and types the numbers 13.

public static void run() {
    int n = IO.readInt();
    int i = 0;
    while(n != 4) {
        if(n % 2 == 1) n = 3 * n + 1;
        if(n % 2 == 0) n /= 2;
        i++;
    }
    IO.println(i);
}
Show all the values taken on by the variables of the program.
n
i
What does the method print?

Solution

Question 1-2

Say we execute the following method.

import csbsju.cs160.*;

public class Q1B {
  public static void run() {
    DrawableFrame w;
    w = new DrawableFrame();
    w.show();
    Graphics g = w.getGraphics();
    g.fillRect(80, 40, 40, 120);
    g.fillRect(40, 80, 120, 40);
    g.setColor(Color.white);
    g.fillRect(80, 80, 40, 40);
  }
}
The fillRect() method draws a filled rectangle. It takes four integer parameters: the left side's x-coordinate, the top side's y-coordinate, the width, and the height.

Complete the below window as it will appear on the screen.

Solution

Question 1-3

Fill in run() so that, when executed, the method will count the number of positive numbers the user types before the user types 0. A user executing the method should see the following, for example. (Boldface indicates what the user types.)

123
-2
8
123
0
3
In this case, the program displays 3, because the user typed three positive numbers (123, 8, and 123).
import csbsju.cs160.*;
public class CountPositives {
    public static void run() {



    }
}

Solution