0% found this document useful (0 votes)
11 views9 pages

III13-14 - OOPWITHSolutions

The document contains a comprehensive examination for Object Oriented Programming at Birla Institute of Technology and Science, Pilani, dated December 6, 2013. It includes multiple questions covering topics such as the clone() method, static methods, cloning differences, design patterns, and class diagrams. The exam is divided into three parts: closed book, with specific instructions and a total of 80 marks available.

Uploaded by

f20220384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

III13-14 - OOPWITHSolutions

The document contains a comprehensive examination for Object Oriented Programming at Birla Institute of Technology and Science, Pilani, dated December 6, 2013. It includes multiple questions covering topics such as the clone() method, static methods, cloning differences, design patterns, and class diagrams. The exam is divided into three parts: closed book, with specific instructions and a total of 80 marks available.

Uploaded by

f20220384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, PILANI

OBJECT ORIENTED PROGRAMMING (CS/IS F213), OBJECT ORIENTED PROGRAMMING AND DESIGN (CS/IS C313)
COMPREHENSIVE EXAMINATION, PART-A (CLOSED BOOK)
DATE: 06/12/2013 RECOMMENDED TIME: 01 HOURS MAX MARKS: 30

Q.1 Why clone() method is not accessible from outside on a Cloneable instance? [04 Marks]
Answer:
clone() is defined as a protected method in the Object class. When we implement the interface Cloneable then only that class gets
the license to call the default Object.clone() method i.e., only the methods of that implementing class can call the inherited clone()
method. The Object.clone() method carries a protected access, so even if we want the default clone() (Shallow Copy) to be called
from outside, we need to override the clone() method in the class implementing Cloneable and promote the access specifier of the
overriden clone() method to 'public'.

Q.2 Give four reasons to make a method static in Java. State two disadvantages of using static methods. [04 Marks]
Answer:
We can make a method static in Java in following scenario:
(a) Method doesn't depends on object's state, in other words doesn't depend on any member variable and everything they need is
passed as parameter to them.
(b) Method belongs to class naturally can be made static in Java.
(c) Utility methods are good candidate of making static in Java because they can directly be accessed using class name without even
creating any instance. Classic example is java.lang.Math
(d) In various designs pattern which need a global access e.g. Singleton pattern, Factory Pattern.
Disadvantages:
1. You cannot override any static method in Java.
2. Since static method maintains global state they can create subtle bug in concurrent environment which is hard to detect and fix.

Q.3 Explain with the help of example the main difference between shallow cloning and deep cloning of objects? [04 Marks]
Answer: They should write some code to demonstrate shallow and deep cloning of objects

Q.4 Consider the definition of class named PurchaseOrder given below:


public class PurchaseOrder{
private List<Order> lstOrder = new ArrayList<Order>();
public double getTotalOrderCost(){
double total =0;
for(Order order : lstOrder) {
total += order.getOrderCost();
}
return total;
}
}
Draw the sequence diagram to capture the system response for the execution of the method getTotalOrderCost() in the
PurchaseOrder class. [04 Marks]
Answer:

Q.5 Consider an online discussion forum, where users can post messages under specific topics and they can also post individual
messages. All Topics and messages are unique and are displayed alphabetically. Periodically the administrator decides to delete all
the topics, the messages within those topics, and the individual messages from the discussion forum. Which design pattern is best in
this situation? Give reason for your choice of design pattern and draw the UML class diagram. [04 Marks]

Page 1 of 2
Answer: 4
The pattern which can suitably be applied here is a composite pattern. The composite pattern is applied when there are objects
(composites objects) that contain other objects (primitive objects) and we want to manipulate composites exactly the same way how
we manipulate primitive objects. In this case Topics compose Messages and the operation that is desired is a delete operation, such
that, if a forum has to be cleaned the call to delete should not discriminate whether it is a Topic (which is a composite) or a Message
(which is a primitive/leaf) is deleted. Identifying pattern with reason : 0/2M UML diagram : 0/1/2M

Q.6 Write a Java class that provides support for arithmetic on the integers performed relative to some modulus “p” which should be
a prime number. A class called Modular is suggested which keeps track of the modulus “p”. Only one instance of the Modular
class can be constructed by specifying the modulus value after checking if it is a prime number or not. An inner class ModInt
represents an integer number. The reduceMod() method returns a new ModInt with the value number%p. Class Modular should
be a thread safe class and cannot be cloned. [NOTE: Assume that the implementation of isPrime(p: int) method is already provided
by the Modular class. The method isPrime(p: int) returns true if the integer p is a prime number]. [10 Marks]

public class Modular{

private int primeMod;


private static Modular instance = null; // 0/1/2 MARKS

// 0/1 MARK
private Modular(int primeMod) { this.primeMod = primeMod; }

// 0/1/2 MARKS
public static synchronized Modular getModularInstance(int number){
// 0/1/2 MARKS
if(null == instance && isPrime(number)){
instance = new Modular(number); // 0/1 MARK
} return instance;
}

// 0/1 MARK
public ModInt reduceMod(int number){ return new ModInt(number%primeMod); }

// 0/1 MARK
class ModInt { private int number;
public ModInt(int number) { this.number = number; }
}

private static boolean isPrime(int number){ //Assume implementation is provided }


}

Page 2 of 2
BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, PILANI
OBJECT ORIENTED PROGRAMMING (CS/IS F213), OBJECT ORIENTED PROGRAMMING AND DESIGN (CS/IS C313)
COMPREHENSIVE EXAMINATION, PART-B (CLOSED BOOK)
DATE: 06/12/2013 RECOMMENDED TIME: 01 HOURS MAX MARKS: 20

INSTRUCTIONS -
1. Write your answers on the space provided below.
2. Write answers clearly (in readable handwriting) without overwriting and too much cutting.
3. Penalty of 2 marks for unreadable answers.

Q.1 A book is written by an author, published by a publisher, sold by a book store, and read by a reader. Moreover, for a
reader to read a book, s/he must buy it from a book store that is selling it. Draw a use case diagram for this scenario,
showing relationships between different use cases. [05 Marks]

Q.2 A library has books, videos, and CDs that it loans to its users. All library material has id# and a title. In addition, books
have one or more authors, videos have one producer and one or more actors, while CDs have one or more entertainers, these
people can be approached through email, fax, and phone. The library maintains one or more copies of each library item
(book, video or CD). Copies of all library material can be loaned to users. Reference-only material is loaned for 2hrs and
can’t be removed from the library. Other material can be loaned for 2 weeks. For every loan, the library records the user,
the loan date and time, and the return date and time. For users, the library maintains their name, address and phone
number. [15 Marks]

 Draw a class diagram for the description above. Make sure to show attributes, multiplicities and relationships, where
appropriate. No need to show any methods/operations.

Page 1 of 1
BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, PILANI
OBJECT ORIENTED PROGRAMMING (CS/IS F213), OBJECT ORIENTED PROGRAMMING AND DESIGN (CS/IS C313)
COMPREHENSIVE EXAMINATION, PART-C (OPEN BOOK)
DATE: 06/12/2013 RECOMMENDED TIME: 01 HOUR 15 MIN MAX MARKS: 30

Q.1 We've an interface MediaPlayer interface and a concrete class AudioPlayer implementing the MediaPlayer
interface. AudioPlayer can play "mp3" format audio files by default. We have another interface
AdvancedMediaPlayer and concrete classes VLCPlayer and Mp4Player implementing the
AdvancedMediaPlayer interface. These classes can play "vlc" and "mp4" format files respectively. We want to
make AudioPlayer to play other formats "vlc" and "mp4" as well. Also the AudioPlayer class can apply either of
the three different types of skins (Elegant, Corporate and Revert) see Fig.1 (a), Fig.1 (b) and Fig.1 (c) below. The
default skin which is applied to the AudioPlayer class is Elegant. It is possible for the user to replace/ change/
apply/ choose a different skin when the player is running. Fig.1 (a) below shows the running AudioPlayer its
default skin (Elegant). It also shows that one can change the skin by pressing "Skin chooser" while the player is
running. Fig. 1(b) and Fig. 1(c) shows the other two skins which can be applied to the AudioPlayer.

(a) Which design pattern(s) you would use for this application? Give a short explanation why? [05 M]
(b) In particular, show an appropriate class diagram(s) and [10 M]
(c) enough code fragments to illustrate your use of the design pattern(s) to solve the problem. [15 M]

Fig.1 (a) AudioPlayer in its default skin (Elegant)

Fig.1 (b) AudioPlayer in its Corporate skin

Fig.1 (c) AudioPlayer in its Revert skin


Answer
In this question we will use two design patterns (a) Adapter and (b) Strategy. The explanation for the choice of the two
design patterns is given below:

CASE-1
We've an interface MediaPlayer interface and a concrete class AudioPlayer implementing theMediaPlayer interface.
AudioPlayer can play "mp3" format audio files by default. We're having another interface AdvancedMediaPlayer and
concrete classes implementing the AdvancedMediaPlayer interface. These classes can play "vlc" and "mp4" format
files. We want to make AudioPlayer to play other formats as well. So what we see here is that the interfaces of
AudioPlayer, VLCPlayer and the Mp4Player are incompatible with each other hence we need a Adapter. Adapter
pattern works as a bridge between two incompatible interfaces. To attain this, we've created an adapter class
MediaAdapter which implements the MediaPlayer interface and uses AdvancedMediaPlayer objects to play the
required format. AudioPlayer uses the adapter class MediaAdapter passing it the desired audio type without knowing
the actual class which can play the desired format.

CASE-2
We want to change the Skin of the AudioPlayer at runtime. Each Skin exhibits a behaviour of the AudioPlayer class. So
here we will use the Strategy design pattern. Using the Strategy we can change the behaviour of a class or its algorithm
at run time.
MINIMAL CODE FOR THE DESIGN PATTERNS USED

CASE-1: ADAPTER
public class MediaAdapter implements MediaPlayer {

AdvancedMediaPlayer advancedMusicPlayer;

public MediaAdapter(String audioType){


if(audioType.equalsIgnoreCase("vlc") ){
advancedMusicPlayer = new VlcPlayer();
} else if (audioType.equalsIgnoreCase("mp4")){
advancedMusicPlayer = new Mp4Player();
}
}

@Override
public void play(String audioType, String fileName) {
if(audioType.equalsIgnoreCase("vlc")){
advancedMusicPlayer.playVlc(fileName);
}else if(audioType.equalsIgnoreCase("mp4")){
advancedMusicPlayer.playMp4(fileName);
}
}
}

public class AudioPlayer implements MediaPlayer {


MediaAdapter mediaAdapter;

@Override
public void play(String audioType, String fileName) {
//inbuilt support to play mp3 music files
if(audioType.equalsIgnoreCase("mp3")){
S.O.P("Playing mp3 file. Name: "+ fileName);
}
//mediaAdapter is providing support to play other file formats
else if(audioType.equalsIgnoreCase("vlc")
|| audioType.equalsIgnoreCase("mp4")){
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
} else{
System.out.println("Invalid media. "+
audioType + " format not supported");
}
}
}

CASE-2: STRATEGY
Just keep one more variable strategy of type Skin inside the AudioPlayer
class and define a method chooseSkin(). Nothing else is expected if you
have given a correct class diagram for strategy pattern.
BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, PILANI
OBJECT ORIENTED PROGRAMMING (CS/IS F213), OBJECT ORIENTED PROGRAMMING AND DESIGN (CS/IS C313)
COMPREHENSIVE EXAMINATION, PART-C (OPEN BOOK)
DATE: 06/12/2013 RECOMMENDED TIME: 01 HOUR 15 MIN MAX MARKS: 30

Q.1 We've an interface MediaPlayer interface and a concrete class AudioPlayer implementing the MediaPlayer
interface. AudioPlayer can play "mp3" format audio files by default. We have another interface
AdvancedMediaPlayer and concrete classes VLCPlayer and Mp4Player implementing the
AdvancedMediaPlayer interface. These classes can play "vlc" and "mp4" format files respectively. We want to
make AudioPlayer to play other formats "vlc" and "mp4" as well. Also the AudioPlayer class can apply either of
the three different types of skins (Elegant, Corporate and Revert) see Fig.1 (a), Fig.1 (b) and Fig.1 (c) below. The
default skin which is applied to the AudioPlayer class is Elegant. It is possible for the user to replace/ change/
apply/ choose a different skin when the player is running. Fig.1 (a) below shows the running AudioPlayer its
default skin (Elegant). It also shows that one can change the skin by pressing "Skin chooser" while the player is
running. Fig. 1(b) and Fig. 1(c) shows the other two skins which can be applied to the AudioPlayer.

(a) Which design pattern(s) you would use for this application? Give a short explanation why? [05 M]
(b) In particular, show an appropriate class diagram(s) and [10 M]
(c) enough code fragments to illustrate your use of the design pattern(s) to solve the problem. [15 M]

Fig.1 (a) AudioPlayer in its default skin (Elegant)

Fig.1 (b) AudioPlayer in its Corporate skin

Fig.1 (c) AudioPlayer in its Revert skin


Answer
In this question we will use two design patterns (a) Adapter and (b) Strategy. The explanation for the choice of the two
design patterns is given below:

CASE-1
We've an interface MediaPlayer interface and a concrete class AudioPlayer implementing theMediaPlayer interface.
AudioPlayer can play "mp3" format audio files by default. We're having another interface AdvancedMediaPlayer and
concrete classes implementing the AdvancedMediaPlayer interface. These classes can play "vlc" and "mp4" format
files. We want to make AudioPlayer to play other formats as well. So what we see here is that the interfaces of
AudioPlayer, VLCPlayer and the Mp4Player are incompatible with each other hence we need a Adapter. Adapter
pattern works as a bridge between two incompatible interfaces. To attain this, we've created an adapter class
MediaAdapter which implements the MediaPlayer interface and uses AdvancedMediaPlayer objects to play the
required format. AudioPlayer uses the adapter class MediaAdapter passing it the desired audio type without knowing
the actual class which can play the desired format.

CASE-2
We want to change the Skin of the AudioPlayer at runtime. Each Skin exhibits a behaviour of the AudioPlayer class. So
here we will use the Strategy design pattern. Using the Strategy we can change the behaviour of a class or its algorithm
at run time.
MINIMAL CODE FOR THE DESIGN PATTERNS USED

CASE-1: ADAPTER
public class MediaAdapter implements MediaPlayer {

AdvancedMediaPlayer advancedMusicPlayer;

public MediaAdapter(String audioType){


if(audioType.equalsIgnoreCase("vlc") ){
advancedMusicPlayer = new VlcPlayer();
} else if (audioType.equalsIgnoreCase("mp4")){
advancedMusicPlayer = new Mp4Player();
}
}

@Override
public void play(String audioType, String fileName) {
if(audioType.equalsIgnoreCase("vlc")){
advancedMusicPlayer.playVlc(fileName);
}else if(audioType.equalsIgnoreCase("mp4")){
advancedMusicPlayer.playMp4(fileName);
}
}
}

public class AudioPlayer implements MediaPlayer {


MediaAdapter mediaAdapter;

@Override
public void play(String audioType, String fileName) {
//inbuilt support to play mp3 music files
if(audioType.equalsIgnoreCase("mp3")){
S.O.P("Playing mp3 file. Name: "+ fileName);
}
//mediaAdapter is providing support to play other file formats
else if(audioType.equalsIgnoreCase("vlc")
|| audioType.equalsIgnoreCase("mp4")){
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
} else{
System.out.println("Invalid media. "+
audioType + " format not supported");
}
}
}

CASE-2: STRATEGY
Just keep one more variable strategy of type Skin inside the AudioPlayer
class and define a method chooseSkin(). Nothing else is expected if you
have given a correct class diagram for strategy pattern.

You might also like