Java Reference
In-Depth Information
2.
What class should be the superclass of any exceptions you create in Java?
a.
Throwable
b.
Error
c.
Exception
3.
If a class implements the
Runnable
interface, what methods must the class contain?
a.
start()
,
stop()
, and
run()
b.
actionPerformed()
c.
run()
Answers
1. b.
2. c.
Throwable
and
Error
are of use primarily by Java. The kinds of errors you'll
want to note in your programs belong in the
Exception
hierarchy.
3. c.
The
Runnable
interface requires only the
run()
method.
Certification Practice
The following question is the kind of thing you could expect to be asked on a Java pro-
gramming certification test. Answer it without looking at today's material or using the
Java compiler to test the code.
The
AverageValue
application is supposed to take up to 10 floating-point numbers as
command-line arguments and display their average.
Given:
public class AverageValue {
public static void main(String[] arguments) {
float[] temps = new float[10];
float sum = 0;
int count = 0;
int i;
for (i = 0; i < arguments.length & i < 10; i++) {
try {
temps[i] = Float.parseFloat(arguments[i]);
count++;
} catch (NumberFormatException nfe) {
System.out.println(“Invalid input: “ + arguments[i]);
7
}