8:00 Quiz 1

One-page version suitable for printing.

Statistics:

mean     21.688 (347.000/16)
stddev   4.984
median   22.000
midrange 20.000-25.000

# avg
1 6.94 / 10
2 8.44 / 10
3 6.31 / 10

Question 1-1

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

public static void run() {
    int n = IO.readInt();
    int i = 0;
    while(n != 1) {
        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 Q1A {
  public static void run() {
    DrawableFrame w;
    w = new DrawableFrame();
    w.show();
    Graphics g = w.getGraphics();
    g.fillRect(50, 50, 100, 100);
    g.setColor(Color.white);
    g.fillRect(75, 0, 50, 200);
    g.fillRect(0, 75, 200, 50);
  }
}
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