Solutions

Problem 1. (6 points) Quickies.

a. A final field is set by an initializer and cannot change.
Since it usually has the same value for every object in the class,
there's no point in having a separate copy of that value for every object.

b. Errors that might be produced by the code line
Object o = s.subString(0);

A run-time error (a NullPointerException to be precise) if s
is null.

A compile-time error if the static type of s does not support
a subString method.

A compile-time error if subString does not return an object.

A compile-time error if s is a local variable that is not
initialized before this line of code executes (and the Java compiler
manages to figure this out, which depends on compiler flow analysis
techniques that you guys don't need to know anything about).

A compile-time error if s is a primitive type, or
simply hasn't been declared at all.

A compile-time error if o has already been declared
as a local field in this method.

No doubt there are other reasonable answers.
(However, no error occurs if s references the empty string "".
Sorry.)

c. The switch statement is buggy because it is missing
break statements at the end of each case (or at least the first two).
For instance, it will always report that i/3 has a remainder of 2,
regardless of the value of i.

To get the classes to compile, you also need to add the following
abstract method declaration to the List class.

public abstract int countRuns();

(Alternatively, you could have added a non-abstract method
with an implementation just so there's a method to override,
but the implementation must return some int or it won't compile.)
Mail inquiries to
cs61b@cory.eecs