JAVA
Object Oriented Programming and Method
Fill in the blanks
1.Abstraction means data hiding
2.Encapsulation means binding and wrapping data into a single unit
3.Inheritance advantage code reuse
4.Polymorphism means one interface, Multiple method or Multitasking
5.Object means Collections of data
6.Class means Collections of Object
7. new is used for address in memory of object is allocated.
8.Write a syntax of method typename(parameter){}
9.Method Overloading means function name same argument different
10.Passing object to a method is called Call by Reference
Answer the Following Questions
(4 marks)
1.Write a program to find volume of box using class and object by passing one argument.
File name: Box.java
class Box {
double
width;
double
height;
double
depth;
}
//This class declares an object of type
Box.
File name: BoxDemo.java
class BoxDemo{
public static void
main(String args[]){
Box
mybox = new Box();
double
vol;
//assign
values to mybox's instance variable
mybox.width
= 10;
mybox.height
= 20;
mybox.depth
= 15;
//computer
volumn of box
vol=mybox.width
* mybox.height * mybox.depth;
System.out.println("Volumn
is"+vol);
}
}
2.Write a program for object Reference
File name: ObjDemo.javaclass
class ObjDemo
{
int a;
public static void main(String args[])
{
ObjDemo o1,o2;
o1=new ObjDemo();
o1.a=100;
o2=new ObjDemo();
o2.a=100;
if(o1==o2)
System.out.println("Both are equal");
else
System.out.println("Both are not equal");
//reference of
object o1 is assigned to object o2
o2=o1;
if(o1==o2)
System.out.println("Both are equal");
else
System.out.println("Both are not equal");
}
}
3.Write a program for call by value
In java simple type of method ,it is
passed by value is called call by value.
File name: CallTest.java
class CallTest{
void
change(int i)
{
i=i+1;
System.out.println("Inside
function i ="+i);
}
}
File name: CallByValue.java
class CallByValue {
public static void
main(String args[]){
CallTest
ob=new CallTest();
int
a=10;
System.out.println("Before
call a="+a);
ob.change(a);
System.out.println("Before
call a="+a);
}
}
4.Write a program for call by reference
File name: RefTest.java
class RefTest{
int
a,b;
void
change(RefTest o){
o.a+=2;
o.b-=2;
}
void
show(){
System.out.println("A="+a+",B="+b);
}
}
File name: CallByRef.java
class CallByRef{
public
static void main(String args[]){
RefTest
ob1=new RefTest();
RefTest
ob2=new RefTest();
ob2.a=10;
ob2.b=20;
System.out.println("Before
calling change");
ob2.show();
ob1.change(ob2);
System.out.println("After
calling change");
ob2.show();
}
}
Answer the Following Questions
(4 marks)
1.Write a class and object program to find volume of box using calling method by passing two argument.
File name: Box.java
class Box {
double
width;
double
height;
double
depth;
//This class declares an object of
type Box.
File name: BoxDemo1.java
class BoxDemo1{
public static void
main(String args[]){
Box
mybox1 = new Box();
Box
mybox2 = new Box();
double
vol;
//assign
values to mybox's instance variable
mybox1.width
= 10;
mybox1.height
= 20;
mybox1.depth
= 15;
mybox2.width
= 3;
mybox2.height
= 6;
mybox2.depth
= 9;
//compute
volume of 1st box
vol=mybox1.width
* mybox1.height * mybox1.depth;
System.out.println("Volumn
is"+vol);
//compute
volume of 2nd box
vol=mybox2.width
* mybox2.height * mybox2.depth;
System.out.println("Volume is"+vol);
}
}
2.Write a program for method overloading.
Two or more methods within the same
class method name same ,argument different is called Overloading.
File name: OverloadDemo.java
class OverloadDemo{
void
multi(){
System.out.println("No
values to multiply");
}
void
multi(int a) {
System.out.println("a*a:"+a*a);
}
void
multi(int a,int b) {
System.out.println(a+"and"+b+":"+a*b);
}
double multi(double a){
return
a*a;
}
public static void main(String args[]){
OverloadDemo
ob =new OverloadDemo();
double
result;
ob.multi();
ob.multi(10);
ob.multi(10,20);
result=ob.multi(123.25);
System.out.println("Result
of ob.multi(123.25):"+result);
}
}
No comments:
Post a Comment