Download Jun 16 Full Paper Solution

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
JUNE 15
Q1 a) WAP to find GCD of two integers (5)
import java.util.*;
class GCD
{
public static void main(String args[])
{
int m,n,r;
Scanner in = new Scanner(System.in);
System.out.print("Enter value of m ");
m = in.nextInt();
System.out.print("Enter value of n ");
n = in.nextInt();
while (n != 0)
{
r = m % n;
m = n;
n = r;
}
System.out.println("GCD = " + m);
}
}
Q1 b) Explain ant 3 features of java (5)
1. Platform Independent
A program written in java language is said to be platform independent because
java program can run on all available operating systems.
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 1
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
2. Fully object oriented programming language
Every java program must start with class declaration. We cannot write even a
single line of code without declaring a class. Hence Java is called fully oopm
But older languages like C++ are not fully OOPM because declaring a class in
C++ program is optional. So we can write a C++ program without declaring a
class.
3. Multitheaded
A flow of control is known as a thred. When any Language executes multiple
thread at a time that language is known as multithreaded.
Q1 c) Draw and explain applet life cycle (5)
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 2
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
Applet class of Java has following methods
1. init(): In this method, the applet object is created by the browser. Because
this method is called before all the other methods, programmer can utilize
this method to initialize variables, setting background and foreground colors
etc.
2. start(): In init() method, even through applet object is created, it is
in inactive state. An inactive applet is not eligible for microprocessor time
even though the microprocessor is idle. To make the applet active, the init()
method calls start() method. In start() method, applet becomes active and
thereby eligible for processor time.
3. paint(): This method takes a java.awt.Graphics object as parameter. This class
includes many methods of drawing necessary to draw on the applet window.
This is the place where the programmer can write his code of what he expects
from applet like animation etc. This is equivalent to runnable state of thread.
4. stop(): In this method the applet becomes temporarily inactive. An applet can
come any number of times into this method in its life cycle and can go back to
the active state (paint() method) whenever would like. It is the best place to
have cleanup code. This method is automatically called whenever the applet
window is minimized by the user.
5. destroy(): This method is called whenever the applet window is closed. This is
the end of the life cycle of applet. It is equivalent to the dead state of the
thread.
 init() method is called at the time of starting the execution of applet. This is
called only once in the life cycle.
 start() method is called by the init() method. This method is called in the
life cycle; whenever the applet is maximized (to make applet active).
 paint() method is called by the start() method. This is called number of
times in the execution.
 stop() method is called whenever the applet window is minimized to
inactivate the applet. This method is called number of times in the
execution.
 destroy() method is called when the applet is closed. This method is called
only once in the life cycle.
Observe, the init() and destroy() methods are called only once in the life cycle.
But, start(), paint() and stop() methods are called a number of times.
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 3
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
Q1 d) Write wrapper classes and their application (5)
Wrapper classes
Each of Java's eight primitive data types has a class dedicated to it. These are
known as wrapper classes, because they "wrap" the primitive data type into an
object of that class. So, there is an Integer class that holds an int variable, there is
a Double class that holds a double variable, and so on. The wrapper classes are
part of the java.lang package.
Following list shows the primitive data types and their wrapper classes
Primitive type
Wrapper class
Boolean
-->
Boolean
Byte
-->
Byte
Char
-->
Character
Short
-->
Short
Int
-->
Integer
Long
-->
Long
Float
-->
Float
Double
-->
Double
The following two statements illustrate the difference between a primitive data
type and an object of a wrapper class:
a) int x = 25;
b) Integer y = new Integer(33);
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 4
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
The first statement declares an int variable named x and initializes it with the
value 25. The second statement instantiates an Integer object. The object is
initialized with the value 33 and a reference y refers to the object The memory
assignments from these two statements are shown as.
x
25
a) Declaration of an int variable
y
33
(b) instantiation of an Integer object
Q2 a) Write a program in java to print following pattern (5)
1
1
2
1
2
3
1
2
3
4
import java.util.*;
class Pattern1
{
public static void main(String args[])
{
int n,x,y;
Scanner in = new Scanner(System.in);
System.out.print("Enter value of n ");
n = in.nextInt();
for (x = 1 ; x <= n ; x++)
{
for (y = 1 ; y <= x ; y++)
System.out.print(y + " ");
System.out.println();
}
}
}
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 5
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
Q2 b) Write a note on System.arraycopy()
(5)
The arraycopy() method of System class copies an array from the specified
source array, beginning at the specified position, to the specified position of the
destination array.
Example
The following example shows the usage of java.lang.System.arraycopy() method.
public class SystemDemo {
public static void main(String[] args) {
int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 5, 10, 20, 30, 40, 50 };
// copies an array from the specified source array
System.arraycopy(arr1, 0, arr2, 0, 5);
System.out.print("array2 = ");
System.out.print(arr2[0] + " ");
System.out.print(arr2[1] + " ");
System.out.print(arr2[2] + " ");
System.out.print(arr2[3] + " ");
System.out.print(arr2[4] + " ");
}
}
The above code has a statement
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 6
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
System.arraycopy(arr1, 0, arr2, 0, 5);
This will copy arr1 array into another array arr2
Q2 c) Identify the classes and relationship for the given problem. (10)
i) Bank maintains 2 types of accounts for customers, saving account and current
account. Saving account provides compound interest and withdrawal facility.
Current account provides cheque book facility but no interest.
The above diagram shows inheritance relation.
BankAccount class will be parent class of Current and saving class. The data
members of BankAccount class will store name , account number, type of account
and balance. These data members will be inherited by both child classes. The
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 7
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
parent class BankAccount has methods such as withdraw() which allows customer
to withdraw() amount. The method withdraw(), deposit() and display() are
inherited by both child classes.
Current is a child class of BankAccount class and has chequebook data member
which shows that current account has chequebook facility.
saving account class has a data member “interest” which shows that saving
account gets interest.
ii) Library maintains Books and Magazines. A student can issue a book or return a
book. A fine is charged if the book is returned after 8 days. The magazines are not
issued but student can read it in the library.
Library
Student
Libraryname
Studentname
Address
StudentID
Address
Branch
Semester
1
1
Magazines
Reads
MagazineName
N
Cost
Books
Title
ISBN
Author
Cost
issuebook()
Computefine()
N
issues
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 8
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
Library class has “Aggregation” relationship with Books and Magazines. It means
Library contains Books and Magazines. The Aggregation relation is shown by using
a Diamond. The Diamond shown here is transparent diamond because the Books
and Magazines can exists even if Library does not exists.
The student class is an idependent class. But the student class has relationship
with book class that 1 student can issue any number of (N) Books. Student class
also has relationship with magazine class that 1 student can read N Magazines.
The Book class will also have methods such as “issuebook” which will issue a
book to some student. “computefine” can be a method to impose fine if the book
is returned late.
Q3 a) What is package. Explain how to create user defined package. (10)
A Package can be defined as a grouping of related types (classes and interfaces)
Some of the existing packages in Java are −

lang − bundles the fundamental classes Eg : Math, System , String etc

io − classes for input , output functions are bundled in this package. Eg :
DataInputStream, BufferedReader

util - contains utility classes. Eg : Vector , Scanner , Date etc

awt - Contains classes for GUI (graphics) programming. Eg: Color , Font etc

applet - contains Applet class which is used for designing applets
All the above packages are stored in a main package called java. Hence java
package contains all the sub packages like lang, util , awt and so on.
To use any of the predefined class of java we must import that class in our
program. For eg if we wish to use Color class of awt package in our program then
the import statement must be
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 9
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
import java.awt.Color;
This will import only Color class of awt package into our program.
Another method to import java classes is
import java.awt.*;
This will import all the classes of awt package into our program.
Steps to create user defined package in java
Step 1 : Suppose we want to create a package names as “glorious”. We must
create a folder under our home folder with the name “glorious”
Step 2 : Create a Java file (say First.java) inside glorious folder as follows
package glorious;
public class First
{
public void show()
{
System.out.println(“Hello”);
}
}
Save this file with name “First.java” in the folder glorious. The statement
“package glorious;” is written so that the class First will become part of glorious
package.
The class First must be public because it will be imported and used by other java
programs.
The method show() also must be public method because it will be called
(invoked) by other programs.
Compile the code First.java using javac compiler. This will create a byte code file
name First.java
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 10
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
The package development is completed. Note : if we wish to add one more class
say Second.java inside glorious package then we must create another file name
Second.java and store it in glorious folder. Hence every class which has to be
added in glorious package must be declared as public class in a separate file.
Step 3 : we can now use the package glorious in our program as follows.
Create a file named “Demo.java” in our home folder as follows
import glorious.*; // this will import all classes of glorious package
class Demo
{
public static void main(String args[])
{
First f = new First(); // create object of First class
f.show(); //call public void show() method
}
}
Q3
import java.util.*;
class VectorDemo2
{
public static void main(String args[])
{
Vector v = new Vector();
String name;
int ch;
do
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 11
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
{
System.out.println("Menu\n1.add\n2.remove\n3.display\n4.exit\n");
System.out.println("Enter choice ");
Scanner in = new Scanner(System.in);
ch = in.nextInt();
switch (ch)
{
case 1 : System.out.println("Enter name ");
name = in.next();
v.add(new String(name));
break;
case 2 : System.out.println("Enter name to delete ");
name = in.next();
v.remove(new String(name));
break;
case 3 :System.out.println("Names are " + v);
break;
case 4 : break;
default :System.out.println("Invalid choice");
}
}
while(ch != 4);
}
}
Q4
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 12
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
import java.util.*;
class Emp
{
private int id;
private String name;
private double salary;
Emp(int id , String name , double salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
void display()
{
System.out.println("Id = " + id);
System.out.println("name = " + name);
System.out.println("salary = " + salary);
}
}
class Array1
{
public static void main(String args[])
{
Emp e[];
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 13
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
int x;
String y;
double z;
e = new Emp[10]; // create array of 10 references to Emp objects
Scanner in = new Scanner(System.in);
for (int i = 0 ; i <= 10 ; i++)
{
System.out.print("Enter id ");
x = in.nextInt();
System.out.print("Enter name ");
y = in.next();
System.out.print("Enter Salary ");
z = in.nextDouble();
e[i] = new Emp(x,y,z); // create Emp object and pass x,y,z to parameterized
constructor of Emp class
}
System.out.println("List of 10 Employees ");
for (int i = 0 ; i <= 10 ; i++)
e[i].display(); // display ith employee
}
}
Q5 a) Explain with example how threads are created in java (5)
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 14
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
Two ways of creating thread in java are
Method 1 :
a) create a subclass from Thread class
b) override public void run() method in the subclass
c) create instance of subclass and call start() method on that instance
example: following program creates 2 threads.
class m1 extends Thread
{
public void run()
{
for(int i =1; i<=20;i++)
System.out.println(“hello”);
}
}
class m2 extends Thread
{
public void run()
{
for(int i =1; i<=20;i++)
System.out.println(“bye”);
}
}
class Test
{
public static void main(String s[])
{
m1 m = new m1();
m.start();
m2 x = new m2();
x.start();
}
}
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 15
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
Method 2 :
a) Create a class which implements Runnable interface
b) Override public void run() method in subclass
c) create instance of Thread class and pass instance of class created in step a as
parameter to constructor of Thread class.
d) now call start method on the instance of Thread class
Example:
class m1 implements Runnable
{
public void run()
{
for(int i =1; i<=20;i++)
System.out.println(“hello”);
}
}
class m2 implements Runnable
{
public void run()
{
for(int i =1; i<=20;i++)
System.out.println(“bye”);
}
}
class Test
{
public static void main(String s[])
{
m1 m = new m1();
Thread t = new Thread(m);
t.start();
m2 x = new m2();
Thread g = new Thread(x);
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 16
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
g.start();
}
}
Q5b) Explain multiple inheritance in java. (5)
Interface can be used achieve multiple inheritance in Java. A class can extend only
one parent class but a class can implement more than one interface. Example
class A extends B implements C
Here class A is child class of class B. class A also implements interface C. Hence
class A has all properties of parent class B and class A should also implement all
abstract methods of interface C.
Here is a program to show multiple inheritance in java
class A
{
void show()
{
System.out.println("In show of A");
}
}
interface B
{
void display();
}
class C extends A
implements B
{
public void display()
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 17
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
{
System.out.println("in
display of C");
}
}
class D
{
public static void
main(String args[])
{
C c = new C();
c.show();
c.display();
}
}
class C extends class A but class C implements interface B. hence class C must
override the abstract method display of interface B.
Q5 c) WAP to count number of alphabets, digits and special symbols in a
string.(10)
import java.util.*;
class Count
{
public static void main(String args[])
{
String x;
Scanner in = new Scanner(System.in);
System.out.print("Enter a string ");
x = in.nextLine();
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 18
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
int len = x.length();
int a = 0 ; //count alphabets
int d = 0 ; //count digits
int sp = 0 ; //count special symbols
for (int i = 0 ; i <= len-1; i++)
{
if (Character.isLetter(x.charAt(i)))
a++;
else
if (Character.isDigit(x.charAt(i)))
d++;
else
sp++;
}
System.out.println("No of alphabets = " + a);
System.out.println("No of Digits = " + d);
System.out.println("No of special chars = " + sp);
}
}
Output
Enter string abc12,e3/t
No of alphabets 5
No of digits 3
No of special symbols 2
Q9 Write short note on any four (20)
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 19
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
i) method overloading and method overriding
If a class have multiple methods by same name but different parameters, it is
known as Method Overloading.
If we have to perform only one operation, having same name of the methods
increases the readability of the program.
Suppose you have to perform addition of the given numbers but there can be 2
numbers added or there can be 3 numbers added. In such case we can write 2
methods with same name “add” as follows
int add(int a , int b)
{
return a+b;
}
int add(int a, int b , int c)
{
return a+b+c;
}
Method overriding
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in java.
In other words, If subclass provides the specific implementation of the method
that has been provided by one of its parent class, it is known as method
overriding.
Rules for Java Method Overriding
1. method must have same name as in the parent class
2. method must have same parameter as in the parent class.
Q6 ii) static data and methods
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 20
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
Static Data Members (Or Fields):
A type of data member that is shared among all the objects of class is known
as static data member. The static data member is defined in the class
with static keyword. When a data member is defined as static, only one variable is
created in memory even if there are many objects of that class. It is normally used
to share some data among all objects of a particular class. The main difference
between instance variable and static variable, static variable is shared among all
objects of the class. Only one memory location is created for static variable but
instance variable is one whose memory space is creating each and every time
whenever an object is created. So static variable make memory more efficient.
Note: When a variable is declared with static keyword it is known as class
variable.
Static Methods:
A method that can be accessed without any object of the class is known as static
method. Normally, a method of any class cannot be accessed or executed without
creating an object of that class. In some situation, a method has to be executed
without referencing any object. The static methods can be used to access a static
data member. The syntax to access static method is same as variable: Class
name.static method name.
Eg :
Math.sqrt() is a static method declared in Math class. Hence sqrt is directly
called on Math class.
Integer.parseInt() is a static method declared in Integer class
Q6 iii) abstract class and abstract methods
1. Abstract method is a method with zero code. Such a method is completely
empty and it has no statements to execute.
2. Abstract method is declared in such a way that declaration is terminated by
semicolon. We must write the keyword abstract before such methods.
3. Example the class X has abstract method show()
abstract class X
{
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 21
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
abstract void show(); // this method is empty and hence terminated by semicolon
} //end of class X
4. The class which has atleast one abstract method is called abstract class.
Hence the above class X is declared as abstract class.
5. Abstract class cannot be instantiated. That is we cannot create object of
abstract class. Following line is invalid because class X is abstract.
X a = new X(); // invalid cannot create object of abstract class X
6. Abstract class is always used as base class. The sub class of abstract class
must override all abstract methods of base class. Example suppose class Y
is declared as sub class of class X. Then class Y must override void show()
method.
class Y extends X
{
void show() // show() method must be defined here
{
// do something
}
}
7. Following program shows abstract class A which has one abstract method
show(). The class A derived 2 sub classes B and C. Both B and C override
show() method of base class A.
abstract class A
{
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 22
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
abstract void show();
}
class B extends A
{
void show()
{
System.out.println("in show of B");
}
}
class C extends A
{
void show()
{
System.out.println("in show of C");
}
}
class D
{
public static void main(String args[])
{
A a = new B();
a.show();
A b = new C();
b.show();
}
}
Output of the above program is
In show of B
In show of C
Q6 iv) Constructor and its types
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 23
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
1. Constructor is defined as a special function which has same name as the class
and no return type (not even void)
2. Constructor is used for initializing data members of object.
3. Constructor is called automatically when a new object is created.
4. A class can have more than one constructor provided these constructors have
different parameters.
5. Following class Complex has two constructors. First constructor has no
parameters and is called “default constructor”. The second parameter has 2
parameters and is called “parameterized constructor”.
class Complex
{
double a , b;
Complex( )
{
a = 0;
b = 0;
}
Complex(double x , double y)
{
a = x;
b = y;
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 24
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
}
// other methods of the class
} // end of Complex class
class Test
{
public static void main(String args[])
{
Complex c1 = new Complex( ); //default constructor is called
// so c1.a=0 and c1.b=0
Complex c2 = new Complex(2.2, 3.2); //parameterized
// constructor is called so c2.a=2.2 and c2.b=3.2
// rest of the program
}
}
Q6 v) JVM
JVM (Java Virtual Machine) is an abstract machine. It is a specification that
provides runtime environment in which java bytecode can be executed.
JVMs are available for many hardware and software platforms (i.e. JVM is
platform dependent).
It is:
1. A specification where working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm. Its
implementation has been provided by Sun and other companies.
2. An implementation Its implementation is known as JRE (Java Runtime
Environment).
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 25
Glorious Academy Paper Soln (JUNE 2015) Subject OOPM (Java) Sem III Computer/IT By Prof Sameer Y Velankar
3. Runtime Instance Whenever you write java command on the command
prompt to run the java class, an instance of JVM is created.
The JVM performs following operation:
o
o
o
o
Loads code
Verifies code
Executes code
Provides runtime environment
Visit us @ www.gloriousacademy.org / www.edumateengg.com Phone 9324793514 / 02225621515
Page 26