Вы находитесь на странице: 1из 5

MCS-024 Object Oriented Technologies and Java Programming

__________________________________________________________________________
Course Code : MCS-024
Course Titlle : Object Oriented Technologies and Java
Programming
Assignment Number : MCA (1)/024/Assign/09
__________________________________________________________________________

Q1.(a)What is Object Orientation? Explain the advantage of object Orientation ?


Ans a):
In o-o (object-oriented) programming, code and data are merged into a single indivisible thing --
an object. Objects are individual instances of a class. For example, you may create an object call
Spot from class Dog. The Dog class defines what it is to be a Dog object, and all the "dog-
related" messages a Dog object can act upon. All object-oriented languages have some means,
usually called a factory, to "manufacture" object instances from a class definition.

Object-oriented programming takes the view that what we really care about are the objects we
want to manipulate rather than the logic required to manipulate them. Examples of objects range
from human beings (described by name, address, and so forth) to buildings and floors (whose
properties can be described and managed) down to the little widgets on your computer desktop
(such as buttons and scroll bars).

One of the principal advantages of object-oriented programming techniques over procedural


programming techniques is that they enable programmers to create modules that do not need to
be changed when a new type of object is added. A programmer can simply create a new object
that inherits many of its features from existing objects. This makes object-oriented programs
easier to modify.
Q2 b) What are different relational operators in Java? Write a Java program to explain the
use of relational operators ?
Ans :

Programme :

public class marks


{
static int stud1;

public marks(int a)
{
stud1=a;
}
public void grade()
{
if((stud1>70)&&(stud1<100))
System.out.println(“Grade A”);
else
if((stud1>50)&&(stud1<70))
System.out.println(“Grade B”);
else
System.out.println(“Fail”);
}

public static void main(String args[])


{
marks m1=new marks(80);
m1.grade();

}
}

The symbols && a n d || are the logical AND a n d OR operators used to evaluate logical
expressions. Use && and || in the evaluation of compound expressions of the form

· expression_1 && expression_2

where expression_1 and expression_2 each evaluate to a scalar, logical result.

The && and || operators support short-circuiting. This means that the second operand is valuated
only when the result is not fully determined by the first operand

Q :4)
a) What is Interface in Java ? Why abstract classes are used in inheritance? Write a
Program in Java to Explain interface and Multiple Inheritance in Java?

Ans : Methods form the object's interface with the outside world; the buttons on the front
of your television set, for example, are the interface between you and the electrical wiring
on the other side of its plastic casing. You press the "power" button to turn the television
on and off. In its most common form, an interface is a group of related methods with
empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:
interface Bicycle {
void changeCadence(int newValue);

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);


}
To implement this interface, the name of your class would change (to ACMEBicycle, for
example), and you'd use the implements keyword in the class declaration:
class ACMEBicycle implements Bicycle {

// remainder of this class implemented as before

we can create a class by extending from more than one class. It is problematic. So in Java this
feature is eliminated. Anyway for getting multiple inheritance, Java introduced the concept of
interfaces. So we can create a class by extending one class or we can implement any number of
interfaces for it. But the restriction is we have to implement / describe every method existing in
the interfaces

b) What is a Constructor ?Explain the need of constructors overloading in Java


Programme ?

Ans : The constructor method is called when a new instance of an object is created. Constructors
and methods differ in three aspects of the signature: modifiers, return type, and name. Like
methods, constructors can have any of the access modifiers: public, protected, private, or none
(often called package or friendly). Unlike methods, constructors can take only access modifiers.
Therefore, constructors cannot be abstract, final, native, static, or synchronized.

Overloading is a powerful feature, but you should use it only as needed. Use it when you actually
do need multiple methods with different parameters, but the methods do the same thing.

Q 5) a) What is an exception? Explain different types of exceptions in Java. Also explain


how an exception subclass are created, in Java.

Ans : Exceptions are the customary way in Java to indicate to a calling method that an abnormal
condition has occurred.When a method encounters an abnormal condition (an exception
condition) that it can't handle itself, it may throw an exception. Throwing an exception is like
throwing a beeping, flashing red ball to indicate there is a problem that can't be handled where it
occurred. Exceptions are caught by handlers positioned along the thread's method invocation
stack.

Exceptions are 2 types.

1.Checked

2.Uncheked

All the exception for that compiler doesn't care are comes under unchecked exception i.e.
ArithmaticException, ArrayIndexOutofBoundexp etc
Exceptions which needs to be defined in throws clause of a method r checked exception ,ie.
sevletException,ClassNotFoundExp,NoSuchFieldException etc

Q 6) What is Multithreading ?Explain different ways of implementation of multithreading in


java .

Ans : The ability of an operating system to execute different parts of a program, called threads,
simultaneously. The programmer must carefully design the program in such a way that all the
threads can run at the same time without interfering with each other.

We can implement the multithreading concept in java in two ways...


1..Extending the thread class..
2..Implementing the runnable interface

Creating Threads in Java


Runnable interface
*Create object implementing Runnable interface.
*Pass it to Thread object via Thread constructor.

Example
public class MyT implements Runnable
{
public void run()
{
… // work for thread
}
}
Thread t = new Thread(new MyT()); // create thread
t.start(); // begin running thread
… // thread executing in parallel

Alternative (Not Recommended)


Directly extend Thread class

Example
public class MyT extends Thread
{
public void run()
{
… // work for thread
}
}
MyT t = new MyT(); // create thread
t.start(); // begin running thread

Q7. b)What is Layout Manager ?Explain Different Layout managers available in java.

Ans: In programming of graphical user interfaces, an object that controls the size and position of
Java components within a container. When placing components into containers in a graphical
user interface, an object called a layout manager may be associated with the container and will
implement a policy for deciding how the components are positioned and sized within the
container.

FlowLayout

The FlowLayout manager is the simplest of the layout managers. In this scheme, components are
arranged in the order they are placed within the container. If a row becomes completely filled, the
remaining components are grouped together on the next row.

BorderLayout

The BorderLayout manager arranges components within specified regions of a container. Valid
regions are "North", "South", "East", "West", and "Center"

GridLayout

A container using the GridLayout scheme arranges components in rows and columns in row-
major order.