Javanotes 5.1.2, Answers For Quiz On Chapter 8
Javanotes 5.1.2, Answers For Quiz On Chapter 8
Answer: A robust program is one that can handle errors and other unexpected
conditions in some reasonable way. This means that the program must
anticipate possible errors and respond to them if they occur.
Question 5: Java has a predefined class called Throwable. What does this class
represent? Why does it exist?
Answer: The class Throwable represents all possible objects that can be thrown
by a throw statement and caught by a catch clause in a try..catch
statement. That is, the thrown object must belong to the class
Throwable or to one of its (many) subclasses such as Exception and
RuntimeException. The object carries information about an exception
from the point where the exception occurs to the point where it is
caught and handled.
Question 6: Write a method that prints out a 3N+1 sequence starting from a given
integer, N. The starting value should be a parameter to the method. If
the parameter is less than or equal to zero, throw an
IllegalArgumentException . If the number in the sequence becomes too
large to be represented as a value of type int, throw an
ArithmeticException.
(Note that it would be possible to declare that this routine can throw
exceptions by adding a "throws" clause to the heading:
static void printThreeNSequence(int N)
throws IllegalArgumentException,
ArithmeticException {
Question 7: Rewrite the method from the previous question, using assert statements
instead of exceptions to check for errors. What the difference between
the two versions of the method when the program is run?
Answer: We can replace the if statements that check for errors with assert
statements that give the same error messages:
static void printThreeNSequence(int N) {
// Print the 3N+1 sequence starting from N.
// Precondition: N > 0 and the 3N+1 sequence for
N does not contain
// any numbers that are too big to be represented
as 32-bit ints.
assert N > 0 : "Starting value for 3N+1 sequence
must be > 0.";
System.out.println("3N+1 sequence starting from " + N
" is: ");
System.out.println(N);
while (N > 1) {
if (N % 2 == 0) { // N is even. Divide by 2.
N = N / 2;
}
else { // N is odd. Multiply by 3 and add 1.
assert N <= 2147483646/3 : "Value has
exceeded the largest int.";
N = 3 * N + 1;
}
System.out.println(N);
}
}
The first version of the method will always check for errors when the
program is run. The second version, on the other hand, does not actually
do any error checking when the program is run in the ordinary way. In
order for assert statements to be executed, the program must be run
with assertions enabled. The assert statements are really there only to
do error-checking during debugging and testing. (In this particular case,
I would say that an exception should definitely be thrown when N
exceeds the maximum legal value, but that it's reasonable to use an
assert to check whether N > 0.)
Answer: try {
processData();
}
catch (IOException e) {
System.out.println("An IOException occurred while
processing the data.");
}
Answer: Terminating the program is too drastic, and this tactic certainly doesn't
lead to robust programs! It's likely that the subroutine doesn't know
what to do with the error, but that doesn't mean that it should abort the
whole program. When the subroutine throws an exception, the
subroutine is terminated, but the program that called the subroutine still
has a chance to catch the exception and handle it. In effect, the
subroutine is saying "Alright, I'm giving up. Let's hope someone else can
deal with the problem."
Question 11: Suppose that a program uses a single thread that takes 4 seconds to
run. Now suppose that the program creates two threads and divides the
same work between the two threads. What can be said about the
expected execution time of the program that uses two threads?
Answer: The execution time will depend on whether the program is being run on
a computer that has more than one processor. If so, the execution time
could be as little as 2 seconds, since each of two processors can do half
of the 4-seconds worth of work. If the computer has only one processor,
however, the two-threaded program will still take 4 seconds, since all the
work will have to be done by the single processor.