Do Exercise 4 of Chapter 16 in the textbook (page 318), changed as follows:
Suppose we have a classwith a method
of type
. Suppose further that a derived class
overrides this method with one of type
. Different languages have different rules about how the types
and
, and
and
must be related. Investigate and report on this aspect of inheritance, citing the sources you used. Answer the following questions:
Write a program that does tests to see what happens for the 18 situations outlined in Exercise 4 on page 357. The program should have the following structure:
class ExceptionTest {
static class Exception1 extends Exception { };
static class Exception2 extends Exception { };
static class Exception3 extends Exception { };
static class Exception4 extends Exception { };
private static String test(...) throws Exception {
System.out.print("A");
try {
System.out.print("B");
... // do something: return "1", or throw Exception1 or 2
System.out.print("C");
} catch (Exception2 ex) {
System.out.print("D");
... // do something: return "2", or throw Exception3
System.out.print("E");
} finally {
System.out.print("F");
... // do something: return "3", or throw Exception4
System.out.print("G");
}
System.out.print("H");
return "4";
}
private static void dotest(String t, ...) {
System.out.print(t + ". ");
try {
String s = test(...);
System.out.println(" return " + s);
} catch (Exception ex) {
System.out.println(" throw " + ex);
}
}
public static void main(String[] args) {
dotest("a",...);
dotest("b",...);
...
dotest("r"...); // 18 times
}
}
Leave your code in the homework9 directory of your AFS volume.
Question: to be answered in written Homework:
There are four pairs of situations which have the same effect. What are these pairs? Explain why they have the same effect.I found the ``
sort -k 2'' Unix command to
be very useful; ``man sort'' for more information
Leave your code in the directory homework9 in your AFS folder:
ExceptionTest.java.
Turn in your written solution to Exercise 4 and the question to the
Exception Test results on paper.