Exercise 15.x2:
For each of the following three programs, answer the following questions:Does this program illustrate a covariant return type or a contravariant return type?
Is this program legal Java?
Does the class conform to the specification documented for the interface? Briefly explain.
Program 1:
interface Producer { Object produce(); // returns a new Object each time } class IntegerProducer implements Producer { public Integer produce(){ return new Integer(42); } }
Program 2:
interface IntegerProducer { Integer produce(); // returns a new Integer each time } class Producer implements IntegerProducer { public Object produce(){ return new Object(); } }
Program 3:
interface Producer { Object produce(); // returns a new Object each time } class IntegerProducer implements Producer { Integer answer = new Integer(42); public Integer produce(){ return answer; } }
Do exercise 16.2 on pages 315-317. As a clarification regarding the three declarations at the bottom of page 317: the goal is that in each case, you would leave everything to the left of the equal sign unchanged and would replace the stuff to the right of the equal sign.
Exercise 17.x1: For each of the following two Java statements, indicate whether it is legal, and if so, explain under what circumstances each exception handler would execute. The first version is
try { System.in.read(); } catch(java.io.CharConversionException e2){ System.err.println("Caught a CharConversionException."); } catch(java.io.IOException e1){ System.err.println("Caught an IOException."); }
and the second version is
try { System.in.read(); } catch(java.io.IOException e1){ System.err.println("Caught an IOException."); } catch(java.io.CharConversionException e2){ System.err.println("Caught a CharConversionException."); }
Do exercise 18.6 on page 384.