1) This exam is open-note, open book.
2) You must answer all of the questions.
3) Answer all the questions on a separate sheet of paper.
4) You must use Java to implement the coding questions.
5) Good luck!
1.
(22 points) Choose the appropriate term from the following list to fill in each
abstract blank below: binary executable extends inheritance interface module class friend jar namespace non-‐ virtual parametric private composition diamond Error generic hash tables implementation
Exception implements java library longjmp
Object protected public jump tables object-‐ oriented pure virtual overloading replicated package
Runtime
Exception setjmp shared supertype tar source static subclass subtype template Throwable throws types virtual machine void vtables …
a.
parametric polymorphism uses templates instantiated with types while
superclass virtual inheritance polymorphism makes use of class hierarchies to support
polymorphism.
b.
Java uses the class Object for its generic class type.
c.
replicated 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 friend and namespace mechanisms.
e.
A virtual machine 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.
Exception What Java class should be used to catch any generic exception?
g.
In Java, an abstract type can be declared using a(an) interface.
h.
vtables are used to implement virtual methods at run-‐time
i.
In C++, a method with the following type of declaration is called a pure virtual method.
void draw() = 0; j.
A jar 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? implementation inheritance means that an object wants to inherit another object’s behavior without inheriting its public set of methods while interface inheritance means that you want to inherit another object’s public set of methods, although not necessarily its behavior.
b.
Why is it always okay to use a subtype object where a supertype object is
expected?
Because a subtype implements a superset of the supertype’s interface, and specifically must implement all of the operations/methods in the
supertype’s interface. c.
What is the advantage of polymorphism?
It allows the same algorithm/class to be applied to multiple types with the same piece of code
d.
Why would a programmer declare a function to be abstract?
Because there is no good common implementation of the function that can be shared by the subclasses and hence the programmer wants every subclass to provide its own implementation
e.
What is the difference between protected and package-‐level access in Java?
A protected member can be accessed by a subclass in another package but a package-‐level member cannot be accessed by a subclass in another package
f.
Why does Java require a fully qualified package name in order to invoke a class stored in a package?
It converts the fully qualified name to a directory path and then appends this path to each directory provided to the classpath flag in order to locate the class file for that class
g.
Name 2 advantages of modern exception handlers (you can use one sentence for each advantage)
1.
They allow the error handling code to be separated from the normal processing code, thus providing code that is easier to read
2.
They allow stack frames to be cleanly popped and stored register values in these frames written to their corresponding variables when an exception is thrown “up” the call stack to a calling function
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). double getGallons(Scanner s) {
double returnValue = 0.0;
try {
System.out.print("please enter the number of gallons: "); returnValue = s.nextDouble();
}
catch (InputMismatchException e) {
returnValue = 10;
} finally {
System.out.println("returning " + returnValue + " gallons");
}
return returnValue;
} 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? double getGallons(Scanner s) throws InputMismatchException
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
. class Label<E> {
E value;
Label(E initialValue) { value = initialValue; }
E getValue() { return value; }
void setValue(E newValue) { value = newValue; }
}
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). abstract class IOobject {
abstract void open(); abstract void close();
} abstract class WriteObject extends IOObject {
abstract void write(String s);
} class Printer extends WriteObject {
void open();
void close();
void write(String s);
} abstract class ReadWriteObject extends IOObject {
abstract String read(s);
abstract void write(String s);
} class Console extends ReadWriteObject {
void open();
void close();
String read(s);
void write(String s);
} class Widget extends ReadWriteObject {
void open();
void close();
String read(s);
void write(String s);
}
class File extends ReadWriteObject {
void open();
void close();
String read(s);
void write(String s);
}