V22.0470 FINAL EXAM SOLUTION

Written By Haytham Allos, Fall 2002

New York University

 

QUESTION 1 ():

 

Consider the class Flower.  Given your knowledge of some common things about flowers, show a class hierarchy in which the class Flower inherits from another class, which, in turn, inherits from yet another class.  Discuss inheritance from class Flower for other closely related subclasses.

 

NOTE:  Solution to this problem varies widely.  One possibility is the following:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


EXPLANATION:  Two potentially subclasses of class Flower are Carnation and Rose.  This inheritance is valid due to the is-a relationship, i.e. “Carnation is a flower” and “Rose is a flower”.  The subclasses should inherit some of the common characteristics and behavior associated with flowers such as every flower has a stem and petals.  However, different flowers have different number of petals.  The number of petals in a carnation is different from the number of petals in a rose.  Hence, the number of petals is particular to a certain kind of a flower which should be defined in the subclasses but at the same time still inheriting the fact that the subclasses have petals.

 

QUESTION 2 ():

 

ANSWER:  123

 

QUESTION 3 ():

 

A class characterizes the structure of states and behaviors that are shared by all its instances.

 

An object has a unique identity, a state, and behaviors.

 

An object is an instance of a class.

 

QUESTION 4 ():

Polymorphism means that a single name can refer to different objects at different times or under different circumstances.  A polymorphic variable is a variable that can be a handle for different kinds of objects.  A polymorphic method means that the same message can lead to different methods, depending on the message receiver (or in some languages depending upon the entire signature, including the message argument types).

 

QUESTION 5 ():

The implementation of a module should be separated from its contractual interface and hidden from the clients of the module.

 

In Java, we can hide instance attributes of a class with the keyword private; the

keyword public makes an instance method of a class available to the class's clients.

 

QUESTION 6 ():

  1. The first column, exclusive of the numbers, refers to needs and desirable properties that may arise in Java programming or OO design.  The second column lists features, techniques or Java methods, classes that help to deal with problems or provide desirable outcomes.  Match each feature/technique/method/class to a need/property that it helps to address.  Your answers must be provided by filling in the appropriate "need/property" number to the right of the feature/technique/method/class that is designed to address this need. (20 pts.)

 

 

 

Need/property

 

Feature/technique/method/class

 

 

1

Requires children to implement a method

 

interface

__6__

 

 

 

 

 

 

2

Reclaims space associated with unused objects

 

encapsulation

__4__

 

 

 

 

 

3

Specifies a class's responsibility to clients

 

garbage collection

__2__

 

 

 

 

 

4

Hides details of implementation

 

this.

__5__

 

 

 

 

 

5

Gives an object the ability to refer explicitly to its own instance methods and attributes

 

protocol

__3__

 

 

 

 

 

6

Provides only a protocol that may be implemented by appropriate classes

 

abstract

__1__

 

QUESTION 7 ():