Friday 10 May 2013

CS2305 PROGRAMMING PARADIGM 16MARKS WITH ANSWER



CS2305 - Programming Paradigm 16MARKS

UNIT I

1. Explain OOP Principles.
OOP is defined as object oriented programming.
Basic Concepts of OOP
1) Class
2) Object
3) Method
4) Inheritance
5) Data Abstraction
6) Data Encapsulation
7) Polymorphism
8) Message Passing
Defining the Class:
A class is defined by the user is data type with the template that helps in defining the properties. Once the class type has been defined we can create the variables of that type using declarations that are similar to the basic type declarations. In java instances of the classes which are actual objects
Eg:
classclassname [extends superclassname]
{
[fields declarations;]
[methods declaration;]
}
Field Declaration
Data is encapsulated in a class by placing data fields inside the body of the class definition. These variables are called as instance variables.
Class Rectangle
{
int length;
int width;
}
Method Declaration
A Class with only data fields has no life, we must add methods for manipulating the data contained in the class. Methods are declared inside the class immediate after the instance variables declaration.
Eg:
class Rectangle
{
int length; //instance variables
int width;
Void ge
tData(int x, int y) // Method Declartion
{
Length =x;
Width = y;
}
}
Creating the objects:
An object in java essentially a block of memory, which contains space to store all the instance variables. Creating an object is also referred as instantiating an object. Object in java are created using the new operator.
Eg:
Rectangle rec1;
// Declare the object
Rec1 = new Rectangle //instantiate the object
The above statements can also be combined as follows
Rectangle rec1 = new Rectangle;
Methods:
Methods are similar to functions or procedures that are available in other programming languages.
Difference B/w methods and functions
Difference b/w method and function is method declared inside class, function can be declared anywhere inside are outside class
Writing methods in java
if we had to repeatedly output a header such as:

System.out.println("GKMCET");
System.out.println ("Allapakkam");
System.out.println ("Meppedu Road");
We could put it all in a method like this:
public
static void printHeader()
{
System.out.println("GKMCET");
System.out.println("Allapakkam");
System.out.println("Meppedu Road");
}
Inheritance:
Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class, the new class that is formed is called derived class.
Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size(reusability) of the program, which is an important concept in object oriented programming.
Data Abstraction:
Data abstraction increases the power of programming language by creating user defined data types. Data abstraction also represents the needed information
in the program without presenting the details.
Data Encapsulation:
Data encapsulation combines data and functions into a single unit called class. When using Data Encapsulation, data is not accessed directly; it is only accessible through the functions present inside the class. Data encapsulation enables the important concept of data hiding possible.
Polymorphism:
Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. The ability of objects to respond differently to the same message or function call.
Message passing:
A message passing system provides primitives for sending and receiving messages using objects. These primitives may by either synchronous or asynchronous or both.


2.Explain Constructors with examples.
Constructor:
Constructors are the method. It is used to give initial values to instance variables of the objects. It is automatically called at the time of object creation.
Syntax :
Constructor name(arguments)
{
Statements;
Initial
values;
}
Rules:
A constructor has the same name as the class.
A class can have more than one constructor.
A constructor can take zero ,one ,or more parameters.
A constructor has no return value.
A construc
tor is always called with the new operator.
Example program:
Class complex:
{
Float rp;
Float ip;
Complex(float x,float y) //Defining Constructor
{
Rp=x;
Ip=y;
}
Void print()
{
System.out.println(“Real part”=rp);
System.out.println(“Imaginary part”+ip);
}
Void sum(complex a,complex b)
{
Rp=a.rp+b.rp;
Ip=a.ip=b.ip;
}
}
Class demo
{
Public static void main(str
ing args[]);
{
Complex a,b,c;
a=new complex(5,6);
//Calling Constructor
b=new complex(8,9);
c=new complex();
c.sum(a,b);
c.print();
}
}

3. Explain Packages in detail.

Packages
Packages are java’s way of grouping a variety of classes and/or interfaces together. Packages are container for the classes.
It is the header file in c++.
It is stored in a hierarichical manner.
If we want to use the packages in a class, we want to import it.
The two types of packages are:
1. System package.
2. User defined package.

Uses of Packages:
Packages reduce the complexity of the software because a large number of classes can be grouped into a limited number of packages.

We can create classes with same name in different packages.
Using packages we can hide classes.
We may like to use many of the classes contained in a package.it can be achieved by
Import packagename .classname
Or
Import packagename.*
Creating the
package
To create our own packages
packagefirstpackage;// package declaration
public class FirstClass //class definition
{.....
(body of class)
.......}
The file is saved as FirstClass.java,located at firstpackage directory. when it is
compiled .class file will be
created in same ditectory.
Access a package
The import statement can be used to search a list of packages for a particular class. The general form of import statement for searching class is a as follows.
Import package1 [.package2] [.package3].classname;
Using the package
package package1 ;
public class classA
{
public void displayA()
{
System.out.println(“class A”);
}
}
Adding a class to package
Define the class and make it public
Place the package statement
Package P1
Before the class definition as follows
Package p1;
Public class B
Package name
{
// body of B
}
Example:
Package college
Class student
{
intregno;
String name;
Student(intr,stringna);
{
Regno=r
Name=na
}
Public void print()
{
System.out.println(“Regno”+regno );
System.out.println(“Name”+name );
}
}
Package Course
Class engineering
{
Intregno;
String branch;
String year;
Engineering(int r, string br, string yr)
{
Regno=br;
Branch=br;
Year=yr;
}
public void print()
{
Systems.out.println(“Regno”+regno);
Systems.out.println(“
Branch”+branch);
Systems.out.println(“Year”+year);
}
}
import college.*;
import course.*;
Class demo
{
Public static void main(string args[])
{
Student sl=new Engineering(1,”CSE”,”III
Year”);
Sl.print();
El.print();
}
}
4. What is Array?.How to declare array?Discuss the methods under Array Class.
Arrays
An array is a data structure that stores a collection of values of the same type. You access each individual
value through an integer  index.
Array Name [index]
Integer constant, variable, or expression
For Instance we can define
an array name salary to represent a set of salaries of a group of employees. A
particular value is indicated by writing a number called index in brackets after the array name.
salary [10]
it represents the salary of the 10th  employee.
Types of arrays
One dimensional arrays
Two dimensional arrays
One Dimensional arrays
A list of items can be given one variable name using only one subscript and such a variable is called single - subscripted of one dimensional array. The subscript can also start from 0. ie x[0]. If we want to represent a set of five numbers, say (35,40,20,57,19) by an array variable number, then we have to create the variable number as follows
int number [ ] = new int [5 ];
The value to the array elements can be assigned as follows
Number
[0] =35;
Number [1] =40;
Number [2] =20;
Number [3] =57;
Number [4] =19;
This would cause the array number to store the values shown as follows;
Creating an array
Declaring the array
Creating memory locations
Putting values into the memory locations.
Declaring the Array
Array in java can be declared in two forms
Form 1
type arrayname [ ];
Form 2 type [ ] arrayname;
Creation of arrays
arrayname = new type [ size ];
Eg;
number = new int [5] ;
average = new float[10];
it is also
possible to combine declaration and creation.
int number [ ] = new int [5];
Initialization of arrays
The final step is to put values into the array created. This process is known as initialization using the array subscripts as shown below.
arrayname[subscript] = value ;
Eg
number[0] = 15;
we can also initialize by following way
typearrayname [ ] = { list of values }
Array Length
All array store the allocated size in an variable named length. We can obtain the length of array a using a.length
Eg:
intasize = a.length;
Sample Code for array manipulation
Two Dimensional arrary:
Usage :intmyArray [ ] [ ];
myArray= new int [3] [4];
or
initmyArray [ ] [ ] = new int [3][4]
This creates a table that can store 12 integer values, four across and three down.
Strings:
Series of characters represents a string, and easiest way to represent the string is array
Eg:
Char charArray [ ] = new char [2] ;
charArray[0] = ‘j’ ;
charArray[1] = ‘a’ ;
String can be declared and created in the following way
string stringname;
stringname = new string (“string”);
Operations on string
int m = stringname.length() //will return the length of the string
string city = “New” + “Delhi”// will return New Delhi ,string concatenating Sample Code for string Manipulation

5. What is javaDoc? Explain the comments for classes, methods, fields and link.
Overview of Javadoc
The basic structure of writing document comments is embed them inside
/** ... */
. The Javadoc is written
next to the items without any
separating newline. The class declaration usually contains:
/**
* @author FirstnameLastname<address @ example.com>
* @version 1.6 (The Java version used)
* @since 2010.0331
(E.g. ISO 8601 YYYY.MMDD)
*/
publicclass
Test
{
// class body
}
For methods there is
(1) a short, concise, one line description to explain what the itemdoes. This is followed by
[2] a longer description that may span in multiple paragraphs. In those the details can be explained in full.
This section, marked in brackets [], is optional.
(3) a tag section to list the accepted input arguments and return values of the method.
/**
*
Short one line description.
*
* Longer description. If there were any, it would be
* here.
*
* @param variable Description text texttext.
* @return Description text texttext.
*/
Variables are documented simi
larly to methods with the exception that part (3) is omitted. Here the variable
contains only the short description:
/**
* Description of the variable here.
*/
Private int debug =0;
6.Illustrate with examples: static and final.
Static Methods
The mean() method would work just as well if it wasn't declared static, as long as it was called from within the same class. If called from outside the class and it wasn't declared static, it would have to be qualified (uselessly) with an object. Even when used within the class, there are good reasons to define a method as static when it could be.
Documentation. Anyone seeing that a method is static will know how to call it. Similarly, any programmer looking at the code will know that a static
method can't interact with instance variables, which makes reading and debugging e
asier.
Efficiency. A compiler will usually produce slightly more efficient code because no implicit object parameter has to be passed to the meth Calling static methods
There are two cases.
Called from within the same class
Just write the static method name.
Eg,
// Called from inside the MyUtils class
doubleavgAtt = mean(attendance);

Called from outside the class
If a method (static or instance) is called from another class, something must be given before the method name to specify the class where the method is defined. For instance methods, this is the object that the method will access. For static methods, the class name should be specified.
Eg,
// Called from outside the MyUtils class.
doubleavgAtt = MyUtils.mean(attendance);
If an object is specified before it, the object value will be ignored and the the class of the object will be used.
Static Members
Let us assume that we want to define a member that is common to all the objects and can be accessed without using a particular object. That is the member belongs to the class as a whole rather than the objects created from the class.
It can be defined as follows
staticint count;
staticint max (int x, int y);
The members declared as static are known as static members.Static variable are used when we want to have a variable common to all instances of class.
Eg: Using static members
Final methods and variables
All method and variables can be overridden by default subclasses. If we wish to prevent the subclasses from overriding the members of the superclass, we can declare them as final using the keyword final as a modifier.
finalint SIZE = 100;
final void sh
owstatus()
Making a method final ensures that the functionality defined in this method will never be altered in anyway, similarly the value of a final variable can never be changed.
Finalizer methods
In java we know that Garbage collector will automatically free the memory resources used by objects. But objects may hold othernon-objectresources such as file descriptor or window system fonts. The Garbage collector cannot free those resources. To facilitate this java provides this finalizer method, similar to destructor in C++.The finalizer method issimply finalize() and can be added to any class.
Finalize method should explicitly specify the operation to be performed

7. Explain method overriding with example program.
A method is said to be overridden when one is in parent class and another is in child class with the same name, same return type, same parameter.
/ Method overriding.
class A {
inti, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j:
" + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k
this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
The output produced by this program is shown here:
k: 3

UNIT-II
1. Explain the concept of inheritance and its types.
2. Define an interface. Explain with example.
3. What is dynamic binding? Explain with example.
4. Explain the uses of reflection with examples.
5.With an example explain proxies
6. Abstract Classes

UNIT-III
1.Explain the classes under 2D shapes.
2.Explain event handling with examples.
3.Explain action event with an example.
4.What are the swing components? Explain.
5.Describe the AWT event hierarchy.
6.Explain the advantage and explain the methods in Model view controller
7.Give the methods available in graphics for COLOR and FONTS


Unit-IV
1.What is Generic Programming? Explain the use of Generic class and Generic methods.
2.What is Inheritance?
3.Explain about Wildcard and its types?
4.What is Reflection and how to use reflection using Generic class?
5.What is Exception? Explain its mechanism.
6.Explain about Assertion and Logging.

Unit-V

1.What is Multithreading?
2.Explain the Thread Concepts.
3.Explain the Synchronization Concept
4.What is Thread Safe Collection and Why it is used?
5.What is Event-Driven Programming and explain its usage.
 

No comments:

Post a Comment