Midterm 0

One-page version suitable for printing.

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?

Solution

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.)

Solution

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() {



}

Solution

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.

Solution

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 {



}

Solution