Midterm 0

Statistics:

(All numbers are out of the possible 80 points.)

mean 55.674 (2561.000/46) stddev 10.966 median 55.000 midrange 48.000-65.000 # avg 1 8.67 / 10 2 5.48 / 10 3 12.06 / 15 4 12.50 / 20 5 16.96 / 25

Question 0-1

Suppose a user executes the below run() method.

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

Question 0-2

What distinguishes the purpose of a class variable from an instance variable? I am not asking how they are syntactically distinguished in Java. (In Java, an instance variable is declared directly inside the class. A class variable is also, but it additionally includes the static keyword.)

Question 0-3

Fill in the below method so that, when executed, it reads an integer n from the user and displays the positive even integers that are less than or equal to n, in ascending order. Following is a sample transcript illustrating how an execution of the method should appear.

Number? 8
2
4
6
8
public static void run() {



}

Question 0-4

Complete the below class method so that it reads two strings from the user and displays ``yes'' if the second string includes only characters from the first, and ``no'' if it contains any characters that the first does not include.

For example, I should be able to execute your method and see the following. (Boldface indicates what the user types.)

brillig glib
yes
Or I might see the following. In this example, it prints ``no'' because broil includes the letter o, which does not occur in brillig.
brillig broil
no

public static void run() {



}

To accomplish this, you may find the following String instance methods useful. We discussed these in class.

int length()
Returns the number of characters in the target string.

char charAt(int i)
Returns the character at index i. For example, if str holds the string brillig, str.charAt(2) would return the character 'i'.

int indexOf(String s)
Returns the index where s occurs first within the string on which the method is called. If s occurs nowhere within the target string, the method returns -1. For example, if str holds the string brillig, str.indexOf("il") would return 2, since this is the index where il occurs first.

Question 0-5

At right, write a definition of a new type, called IntRange, representing a range of integers. This class should support the following instance methods.

IntRange(int start, int end)
(Constructor method) Sets up a new IntRange object representing the set of integers between start and end, including these two endpoints. The method may assume that the first parameter is below or equal to the second parameter.
int getSize()
Returns the number of integers in the range.
boolean contains(int i)
Returns true if i lies within the range.
The following example method, which uses the IntRange class you define, illustrates how the class should work.
public static void run() {
    IntRange range = new IntRange(2, 4);
    IO.println(range.getSize());         // this prints ``3''
    IO.println(range.contains(1));       // this prints ``false''
    IO.println(range.contains(3));       // this prints ``true''
    IO.println(range.contains(4));       // this prints ``true''
    IO.println(range.contains(5));       // this prints ``false''
}
public class IntRange {



}

Solutions

Solution 0-1

There were actually three versions of this question. One began with n at 26, one at 22, and one at 20. Here are the answers for these three versions.
n 26 13  6  3  1  0
k  0  1     2  3
In this case, the program would print 3.
n 22 11  5  2  1  0
k  0  1  2     3
In this case, the program would print 3.
n 20 10  5  2  1  0
k  0     1     2
In this case, the program would print 2.

Solution 0-2

A class variable is at a fixed location in memory. There can only be one copy of it available at any time. An instance variable, on the other hand, is associated with each individual instance of the class.

Solution 0-3

There were two versions of the question. One asked for the even integers, and one for the odd integers.
public static void run() {
    IO.print("Number? ");
    int num = IO.readInt();
    for(int i = 2; i <= num; i += 2) {
        IO.println(i);
    }
}
The odd-integer answer is identical, except that it begins i at 1.
    for(int i = 1; i <= num; i += 2) {

Solution 0-4

public static void run() {
    String first = IO.readString();
    String second = IO.readString();

    for(int i = 0; i < second.length(); i++) {
        if(first.indexOf("" + second.charAt(i)) == -1) {
            IO.println("no");
            return;
        }
    }
    IO.println("yes");
}
One of the subtleties of this answer that nobody correctly addressed was that the charAt() method returns a char, while the indexOf method requires a String. The solution is to append the character returned by charAt() to an empty string, which results in a string consisting only of that single letter that charAt() returns.

Solution 0-5

public class IntRange {
    private int first;
    private int last;

    public IntRange(int start, int end) {
        first = start;
        last = end;
    }

    public int getSize() {
        return last - first + 1;
    }

    public boolean contains(int i) {
        return start <= i && i <= last;
    }
}