MCS-024 Object Oriented Technologies and Java
MCS-024 Object Oriented Technologies and Java
com
This assignment has twenty questions in all and carries 80 marks. The rest of the 20 marks are
for viva-voice. Answer all the questions. All questions carry equal marks (i.e. 4 marks each).
Please go through the guidelines regarding assignments given in the Programme Guide for the
format of presentation.
Disclaimer: This Assignment is prepared by our students. The Institution and publisher are not
responsible for any omission and errors.
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
Object
Any entity that has state and behavior is known as an object. For example: chair, pen,
table, keyboard, bike etc. It can be physical and logical.
Class
Collection of objects is called class. It is a logical entity.
Inheritance
When one object acquires all the properties and behaviours of parent object i.e.
known as inheritance. It provides code reusability. It is used to achieve runtime
polymorphism.
Polymorphism
When one task is performed by different ways i.e. known as polymorphism. For
example: to convince the customer differently, to draw something e.g. shape or rectangle
etc.
In java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For
example: phone call, we don't know the internal processing.
In java, we use abstract class and interface to achieve abstraction.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as
encapsulation. For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
JVM provides definitions for the:
o Memory area
o Class file format
o Register set
o Garbage-collected heap
o Fatal error reporting etc.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
o
Q.2.a) Write a java program to demonstrate use of different operators available in
java.
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
a = 0011 1100
b = 0000 1101
-----------------
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
C <<= 2
is same
<<= Left shift AND assignment operator.
as C = C
<< 2
C >>= 2
is same
>>= Right shift AND assignment operator.
as C = C
>> 2
C &= 2 is
&= Bitwise AND assignment operator. same as C
=C&2
Miscellaneous Operators
There are few other operators supported by Java Language.
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator. This operator
consists of three operands and is used to evaluate Boolean expressions. The
goal of the operator is to decide, which value should be assigned to the
variable. The operator is written as
variable x = (expression) ? value if true : value if false
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
1. Public
2. Private
3. Protected
4. Default
If you do not define any access specifier/modifier than java will take default access
specifier as the default for variables/methods/class.
<access-specifier><class-keyword><class-name>
For e.g.
If you declare any method/function as the public access specifier than that
variable/method can be used anywhere.
public Access Specifier Example :
package abc;
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
}
}
private access specifier :
If you declare any method/variable as private than it will only accessed in the same class
which declare that method/variable as private. The private members cannot access in
the outside world. If any class declared as private than that class will not be inherited. :
class AccessDemo
{
private int x = 56;
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
class AccessDemo
{
default int a = 4;
}
It has same properties as that of private access specifier but it can access the
methods/variables in the child class of parent class in which they are declared as
protected.
When we want to use the private members of parent class in the child class then we
declare those variables as protected.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
o Java applications are compiled using the javac command and run using
the java command.
2.Applets - Java programs that can run over the Internet. The standard client/server model
is used when the Applet is executed. The server stores the Java Applet, which is sent to
the client machine running the browser, where the Applet is then run.
Application is a Java class that has a main() method. An applet is a Java class which
extends java.applet.Applet. Generally, application is a stand-alone program, normally
launched from the command line, and which has unrestricted access to the host system.
An applet is a program which is run in the context of an applet viewer or web browser,
and which has strictly limited access to the host system. For instance, an applet can
normally not read or write files on the host system whereas an application normally can.
The actions of both applets and applications can be controlled by SecurityManager
objects.
Applets may communicate with other applets running on the same virtual machine. If the
applets are of the same class, they can communicate via shared static variables. If the
applets are of different classes, then each will need a reference to the same class with
static variables.
An array of objects is created just like an array of primitive type data items in the following
way.
Student[] studentArray = new Student[7];
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
For instance, the following statement throws a NullPointerException during runtime which
indicates that studentArray[0] isn't yet pointing to a Student object.
studentArray[0].marks = 100;
The Student objects have to be instantiated using the constructor of the Student class and
their references should be assigned to the array elements in the following way.
studentArray[0] = new Student();
In this way, we create the other Student objects also. If each of the Student objects have
to be created using a different constructor, we use a statement similar to the above several
times. However, in this particular case, we may use a for loop since all Student objects
are created with the same default constructor.
for ( int i=0; i<studentArray.length; i++) {
studentArray[i]=new Student();
}
The above for loop creates seven Student objects and assigns their reference to the array
elements. Now, a statement like the following would be valid.
studentArray[0].marks=100;
Enhanced for loops find a better application here as we not only get the Student object
but also we are capable of modifying it.
for ( Student x : studentArray ) {
x.marks = s.nextInt(); // s is a Scanner object
}
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable that have no value it
is called blank final variable or uninitialized final variable. It can be initialized in the
constructor only. The blank final variable can be static also which will be initialized in the
static block only. We will have detailed learning of these. Let's first learn the basics of final
keyword.
class Bike9{
void run(){
speedlimit=400;
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
obj.run();
3.(c) Write a java program to create Ticket class with proper constructor, to create a
railway ticket. Define a method to display ticket details.
A.3.(c)
public class Station {
private int stationID;
private String stationName;
// Getters and Setters (including methods to add new stations with fares and
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
calculateFare();
}
// Calculates fare based on Train and from and to Stations and taxes, etc.
private void calculateFare() {
this.fare = ...
}
}
// Assuming card payment only for online reservation system for simplicity.
// Design can be modified and enhanced suitably.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
For example, integers and floats are implicitly polymorphic since you can add, subtract,
multiply and so on, irrespective of the fact that the types are different. They're rarely
considered as objects in the usual term.
But, in that same way, a class like BigDecimal or Rational or Imaginary can also provide
those operations, even though they operate on different data types.
The classic example is the Shape class and all the classes that can inherit from it
(square, circle, dodecahedron, irregular polygon, splat and so on).
With polymorphism, each of these classes will have different underlying data. A point
shape needs only two co-ordinates (assuming it's in a two-dimensional space of course).
A circle needs a center and radius. A square or rectangle needs two co-ordinates for the
top left and bottom right corners and (possibly) a rotation. An irregular polygon needs a
series of lines.
Yes, It provides flexibility in application development. We can understand with the helpof an
Example:-
Java has excellent support of polymorphism in terms of Inheritance, method
overloading and method overriding. Method overriding allows Java to invoke
method based on a particular object at run-time instead of declared type while coding.
To get hold of concept let's see an example of polymorphism in Java:
With polymorphism, each of these classes will have different underlying data. A point
shape needs only two co-ordinates (assuming it's in a two-dimensional space of course).
A circle needs a center and radius. A square or rectangle needs two co-ordinates for the
top left and bottom right corners and (possibly) a rotation. An irregular polygon needs a
series of lines.
By making the class responsible for its code as well as its data, you can achieve
polymorphism. In this example, every class would have its own Draw() function and the
client code could simply do:
shape.Draw()
Q.4.(b) Explain the need of package in Java. Explain accessibility rules for package.
Also explain how members of a package are imported. Write java program to create
your own package for finding area of different shapes.
A.4.(b) Package:-
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
class Rectangle
{
double length;
double breadth;
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
this.length = length;
this.breadth = breadth;
}
double getArea()
{
return length * breadth;
}
}
class Square
{
double side;
Square(double side)
{
this.side = side;
}
double getArea()
{
return side * side;
}
}
class Circle
{
double radius;
Circle(double radius)
{
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
this.radius = radius;
}
double getArea()
{
return (22.0/7.0) * radius * radius;
}
}
OUTPUT
Rectangle Area : 40.0
Square Area : 49.0
Circle Area : 38.5
Q.5.(a) What is abstract class? Explain need of abstract class with the help of an
example.
A.5.(a) A class that is declared with abstract keyword, is known as abstract class in java.
It can have abstract and non-abstract methods (method with body).
Before learning java abstract class, let's understand the abstraction in java first.
Another way, it shows only important things to the user and hides the internal details for
example sending sms, you just type the text and send the message. You don't know the
internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
2. Interface (100%)
Abstract class in Java
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
Abstract method
A method that is declared as abstract and does not have implementation is known as
abstract method.
In this example, Bike the abstract class that contains only one abstract method run. It
implementation is provided by the Honda class.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
running safely..
A.5.(b)
An exception (or exceptional event) is a problem that arises during the execution of a program. When
an Exception occurs the normal flow of the program is disrupted and the program/Application
terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.
An exception can occur for many different reasons. Following are some scenarios where
an exception occurs.
A user has entered an invalid data.
A file that needs to be opened cannot be found.
A network connection has been lost in the middle of communications or the JVM
has run out of memory.
Some of these exceptions are caused by user error, others by programmer error,
and others by physical resources that have failed in some manner.
Based on these, we have three categories of Exceptions. You need to understand them
to know how exception handling works in Java.
Checked exceptions A checked exception is an exception that occurs at the
compile time, these are also called as compile time exceptions. These exceptions cannot
simply be ignored at the time of compilation, the programmer should take care of (handle)
these exceptions.
For example, if you use FileReader class in your program to read data from a file, if the
file specified in its constructor doesn't exist, then a FileNotFoundException occurs, and
the compiler prompts the programmer to handle the exception.
The runtime system searches the call stack for a method that contains a block of code that
can handle the exception. This block of code is called an exception handler.
import java.util.Scanner;
class Billing{
String name;
int Price,total,Quantity,d;
public void input(){
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
void process()
{
try{
total=Price*Quantity;
total=total/d;
output();
}
catch(ArithmeticException e){
System.out.println("Arithmetic Exception");
}
}
void output(){
System.out.println("Your medicine "+name);
System.out.println("Price "+Price);
System.out.println("Quantity "+Quantity);
System.out.println("Discount "+d+"%");
System.out.println("total ammount "+total);
}
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
obj.input();
obj.process();
}
}
Q.6.(a) What is I/O stream in java? Write a program in java to create a file and count
the number of words in it.
A.6.(a)
The java.io package contains nearly every class you might ever need to perform input
and output (I/O) in Java. All these streams represent an input source and an output
destination. The stream in the java.io package supports many data such as primitives,
object, localized characters, etc.
Stream
A stream can be defined as a sequence of data. There are two kinds of Streams
Java provides strong but flexible support for I/O related to files and networks but this
tutorial covers very basic functionality related to streams and I/O. We will see the most
commonly used examples one by one
Byte Streams
Java byte streams are used to perform input and output of 8-bit bytes. Though there are
many classes related to byte streams but the most frequently used classes
are, FileInputStream and FileOutputStream.
Character Streams
Java Byte streams are used to perform input and output of 8-bit bytes, whereas
Java Character streams are used to perform input and output for 16-bit unicode. Though
there are many classes related to character streams but the most frequently used classes
are, FileReader and FileWriter. Though internally FileReader uses FileInputStream and
FileWriter uses FileOutputStream but here the major difference is that FileReader reads
two bytes at a time and FileWriter writes two bytes at a time.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
program in java to create a file and count the number of words in it.
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;
class NumberWords {
int a = 0;
int b = 0;
b++;
System.out.println(str);
while (st.hasMoreTokens()) {
String s = st.nextToken();
a++;
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
OUTPUT:
Totally 6 lines
A.6(b)
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.IOException.*;
// set the size of the applet to the size of the background image.
// Resizing the applet may cause distortion of the image.
setSize(300, 300);
// Set the image name to the background you want. Assumes the image
// is in the same directory as the class file is
backGround = getImage(getCodeBase(), "save.GIF");
BackGroundPanel bgp = new BackGroundPanel();
bgp.setLayout(new FlowLayout());
bgp.setBackGroundImage(backGround);
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
bgp.add(new TextField("Only photos entered in the correct session timeframe for the unit
will be accepted"));
bgp.add(new TextField("Only photos entered through the online entry form will be
accepted in the competition.");
bgp.add(new TextField("Entries must be taken during your PACE activity in one of the
sessions from March 2016 - March 2017.");
BackGroundPanel() {
super();
}
// get the size of this panel (which is the size of the applet),
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
Constructor in Java
Java constructor is invoked at the time of object creation. It constructs the values i.e.
provides data for the object that is why it is known as constructor.Constructor in java is
a special type of method that is used to initialize the object.
In java, Strings can be created like this: Assigning a String literal to a String instance:
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
1. String()
This initializes a newly created String object so that it represents an empty character
sequence.
2
String(byte[] bytes)
This constructs a new String by decoding the specified array of bytes using the platform's
default char set.
3
String(byte[] bytes, Charset charset)
This constructs a new String by decoding the specified array of bytes using the specified
charset.
4
String(byte[] bytes, int offset, int length)
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
This constructs a new String by decoding the specified subarray of bytes using the
platform's default charset
5
String(byte[] bytes, int offset, int length, Charset charset)
This constructs a new String by decoding the specified subarray of bytes using the
specified charset.
6
String(byte[] bytes, int offset, int length, String charsetName)
This constructs a new String by decoding the specified subarray of bytes using the
specified charset.
7
String(byte[] bytes, String charsetName)
This constructs a new String by decoding the specified array of bytes using the specified
charset.
8
String(char[] value)
This allocates a new String so that it represents the sequence of characters currently
contained in the character array argument.
9
String(char[] value, int offset, int count)
This allocates a new String that contains characters from a subarray of the character array
argument.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
10
String(int[] codePoints, int offset, int count)
This allocates a new String that contains characters from a subarray of the Unicode code
point array argument.
11
String(String original)
This initializes a newly created String object so that it represents the same sequence of
characters as the argument; in other words, the newly created string is a copy of the
argument string.
12
String(StringBuffer buffer)
This allocates a new string that contains the sequence of characters currently contained
in the string buffer argument.
13
String(StringBuilder builder)
Q.7.(a) What is layout manager? Explain different layouts available in java for GUI
programming. What is default layout of an Applet? Explain how to set the layout of an
applet.
A.7.(a)
The LayoutManagers are used to arrange components in a particular manner.
LayoutManager is an interface that is implemented by all the classes of layout managers.
There are following classes that represents the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.
BorderLayout
A BorderLayout places one component in the center of a
container. The central component is surrounded by up to four
other components that border it to the "North", "South", "East",
and "West", as shown in the diagram at the right. Each of the
four bordering components is optional. The layout manager
first allocates space to the bordering components. Any space
that is left over goes to the center component.
Flow Layout
A FlowLayout simply lines up its components without trying to be particularly neat about
it. After laying out as many items as will fit in a row across the container, it will move on to
the next row. The components in a given row can be either left-aligned, right-aligned, or
centered, and there can be horizontal and vertical gaps. If the default constructor, "new
FlowLayout()" is used, then the components on each row will be centered and the
horizontal and vertical gaps will be five pixels. The constructor
FlowLayout(int align, int hgap, int vgap)
Grid Layout
A GridLayout lays out components in a grid of equal sized rectangles. The illustration
shows how the components would be arranged in a grid layout with 3 rows and 2 columns.
If a container uses a GridLayout, the appropriate add method takes a single parameter of
type Component (for example: add(myButton)). Components are added to the grid in the
order shown; that is, each row is filled from left to right before going on the next row.
The constructor for a GridLayout with R rows and C columns takes the
form GridLayout(R,C). If you want to leave horizontal gaps of H pixels between columns
and vertical gaps of V pixels between rows, use GridLayout(R,C,H,V) instead.
GridBagLayout
A GridBagLayout is similar to a GridLayout in that the container is broken down into rows
and columns of rectangles. However, a GridBagLayout is much more sophisticated
because the rows do not all have to be of the same height, the columns do not all have to
be of the same width, and a component in the container can spread over several rows and
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
CardLayout
CardLayouts differ from other layout managers in that in a container that uses
a CardLayout, only one of its components is visible at any given time. Think of the
components as a set of "cards". Only one card is visible at a time, but you can flip from
one card to another. Methods are provided in the CardLayout class for flipping to the first
card, to the last card, and to the next card in the deck. A name can be specified for each
card as it is added to the container, and there is a method in the CardLayout class for
flipping directly to the card with a specified name. (The container object has to be passed
as a parameter to each of these methods.)
import java.awt.*;
import java.applet.*;
public class LayoutExample extends Applet
{
Button okButton1;
Button okButton2;
Button okButton3;
Button okButton4;
Button okButton5;
public void
init()
{
// sets the LayoutManager to BorderLayout
setLayout(new BorderLayout());
okButton1 = new Button("Centered Button");
okButton2 = new Button("Cold North");
okButton3 = new Button("Go West");
okButton4 = new Button("At East");
okButton5 = new Button("Hot South");
// always says where the component should be placed when adding
// Options are center,East,West,Nort and South
add(okButton1,"Center");
add(okButton2,"North");
add(okButton3,"West");
add(okButton4,"East");
add(okButton5,"South");
}
}
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
If your computer has only a single CPU, you might be wondering how it can execute more
than one thread at the same time. In single-processor systems, only a single thread of
execution occurs at a given instant. The CPU quickly switches back and forth between
several threads to create the illusion that the threads are executing at the same time.
Single-processor systems support logical concurrency, not physical concurrency. Logical
concurrency is the characteristic exhibited when multiple threads execute with separate,
independent flows of control. On multiprocessor systems, several threads do, in fact,
execute at the same time, and physical concurrency is achieved. The important feature of
multithreaded programs is that they support logical concurrency, not whether physical
concurrency is actually achieved.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
The JDBC library includes APIs for each of the tasks mentioned below that are commonly
associated with database usage.
Making a connection to a database.
Creating SQL or MySQL statements.
Executing SQL or MySQL queries in the database.
Viewing & Modifying the resulting records.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
The forName() method of Class class is used to register the driver class. This method is
used to dynamically load the driver class.
Class.forName("oracle.jdbc.driver.OracleDriver");
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
Statement stmt=con.createStatement();
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
By closing connection object statement and ResultSet will be closed automatically. The
close() method of Connection interface is used to close the connection.
con.close();
A server application normally listens to a specific port waiting for connection requests from
a client. When a connection request arrives, the client and the server establish a dedicated
connection over which they can communicate. During the connection process, the client
is assigned a local port number, and binds a socket to it. The client talks to the server by
writing to the socket and gets information from the server by reading from it. Similarly, the
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
server gets a new local port number (it needs a new port number so that it can continue
to listen for connection requests on the original port). The server also binds a socket to its
local port and communicates with the client by reading from and writing to it.
The client and the server must agree on a protocol--that is, they must agree on the
language of the information transferred back and forth through the socket.
Stream Socket:
A dedicated point-to-point channel between a client and server.
Use TCP (Transmission Control Protocol) for data transmission.
Lossless and reliable.
Sent and received in the same order.
Datagram Socket:
No dedicated point-to-point channel between a client and server.
Use UDP (User Datagram Protocol) for data transmission.
May lose data and not 100% reliable.
Data may not received in the same order as sent.
Q.8.(b)What is RMI? Explain RMI architecture.
A.8.(b)
stub
The stub is an object, acts as a gateway for the client side. All the outgoing requests are
routed through it. It resides at the client side and represents the remote object. When the
caller invokes method on the stub object, it does the following tasks:
The servlet is normally created when a user first invokes a URL corresponding to the
servlet, but you can also specify that the servlet be loaded when the server is first started.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
Each time the server receives a request for a servlet, the server spawns a new thread and
calls service. The service() method checks the HTTP request type (GET, POST, PUT,
DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
After the destroy() method is called, the servlet object is marked for garbage collection.
The destroy method definition looks like this:
Architecture Digram:
The following figure depicts a typical servlet life-cycle scenario.
o First the HTTP requests coming to the server are delegated to the servlet container.
o The servlet container loads the servlet before invoking the service() method.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com
IGNOU Solved Assignments By http://ignousolvedassignments.com
o Then the servlet container handles multiple requests by spawning multiple threads,
each thread executing the service() method of a single instance of the servlet.
Course Code : MCS-024 Course Title : Object Oriented Technologies and Java
Assignment Number : MCA(2)/024/Assignment/16-17 http://ignousolvedassignments.com