CS365 Midterm 1) 2) 3) 4) 1. You must answer all of the questions. Place your name on the exam. You must use Java to implement the coding questions. Good luck! (22 points) Choose the appropriate term from the following list to fill in each blank below: abstract executable binary extends class friend inheritance interface jar module non-­‐ virtual protected namespace parametric private composition diamond Error generic hash implementation tables java jump library tables Object object-­‐ overloading oriented public pure replicated virtual static subclass subtype Throwable throws types … setjmp shared source supertype tar template virtual void vtables machine a. ____________________________ polymorphism uses templates instantiated with Exception implements longjmp package Runtime Exception superclass virtual types while ____________________________ polymorphism makes use of class hierarchies to support polymorphism. b. Java uses the class ____________________________ for its generic class type. c. ______________________________ inheritance refers to multiple inheritance in which a subclass receives multiple copies of any shared superclasses. d. C++ attempts to provide some of the benefits of packages/modules through its ______________________________ and ___________________________ mechanisms. e. A __________________________ is an interpreter that simulates the execution of a machine by executing the machine's instruction set using software piece of software that simulates machine instructions f. ________________________________ What Java class should be used to catch any generic exception? g. In Java, an abstract type can be declared using a(an) ___________________________________. h. _________________________________ are used to implement virtual methods at run-­‐ time i. In C++, a method with the following type of declaration is called a _______________________________ method. void draw() = 0; j. A _____________________________ file in Java allows a collection of classes to be bundled together and treated like a single file for execution, much like a binary file in C/C++. 2. (21 points) Short Answer: Answer each of the following questions in a single sentence unless stated otherwise. a. What is the difference between implementation inheritance and interface inheritance? b. Why is it always okay to use a subtype object where a supertype object is expected? c. What is the advantage of polymorphism? d. Why would a programmer declare a function to be abstract? e. What is the difference between protected and package-­‐level access in Java? f. Why does Java require a fully qualified package name in order to invoke a class stored in a package? g. Name 2 advantages of modern exception handlers (you can use one sentence for each advantage) 3. (21 points) Exception handling: a. Write a Java method named getGallons that takes a Scanner object as a parameter and does the following: 1. Asks the user to input gallons as a floating point number and uses the Scanner object to read it (use the nextDouble method). 2. Catches an InputMismatchException if the user enters something other than a double and sets the number of gallons to the default value of 10 3. Regardless of whether or not the user enters a correct value, getGallons prints either the user entered value or the default value. An appropriate printed message might be “returning 15.7 gallons”. Make sure that this message gets printed, even if the specification is later changed so that the InputMismatchException gets rethrown. 4. Returns the number of gallons (either the user-­‐provided value or the default value). b. Suppose that instead of catching and handling the InputMismatchException, that I want to have the calling function handle it. Although I’m not required to, I would like my function to be a good citizen and declare to the compiler that it is not going to handle the InputMismatchException. How would I rewrite the function header for getGallons? 4. (16 points) Write a Java template class named Label that takes a single type parameter named E and supports three methods: a. a constructor that takes a single argument, which is the value of the label, and stores it in an instance variable named value. b. getValue: takes no parameters and returns value. c. setValue: a void method that takes a single parameter of type E and assigns it to value. 5. (25 points) Suppose that you are designing the input/output portion of an application. The application should be able to read from the 1) console, 2) a file, or 3) a widget such as a type-­‐in text box. The application should be able to write to the 1) console, 2) a printer, 3) a file, or 4) a widget such as a type-­‐in text box. You have been given the following specifications: a. Regardless of which type of object the application is reading from or writing to, the application needs to be able to open/close the object (open and close methods that take no parameters and return void). b. The application needs to be able to read a text string from an input object (read method that returns a string) and write a text string to an output object (write method that takes a string as a parameter and returns void). c. Assume consoles, files, and widgets are read/write objects and that a printer is a write-­‐only object. d. Read/write objects should be interchangeable (i.e., it should be easy for a programmer to swap out a console and swap in a file) and so should write-­‐only objects (even though there is only one write-­‐only object in this problem, one could envision adding additional write-­‐ only objects in the future). e. There should be a root, base class for all of your objects. Design a class hierarchy for the above four objects using Java. You should write declarations for the classes and the methods described in this problem (and only these methods). You should not show any implementation. Your answer should make proper use of inheritance and make it clear as to whether or not a class is providing an implementation of a method (this can be done without actually writing the implementation).