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
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);
}
n k
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.)
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() {
}
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.)
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 glib yes
brillig broil no
public static void run() {
}
To accomplish this, you may find the following String instance methods useful. We discussed these in class.
At right, write a definition of a new type, called IntRange, representing a range of integers. This class should support the following instance methods.
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 {
}
In this case, the program would print 3.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 2.n 20 10 5 2 1 0 k 0 1 2
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) {
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.
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;
}
}