8/12/2019 IGNOU MCS-024 Solved Assigment 2013-14 http://slidepdf.com/reader/full/ignou-mcs-024-solved-assigment-2013-14 1/27 www.ignousolvedassignments.com Course Code : MCS-024 Course Title: Object Oriented Technologies and Java Programming Assignment Number : MCA (2)/024/Assign/13 Assignment Marks : 100 Maximum Marks : 25% Last Dates for Submission : 15th October, 2013 (For July 2013 Session) 15th April, 2014 (For January 2014 Session) There are eight questions in this assignment which carries 80 marks. Rest of 20 marks are for viva- voce. Answer all the questions. Write and execute the program given in this assignment. Also in your programs give appropriate comments to increase understandability. Please go through the guidelines regarding assignments given in the Programme Guide for the format of presentation. Question 1 : a) What is Object Oriented Paradigm? Explain features of Object Oriented Paradigm. Why Object Oriented Programming are preferred over structured programming ? Solution : Oops is a process of solving problems in computers compared to the procedural language programming such as in C. Object-oriented programming is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance. Following are the fundamental features we get: Encapsulation Inheritance Re-usability Information Hiding Encapsulation: The way we make a logical boundary around behaviors (methods) and data (properties) they workon is called Encapsulation. Inheritance: Objects in OOP relate to each other in one way or another, the relationship in most of the case is parent/child relationship . The child objects inherent all the functionalities (methods) and data (properties) of their parents.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Course Title: Object Oriented Technologies and Java Programming
Assignment Number : MCA (2)/024/Assign/13
Assignment Marks : 100
Maximum Marks : 25%
Last Dates for Submission : 15th October, 2013 (For July 2013 Session)
15th April, 2014 (For January 2014 Session)
There are eight questions in this assignment which carries 80 marks. Rest of 20 marks are for viva-
voce. Answer all the questions. Write and execute the program given in this assignment. Also in
your programs give appropriate comments to increase understandability. Please go through the
guidelines regarding assignments given in the Programme Guide for the format of presentation.
Question 1 : a) What is Object Oriented Paradigm? Explain features of Object Oriented Paradigm.
Why Object Oriented Programming are preferred over structured programming ?
Solution :
Oops is a process of solving problems in computers compared to the procedural language
programming such as in C.
Object-oriented programming is a programming paradigm
that uses "objects" – data structures consisting of data fields and methods together with theirinteractions – to design applications and computer programs. Programming techniques may include
features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance.
Following are the fundamental features we get:
Encapsulation
Inheritance
Re-usability Information Hiding
Encapsulation:
The way we make a logical boundary around behaviors (methods) and data (properties) they work
on is called Encapsulation.
Inheritance:
Objects in OOP relate to each other in one way or another, the relationship in most of the case is
parent/child relationship. The child objects inherent all the functionalities (methods) and data
When the overriden method (getArea) is invoked for an object of the Cylinder class, the new
definition of the method is called and not the old definition from the superclass(Circle).
Example code
This is the code to instantiate the above two classes
Circle myCircle;
myCircle = new Circle(1.20);
Cylinder myCylinder;
myCylinder = new Cylinder(1.20,2.50);
b) What is abstract class? Explain advantages of abstract class with the help of an example.
Solution :
Java Abstract classes are used to declare common characteristics of subclasses. An
abstract class cannot be instantiated. It can only be used as a superclass for other
classes that extend the abstract class. Abstract classes are declared with the abstract
keyword. Abstract classes are used to provide a template or design for concrete
subclasses down the inheritance tree.
Like any other class, an abstract class can contain fields that describe the characteristics
and methods thatdescribe the actions that a class can perform. An abstract class can
include methods that contain no implementation. These are called abstract methods. The
abstract method declaration must then end with a semicolon rather than a block. If aclass has any abstract methods, whether declared or inherited, the entire class must be
declared abstract. Abstract methods are used to provide a template for the classes that
inherit the abstract methods.
Abstract classes cannot be instantiated; they must be subclassed, and actual
implementations must be provided for the abstract methods. Any implementation
specified can, of course, be overridden by additional subclasses. An object must have an
implementation for all of its methods. You need to create a subclass that provides an
public static void print() {System.out.println("point: " + x + "," + y);
}
public static void main(String args[]) {
Point p = new Point();
p.print();
}
}
Output
point: 0, 0
Notice that, in order to create a Point object, its class cannot be abstract. This means
that all of the abstractmethods of the Shape class must be implemented by the Point
class.
The subclass must define an implementation for every abstract method of the abstract
superclass, or the subclass itself will also be abstract. Similarly other shape objects can
be created using the generic Shape Abstract class.
A big Disadvantage of using abstract classes is not able to use multiple inheritance. In
the sense, when a class extends an abstract class, it can’t extend any other class.
Question 4 : a) What is polymorphism? Write a java program to show theadvantage of polymorphism
Solution :
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOPoccurs when a parent class reference is used to refer to a child class object.
Any java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all java objectsare polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference variable. A referencevariable can be of only one type. Once declared the type of a reference variable cannot be changed.
The reference variable can be reassigned to other objects provided that it is not declared final. The type of thereference variable would determine the methods that it can invoke on the object.
A reference variable can refer to any object of its declared type or any subtype of its declared type. A referencevariable can be declared as a class or interface type.
public class Animal{} public class Deer extends Animal implements Vegetarian{}
Now the Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for theabove example:
A Deer IS-A Animal
A Deer IS-A Vegetarian
A Deer IS-A Deer
A Deer IS-A Object
When we apply the reference variable facts to a Deer object reference, the following declarations are legal:
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
All the reference variables d,a,v,o refer to the same Deer object in the heap.
Virtual Methods:
In this section, I will show you how the behavior of overridden methods in Java allows you to take advantage ofpolymorphism when designing your classes.
We already have discussed method overriding, where a child class can override a method in its parent. Anoverridden method is essentially hidden in the parent class, and is not invoked unless the child class uses thesuper keyword within the overriding method.
/* File name : Employee.java */
public class Employee
{
private String name;
private String address;
private int number;
public Employee(String name, String address, int number)
{ System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public void mailCheck()
{
System.out.println("Mailing a check to " + this.name
Mailing check to Mohd Mohtashim with salary 3600.0
Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.0
Here we instantiate two Salary objects . one using a Salary reference s, and the other using an Employee referencee.
While invoking s.mailCheck() the compiler sees mailCheck() in the Salary class at compile time, and the JVM
invokes mailCheck() in the Salary class at run time.Invoking mailCheck() on e is quite different because e is an Employee reference. When the compilersees e.mailCheck(), the compiler sees the mailCheck() method in the Employee class.
Here, at compile time, the compiler used mailCheck() in Employee to validate this statement. At run time, however,the JVM invokes mailCheck() in the Salary class.
This behavior is referred to as virtual method invocation, and the methods are referred to as virtual methods. Allmethods in Java behave in this manner, whereby an overridden method is invoked at run time, no matter what datatype the reference is that was used in the source code at compile time.
b) What is package in Java ? Explain how to decide the need of package(s) in a system which is to
be developed using Java.
Solution :
Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating andusage of classes, interfaces, enumerations and annotations easier etc.
A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations )providing access protection and name space management.
Some of the existing packages in Java are::
java.lang - bundles the fundamental classes
java.io - classes for input , output functions are bundled in this package
Programmers can define their own packages to bundle group of classes/interfaces etc. It is a good practice togroup related classes implemented by you so that a programmers can easily determine that the classes, interfaces,enumerations, annotations are related.
Since the package creates a new namespace there won't be any name conflicts with names in other packages.Using packages, it is easier to provide access control and it is also easier to locate the related classes.
Creating a package: When creating a package, you should choose a name for the package and put a package statement with thatname at the top of every source file that contains the classes, interfaces, enumerations, and annotation types thatyou want to include in the package.The package statement should be the first line in the source file. There can be only one package statement in eachsource file, and it applies to all types in the file.
If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put intoan unnamed package.
Example: Let us look at an example that creates a package called animals. It is common practice to use lowercased namesof packages to avoid any conflicts with the names of classes, interfaces.Put an interface in the package animals:
/* File name : Animal.java */
package animals;
interface Animal {
public void eat(); public void travel();
}
Now put an implementation in the same package animals:
package animals;
/* File name : MammalInt.java */
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels"); }
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
Now you compile these two files and put them in a sub-directory called animals and try to run as follows:
Question 6 : a) What is multithreading ? Explain advantages of multithreaded programming with
the help of a Java program.
Solution :
A multithreaded program contains two or more parts that can run concurrently. Each part of such a
program is called a thread, and each thread defines a separate path of execution. A multithreading is a
specialized form of multitasking. Multitasking threads require less overhead than multitasking processes.
Life Cycle of a Thread:
New: A new thread begins its life cycle in the new state. It remains in this state until the programstarts the thread. It is also referred to as a born thread.Runnable: After a newly born thread is started, the thread becomes runnable. A thread in thisstate is considered to be executing its task.
Waiting: Sometimes a thread transitions to the waiting state while the thread waits for anotherthread to perform a task.A thread transitions back to the runnable state only when another thread
signals the waiting thread to continue executing.
Timed waiting: A runnable thread can enter the timed waiting state for a specified interval oftime. A thread in this state transitions back to the runnable state when that time interval expiresor when the event it is waiting for occurs.
Terminated: A runnable thread enters the terminated state when it completes its task orotherwise terminates
Question 7 : a) How a Java Applet is different from Java Application program? Create an Applet
program to display current date and time.
Solution :
All Java programs can be classified as Applications and Applets. The striking differencesare that applications contain main() method where as applets do not. One more is,
applications can be executed at DOS prompt and applets in a browser. We can say, an appletis an Internet application.
Applet is a Java program executed by a browser. The position of applets in software world is
they occupy the client-side position in Web communication. On the server-side, you guess,another Java program comes, Servlets. Applets on client-side and servlets on server-side
makes Java a truly "Internet-based language". To execute applets, the browsers come with JRE (Java Runtime Environment). The browsers with Java Runtime Environment (orto say, JVM) loaded are known as Java enabled browsers.
Question 8 : a) What is a socket ? Explain how a network socket is created usingJava.
Solution :
A socket is one end-point of a two-way communication link between two programs running on the
network. Socket classes are used to represent the connection between a client program and a serverprogram. The java.net package provides two classes--Socket and ServerSocket--that implement the
client side of the connection and the server side of the connection, respectively.
Normally, a server runs on a specific computer and has a socket that is bound to a specific port
number. The server just waits, listening to the socket for a client to make a connection request.
On the client-side: The client knows the hostname of the machine on which the server is running and
the port number on which the server is listening. To make a connection request, the client tries to
rendezvous with the server on the server's machine and port. The client also needs to identify itselfto the server so it binds to a local port number that it will use during this connection. This is usually
assigned by the system.
An endpoint is a combination of an IP address and a port number. Every TCP connection can be
uniquely identified by its two endpoints. That way you can have multiple connections between your
host and the server.
b) What is DNS? Explain components of DNS.
Solution :
The DNS translates Internet domain and host names to IP addresses. DNS automatically convertsthe names we type in our Web browser address bar to the IP addresses of Web servers hostingthose sites.
DNS implements a distributed database to store this name and address information for all public
hosts on the Internet. DNS assumes IP addresses do not change (are statically assigned rather
than dynamically assigned).
The DNS database resides on a hierarchy of special database servers. When clients like Web
browsers issue requests involving Internet host names, a piece of software called the DNS
resolver (usually built into the network operating system) first contacts a DNS server to determine
the server's IP address. If the DNS server does not contain the needed mapping, it will in turn
forward the request to a different DNS server at the next higher level in the hierarchy. After
potentially several forwarding and delegation messages are sent within the DNS hierarchy, the IP
The third component of the DNS is the "resolver". The resolver is a piece of software that's
implemented in the IP stack of every destination point, or "host" in IETF-speak. When a host is
configured, manually or through DHCP, it's assigned at least one default name server along with
its IP address and subnet mask. This name server is the first place that the host looks in order toresolve a domain name into an IP address. If the domain name is in the local zone, the default
name server can handle the request. Otherwise, the default name server queries one of the root
servers. The root server responds with a list of name servers that contain data for the TLD of the
query. This response is known as a referral. The name server now queries the TLD name server
and receives a list of name servers for the second-level domain name. The process repeats until
the local name server receives the address for the domain name. The local server then caches
the record and returns the address or other DNS data to the original querier.newdiv