CS 2136 B-Term 1998
SAMPLE EXAM 2
All five problems count equally.
1. Define each of the following terms. Answer parts a) and b) in the context of the Java programming language. (Just one or two sentences each.)
2. Define dymanic or late binding (these are synonyms) and give a brief example of code in Java where dynamic binding would be used.. Explain why your example uses dynamic binding. (You do not need to write a complete program – just enough to show where the dynamic binding would be used.)
3. We discussed two approaches in Java for re-using an existing class inside
a new class
a) Give two examples of Java code, one using each method for re-using an existing class.
b) Give general guidelines for when you should use each of these methods.
4. You want to write an applet which has a graphical user interface on which there will be a button. When the button is pressed, some action is taken (you don’t have to actually write the code for the action.)
a) Describe all the steps necessary to create the button and have the action take place when the button is pressed. (No code for this part of the question.
b) Write Java code to create the button and have the action take place. In place of the code for the actual action, you can just put a comment // some action takes place.
5. a) Write a complete Java class definition, including the bodies of all the methods, for a class of Times. The data members of the class will be hours (any non-negative integer) and minutes (integer between 0 and 59). Your constructor must assure that objects are initialized with legal values. (If it is presented with illegal values, create a time with value 0 hours and 0 minutes. Provide a method to add two times. The add method should not change either of the two times passed to it, and but should return a new Time object containing the sum. (No other methods are required.)
b) Create a Java class of PreciseTimes as a sub-class of Times. In addition to hours and minutes, PreciseTimes has an instance variable of milliseconds (a integer between 0 and 999). Write a constructor, again assuring that the values are legal. If any of the values are illegal, return a precise time all of whose variables are 0.