OOPM May 2018 Solution
OOPM May 2018 Solution
(Model Solution – All programs can be written using other algorithm too. Given is
only one way of writing program)
Total Marks : 80
(3 Hours)
Circle(double radius)
{
this.radius = radius;
CalculateArea()
{
area = (22*r*r)/7;
}
CalculatePerimeter()
{
perimeter = (22*2*r)/7;
}
}
Static data members, methods, and constants reside with a class and not instances
of classes. They can be accessed from within the class defined or another class
using the dot operator.
Static data members have the same features as static methods, plus they are
stored in a single location in memory.
They are used when only one copy of a data member is needed across all
instances of a class (e.g., a counter).
// Declaring a static data member
return voterCount;
...
Static Methods
Analyzer.getVotesByAge();
return a+b;
}
return a+b+c;
return a+b;
return a+b+c;
System.out.println(sumOf(1,2));
System.out.println(sumOf(10d,20d,30d));
}
@Override
public void makeSound() {
}
Q. 2 a Explain different types of relationships among entities. [10]
Define the relationships among the objects of given sentences:
1) Customer has Account.
2) CurrentAccount, SavingsAccount is a kind of Account.
3) Customer makes payment
4M – Explaination
6M – Relationships
Inheritance
It is the mechanism in java by which one class is allow to inherit the
features(fields and methods) of another class.
Association is relation between two separate classes which establishes through
their Objects. Association can be one-to-one, one-to-many, many-to-one, many-
to-many.
In Aggregation, both the entries can survive individually which means ending
one entity will not effect the other entity
When there is a composition between two entities, the composed object cannot
exist without the other entity.
Type of relationship
1)Composition
2)Inheritance
3)Association
b What is a thread?Which are the two ways to create a thread? [10]
Write a program to show interleaving of actions from 2 thread: t1 and t2
synchronizing on a shared object.
t1 print message “ping” and t2 print message “pong”.
4M - Theory
6M – Program
Multithreading is a Java feature that allows concurrent execution of two or more
parts of a program for maximum utilization of CPU. Each part of such program
is called a thread. So, threads are light-weight processes within a process.
import java.io.*;
import java.util.*;
public class PingPong extends java.lang.Thread {
// constructor:
word = whatToSay;
delay = delayTime;
try {
while(true) {
ping.start();
pong.start();
2. Logically cohesive: A subsystem that performs the tasks that are logically
related with each other is called logically cohesive.
Coupling:
2. Control coupling: The modules share related control data in control coupling.
import java.util.Scanner;
while(true)
System.out.println("Next number");
Double val = in.nextDouble();
if(val == -999)
break
else
numbers.add(val);
if (numbers.size() == 0)
System.out.println("List is empty.");
}
else
second = first;
first = element;
second = element;
}
b What is checked and unchecked exception in Java? Explain the use of [5]
following in exception handling.
Try-Catch, Finally, Throw, Throws
1M + 4M
Checked: are the exceptions that are checked at compile time. If some code
within a method throws a checked exception, then the method must either handle
the exception or it must specify the exception using throws keyword.
Unchecked are the exceptions that are not checked at compiled time.
Try-Catch
The try block contains set of statements where an exception can occur. A try
block is always followed by a catch block,which handles the exception that
occurs in associated try block. A try block must be followed by catch blocks or
finally block or both.
A catch block is where you handle the exceptions, this block must follow the try
block. A single try block can have several catch blocks associated with it. You
can catch different exceptions in different catch blocks. When an exception
occurs in try block, the corresponding catch block that handles that particular
exception executes.
Finally
A finally block contains all the crucial statements that must be executed whether
exception occurs or not. The statements present in this block will always execute
regardless of whether exception occurs in try block or not such as closing a
connection, stream etc.
Throw
We can define our own set of conditions or rules and throw an exception
explicitly using throw keyword. For example, we can throw ArithmeticException
when we divide number by 5, or any other numbers, what we need to do is just
set the condition and throw any exception using throw keyword. Throw keyword
can also be used for throwing custom exceptions.
Throws
Suppose there are several methods that can cause exceptions, in that case it
would be tedious to write try-catch for each method. The code will become
unnecessary long and will be less-readable.
One way to overcome this problem is by using throws : declare the exceptions in
the method signature using throws and handle the exceptions where you are
calling this method by using try-catch.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
g.drawOval(250,250,300,300);
g.drawOval(310,320,30,30);
g.drawOval(440,320,30,30);
g.drawLine(390,350,390,390);
}
Q. 5 a Explain creation of user defined package with an example. [10]
5M – Theory
5M – Program
A package is a mechanism to group the similar type of classes, interfaces and
sub-packages and provide access control. It organizes classes into single unit.
Vehicle.java
package vehicles;
interface Vehicle
Car.java
package vehicles;
System.out.println("Car is running.");
Car.run();
Car.speed();
System.out.println("Hello World!");
this.length = length;
this.breadth = width;
//Overriding equals
return true;