2021 Object Oriented Programming
2021 Object Oriented Programming
Sc Computer Science
Object Oriented Programming
WBSU
c l a s s T e s t N e s t e d I n t e r f a c e 2 implements A . Message {
p u b l i c v o i d msg ( ) { S y s t e m . o u t . p r i n t l n ( " H e l l o n e s t e d i n t e r f a c e " ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
A . M e s s a g e m e s s a g e =new T e s t N e s t e d I n t e r f a c e 2 ( ) ; / / u p c a s t i n g h e r e
m e s s a g e . msg ( ) ;
}
}
Part (c): Which class is at the top of the exception class hierarchy?
The Throwable class is at the top of the Exception Hierarchy in Java. It has two subclasses, Error and Exception. Errors
are typically caused by external factors, such as hardware failure or network connectivity issues. Exceptions are typically
caused by programming errors, such as trying to access an element of an array that is out of bounds.
Page 1 of 15
2021: Object Oriented Programming WBSU
• Interfaces: Use interfaces when there is no inherent "is-a" relationship, and multiple unrelated classes need to
adhere to a common contract.
Part (e): Give an example of run-time operator in java and its use?
An example of a runtime operator in Java is the instanceof operator. The instanceof operator is used to test whether a
given object is an instance of a specified class or interface.
Example
c l a s s Animal { }
c l a s s Dog e x t e n d s A n i m a l {
void bark ( ) {
S y s t e m . o u t . p r i n t l n ( " Woof ! " ) ;
}
}
public c l a s s InstanceofExample {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
A n i m a l myAnimal = new Dog ( ) ; / / U p c a s t i n g Dog t o A n i m a l
i f ( myAnimal i n s t a n c e o f Dog ) {
Dog myDog = ( Dog ) myAnimal ; / / D o w n c a s t i n g A n i m a l t o Dog
myDog . b a r k ( ) ; / / S a f e t o c a l l b a r k method
}
}
}
Part (g): How to use JNI in Java? How to apply native code in Java?
Page 2 of 15
2021: Object Oriented Programming WBSU
Question 2:
Part (a): What is package? Show how different access protections are applied on packages.
2+3
Part (b): What is interface? Show accessing implementations through interface references.
2+3
Ans: (a) A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be catego-
rized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt,
javax, swing, net, io, util, sql etc.
Advantages:
1. Java package is used to categorize the classes and interfaces so that they can be easily maintained.
2. Java package provides access protection.
Example:
interface printable {
void p r in t ( ) ;
}
c l a s s A6 i m p l e m e n t s p r i n t a b l e {
p u b l i c void p r i n t ( ) { System . out . p r i n t l n ( " H e l l o " ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
A6 o b j = new A6 ( ) ;
obj . print ( ) ;
}
}
——————————————————————————-> Start of 2nd part
interface Callback {
v o i d c a l l b a c k ( i n t param ) ;
}
c l a s s C l i e n t implements Callback {
// Implement Callback ’ s i n t e r f a c e
public void c a l l b a c k ( i n t p ) {
System . out . p r i n t l n ( " c a l l b a c k c a l l e d with " + p ) ;
}
Page 3 of 15
2021: Object Oriented Programming WBSU
void aa ( ) {
System . out . p r i n t l n ( " h i . " ) ;
}
}
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
C a l l b a c k c = new C l i e n t ( ) ;
c . callback (42);
}
}
The output of this program is shown here:
c a l l b a c k c a l l e d with 42
The variable c is declared to be of the interface type Callback, yet it was assigned an instance of Client. Although c can be
used to access the callback() method, it cannot access any other members of the Client class.
An interface reference variable has knowledge only of the methods declared by its interface declaration. Thus, c could
not be used to access aa() since it is defined by Client but not Callback.
Question 3:
Part (a): What is multithreading? What is its need in java? how would you add threads to a
class that already inherits from a class other than the thread class?(Explain with an Example)
2+2+6
Ans: Multithreading in Java is a process of executing multiple threads simultaneously. A thread is a lightweight sub-
process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.
However, we use multithreading than multiprocessing because threads use a shared memory area. They don’t allocate
separate memory area so saves memory, and context-switching between the threads takes less time than process. Java
Multithreading is mostly used in games, animation, etc.
Advantages/Needs:
• It doesn’t block the user because threads are independent and you can perform multiple operations at the same
time.
• You can perform many operations together, so it saves time.
• Threads are independent, so it doesn’t affect other threads if an exception occurs in a single thread.
If a class already extends another class, you can add threading by implementing the Runnable interface.
class BaseClass {
void baseMethod ( ) {
S y s t e m . o u t . p r i n t l n ( " B a s e c l a s s method " ) ;
}
}
c l a s s MyClass e x t e n d s B a s e C l a s s i m p l e m e n t s R u n n a b l e {
@Override
p u b l i c v o i d run ( ) {
f o r ( i n t i = 1 ; i <= 5 ; i + + ) {
System . out . p r i n t l n ( " Thread running : " + i ) ;
try {
Thread . s l e e p ( 1 0 0 0 ) ; // S l e e p f o r 1 second
} catch ( InterruptedException e ) {
System . out . p r i n t l n ( " Thread i n t e r r u p t e d " ) ;
Page 4 of 15
2021: Object Oriented Programming WBSU
}
}
}
v o i d myMethod ( ) {
S y s t e m . o u t . p r i n t l n ( " MyClass method " ) ;
}
}
p u b l i c c l a s s ThreadExample {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
MyClass m y O b j e c t = new MyClass ( ) ;
T h r e a d t h r e a d = new T h r e a d ( m y O b j e c t ) ;
t h r e a d . s t a r t ( ) ; / / S t a r t t h e new t h r e a d
myObject . baseMethod ( ) ;
m y O b j e c t . myMethod ( ) ;
}
}
Explanation
1. Extend BaseClass: MyClass extends BaseClass.
2. Implement Runnable: MyClass implements Runnable and overrides run().
3. Create and Start Thread: Create a Thread with MyClass instance and call start().
Question 4:
Part (a): Explain the life cycle of applet in brief?(example) 5.
Part (b): write a program in java to take text from keyboard and write to a .txt file. 5.
Ans:(a) In Java, an applet is a special type of program embedded in the web page to generate dynamic content. Applet is
a class in Java.
The applet life cycle can be defined as the process of how the object is created, started, stopped, and destroyed during
the entire execution of its application. It basically has five core methods namely init(), start(), stop(), paint() and de-
stroy().These methods are invoked by the browser to execute.
Along with the browser, the applet also works on the client side, thus having less processing time.
Page 5 of 15
2021: Object Oriented Programming WBSU
1. init(): The init() method is the first method to run that initializes the applet. It can be invoked only once at the time
of initialization. The web browser creates the initialized objects, i.e., the web browser (after checking the security
settings) runs the init() method within the applet.
2. start(): The start() method contains the actual code of the applet and starts the applet. It is invoked immediately
after the init() method is invoked. Every time the browser is loaded or refreshed, the start() method is invoked. It
is also invoked whenever the applet is maximized, restored, or moving from one tab to another in the browser. It
is in an inactive state until the init() method is invoked.
3. stop(): The stop() method stops the execution of the applet. The stop () method is invoked whenever the applet is
stopped, minimized, or moving from one tab to another in the browser, the stop() method is invoked. When we go
back to that page, the start() method is invoked again.
4. destroy(): The destroy() method destroys the applet after its work is done. It is invoked when the applet window is
closed or when the tab containing the webpage is closed. It removes the applet object from memory and is executed
only once. We cannot start the applet once it is destroyed.
5. paint(): The paint() method belongs to the Graphics class in Java. It is used to draw shapes like circle, square, trapez-
ium, etc., in the applet. It is executed after the start() method and when the browser or applet windows are resized.
public c l a s s WriteToFile {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S c a n n e r s c a n n e r = new S c a n n e r ( S y s t e m . i n ) ;
System . out . p r i n t l n ( " Enter t e x t to w r i t e to f i l e : " ) ;
String text = scanner . nextLine ( ) ;
try {
F i l e W r i t e r w r i t e r = new F i l e W r i t e r ( " o u t p u t . t x t " ) ;
writer . write ( text ) ;
writer . close ( ) ;
System . out . p r i n t l n ( " Text w r i t t e n to f i l e s u c c e s s f u l l y ! " ) ;
} catch ( IOException e ) {
S y s t e m . o u t . p r i n t l n ( " An e r r o r o c c u r r e d w h i l e w r i t i n g t o f i l e . " ) ;
e . printStackTrace ( ) ;
}
}
}
Output:
Page 6 of 15
2021: Object Oriented Programming WBSU
Figure 2: WriteToFile.java
Question 5:
Part (a): Create a try block that is likely to generate three types of exception and then incor-
porate necessary catch blocks to catch and handle them appropriately. 5
Part (b): What are various mouse events and their corresponding listener interface? Discuss
with example. 5
Ans:(a)
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try {
int a = 0;
int b = 10;
int c = b/a ;
}
catch ( ArithmeticException e1 ) {
System . out . p r i n t l n ( " A r i t h m e t i c Exception occured "+ e 1 ) ;
}
c a t c h ( ArrayIndexOutOfBoundsException e2 ) {
System . out . p r i n t l n ( " ArrayIndexOutOfBounds Exception occured "+ e2 ) ;
}
catch ( Exception e3 ) {
System . out . p r i n t l n ( e3 ) ;
}
}
}
Explanation:
In Java, try block can be followed by multiple catch blocks. Creating our own Exception is known as Custom Exception
or User-defined Exception. Java allows custom exceptions to customize the exception according to user’s need.
Ans: (b) In Java, mouse events are part of the AWT (Abstract Window Toolkit) and are used to handle mouse inter-
actions in graphical user interfaces. The MouseEvent class defines the various types of mouse events, and there are two
main listener interfaces to handle these events: MouseListener and MouseMotionListener.
• mouseClicked(MouseEvent e)
• mousePressed(MouseEvent e)
• mouseReleased(MouseEvent e)
• mouseEntered(MouseEvent e)
Page 7 of 15
2021: Object Oriented Programming WBSU
• mouseExited(MouseEvent e)
• mouseMoved(MouseEvent e)
• mouseDragged(MouseEvent e)
Example:
i m p o r t j a v a . awt . ∗ ;
i m p o r t j a v a . awt . e v e n t . ∗ ;
import j a v a x . swing . ∗ ;
p u b l i c c l a s s MouseEve ntExampl e e x t e n d s
JFrame implements MouseListener , MouseMotionListener {
private JLabel label ;
p u b l i c MouseEve ntExample ( ) {
s e t T i t l e ( " Mouse E v e n t Example " ) ;
setSize (400 , 300);
s e t D e f a u l t C l o s e O p e r a t i o n ( J F r a m e . EXIT_ON_CLOSE ) ;
s e t L a y o u t ( new F l o w L a y o u t ( ) ) ;
/ / Add M o u s e L i s t e n e r and M o u s e M o t i o n L i s t e n e r t o t h e f r a m e
addMouseListener ( t h i s ) ;
addMouseMotionListener ( t h i s ) ;
}
@Override
p u b l i c v o i d m o u s e C l i c k e d ( MouseEvent e ) {
l a b e l . s e t T e x t ( " Mouse C l i c k e d a t ( " + e . g e t X ( ) + " , " + e . g e t Y ( ) + " ) " ) ;
}
@Override
p u b l i c v o i d m o u s e P r e s s e d ( MouseEvent e ) {
l a b e l . s e t T e x t ( " Mouse P r e s s e d a t ( " + e . g e t X ( ) + " , " + e . g e t Y ( ) + " ) " ) ;
}
@Override
p u b l i c v o i d m o u s e R e l e a s e d ( MouseEvent e ) {
l a b e l . s e t T e x t ( " Mouse R e l e a s e d a t ( " + e . g e t X ( ) + " , " + e . g e t Y ( ) + " ) " ) ;
}
@Override
p u b l i c v o i d m o u s e E n t e r e d ( MouseEvent e ) {
l a b e l . s e t T e x t ( " Mouse E n t e r e d t h e window " ) ;
}
@Override
p u b l i c v o i d m o u s e E x i t e d ( MouseEvent e ) {
l a b e l . s e t T e x t ( " Mouse E x i t e d t h e window " ) ;
Page 8 of 15
2021: Object Oriented Programming WBSU
@Override
p u b l i c v o i d mouseMoved ( MouseEvent e ) {
l a b e l . s e t T e x t ( " Mouse Moved t o ( " + e . g e t X ( ) + " , " + e . g e t Y ( ) + " ) " ) ;
}
@Override
p u b l i c v o i d mouseDragged ( MouseEvent e ) {
l a b e l . s e t T e x t ( " Mouse D r a g g e d t o ( " + e . g e t X ( ) + " , " + e . g e t Y ( ) + " ) " ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S w i n g U t i l i t i e s . i n v o k e L a t e r ( ( ) −> {
Mouse EventExa mple e x a m p l e = new Mou seEventE xample ( ) ;
example . s e t V i s i b l e ( t r u e ) ;
});
}
}
Question 6:
Part (a): What is the function of layout manager? What is the use of AWT? 2+2
Part (b): Create Base classes patient (pat-name, age, sex) and IPD (ward no, bed no, charge
per day). Derive a class IPD-patient from these two base classes with no-of-days-admitted
attribute. 6
Write necessary member functions to.
i) Input n records
ii) Display all records
iii) Search a patient by patient name.
You may use package and interface for this purpose.
Ans: (a) A layout manager in Java is responsible for arranging the components within a container (like a JFrame, JPanel,
etc.) in a specific layout. The layout manager automatically positions and sizes the components according to the rules of
the particular layout being used. This helps in creating flexible and adaptable user interfaces that can adjust to different
screen sizes and resolutions.
Key Functions of a Layout Manager
• Component Positioning: Determines where each component should be placed within the container.
• Component Sizing: Calculates the size of each component based on the available space and the layout rules.
Page 9 of 15
2021: Object Oriented Programming WBSU
• Responsive Design: Adapts the layout when the container is resized, ensuring components are repositioned and
resized accordingly.
• Ease of Use: Simplifies the process of creating complex layouts without needing to manually calculate positions
and sizes.
AWT (Abstract Window Toolkit) is a part of the Java programming language and is used to create graphical user interfaces
(GUIs) for Java applications. Here are the primary uses of AWT:
• GUI Component Creation: AWT provides a set of pre-built components like buttons, labels, text fields, checkboxes,
and more, which can be used to construct user interfaces.
• Event Handling: AWT includes a robust event handling model that allows developers to manage user interactions
with GUI components, such as clicks, key presses, and other actions.
• Layout Management: It offers different layout managers to control the positioning and sizing of GUI components
within a window. Examples include FlowLayout, BorderLayout, and GridLayout.
• Graphics and Drawing: AWT provides classes for drawing shapes, text, and images directly on components. This
is useful for custom graphics and game development.
• Window Management: AWT includes classes for creating and managing windows and dialogs, enabling developers
to create complex and interactive applications.
• Cross-Platform Compatibility: Since AWT is part of Java, it allows developers to create applications that can run
on any platform that supports Java, ensuring wide compatibility and deployment flexibility.
Overall, AWT is essential for developing desktop applications with graphical user interfaces in Java, providing the neces-
sary tools and components to build and manage these interfaces effectively.
Ans:(b)
‘Patient‘ Class
public class Patient {
private S t r i n g patName ;
private i n t age ;
private String sex ;
p u b l i c P a t i e n t ( S t r i n g patName , i n t age , S t r i n g s e x ) {
t h i s . patName = patName ;
t h i s . age = age ;
t h i s . sex = sex ;
}
/ / G e t t e r s and S e t t e r s
p u b l i c S t r i n g getPatName ( ) {
r e t u r n patName ;
}
p u b l i c v o i d s e t P a t N a m e ( S t r i n g patName ) {
t h i s . patName = patName ;
}
public i n t getAge ( ) {
return age ;
}
Page 10 of 15
2021: Object Oriented Programming WBSU
/ / G e t t e r s and S e t t e r s
p u b l i c i n t getWardNo ( ) {
r e t u r n wardNo ;
}
p u b l i c v o i d setWardNo ( i n t wardNo ) {
t h i s . wardNo = wardNo ;
}
p u b l i c i n t getBedNo ( ) {
r e t u r n bedNo ;
}
p u b l i c v o i d s e t B e d N o ( i n t bedNo ) {
t h i s . bedNo = bedNo ;
}
p ub li c double getChargePerDay ( ) {
return chargePerDay ;
}
Page 11 of 15
2021: Object Oriented Programming WBSU
p r i v a t e i n t bedNo ;
p r i v a t e double chargePerDay ;
p r i v a t e i n t noOfDaysAdmitted ;
/ / G e t t e r s and S e t t e r s
p u b l i c i n t getWardNo ( ) {
r e t u r n wardNo ;
}
p u b l i c v o i d setWardNo ( i n t wardNo ) {
t h i s . wardNo = wardNo ;
}
p u b l i c i n t getBedNo ( ) {
r e t u r n bedNo ;
}
p u b l i c v o i d s e t B e d N o ( i n t bedNo ) {
t h i s . bedNo = bedNo ;
}
p ub li c double getChargePerDay ( ) {
return chargePerDay ;
}
p u b l i c i n t getNoOfDaysAdmitted ( ) {
r e t u r n noOfDaysAdmitted ;
}
// Display d e t a i l s
public void d i s p l a y ( ) {
System . out . p r i n t l n ( " P a t i e n t Name : " + g e t P a t N a m e ( ) ) ;
System . out . p r i n t l n ( " Age : " + g e t A g e ( ) ) ;
System . out . p r i n t l n ( " Sex : " + g e t S e x ( ) ) ;
System . out . p r i n t l n ( " Ward No : " + wardNo ) ;
System . out . p r i n t l n ( " Bed No : " + bedNo ) ;
System . out . p r i n t l n ( " C h a r g e P e r Day : " + c h a r g e P e r D a y ) ;
System . out . p r i n t l n ( " Number o f Days A d m i t t e d : " + n o O f D a y s A d m i t t e d ) ;
Page 12 of 15
2021: Object Oriented Programming WBSU
System . out . p r i n t l n ( ) ;
}
}
Main class for I/P, O/P, and Serach
import j a v a . u t i l . A r r a y L i s t ;
import j a v a . u t i l . Scanner ;
p u b l i c c l a s s HospitalManagement {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S c a n n e r s c a n n e r = new S c a n n e r ( S y s t e m . i n ) ;
A r r a y L i s t < I P D P a t i e n t > p a t i e n t s = new A r r a y L i s t < > ( ) ;
// Input n records
S y s t e m . o u t . p r i n t ( " E n t e r number o f p a t i e n t s t o i n p u t : ");
int n = scanner . nextInt ( ) ;
s c a n n e r . n e x t L i n e ( ) ; / / consume t h e n e w l i n e
// Display a l l records
System . out . p r i n t l n ( " \ n D i s p l a y i n g a l l p a t i e n t r e c o r d s : " ) ;
for ( IPDPatient patient : patients ) {
Page 13 of 15
2021: Object Oriented Programming WBSU
patient . display ( ) ;
}
/ / S e a r c h a p a t i e n t by p a t i e n t name
S y s t e m . o u t . p r i n t ( " E n t e r p a t i e n t name t o s e a r c h : ");
S t r i n g s ea rc hN am e = s c a n n e r . n e x t L i n e ( ) ;
boolean found = f a l s e ;
if ( ! found ) {
System . out . p r i n t l n ( " P a t i e n t not found . " ) ;
}
scanner . close ( ) ;
}
}
Output
C : \ U s e r s \ YourUsername \ J a v a P r o j e c t s > j a v a c ∗ . j a v a
C : \ U s e r s \ YourUsername \ J a v a P r o j e c t s > j a v a H o s p i t a l M a n a g e m e n t
E n t e r number o f p a t i e n t s t o i n p u t : 2
Enter d e t a i l s for p a t i e n t 1 :
P a t i e n t Name : J o h n Doe
Age : 30
S e x : Male
Ward No : 5
Bed No : 1 2
C h a r g e P e r Day : 1 5 0 0 . 5 0
Number o f Days A d m i t t e d : 4
Enter d e t a i l s for p a t i e n t 2 :
P a t i e n t Name : J a n e S m i t h
Age : 2 5
Sex : Female
Ward No : 6
Bed No : 1 8
C h a r g e P e r Day : 1 2 0 0 . 7 5
Number o f Days A d m i t t e d : 3
Page 14 of 15
2021: Object Oriented Programming WBSU
C h a r g e P e r Day : 1 5 0 0 . 5
Number o f Days A d m i t t e d : 4
P a t i e n t Name : J a n e S m i t h
Age : 2 5
Sex : Female
Ward No : 6
Bed No : 1 8
C h a r g e P e r Day : 1 2 0 0 . 7 5
Number o f Days A d m i t t e d : 3
E n t e r p a t i e n t name t o s e a r c h : J o h n Doe
P a t i e n t found :
P a t i e n t Name : J o h n Doe
Age : 30
S e x : Male
Ward No : 5
Bed No : 1 2
C h a r g e P e r Day : 1 5 0 0 . 5
Number o f Days A d m i t t e d : 4
Page 15 of 15