You are on page 1of 24

QUES 1> Write a programme for adding two numbers.

Initialize both
the variables in the programme.

class add1

public static void main(String args[])

int a=28,b=34,c;

c=a+b;

System.out.println("the sum of given two no.s is " +c);

OUTPUT
QUES 2> Write a programme for adding two numbers using command
line arguments.

class add2

public static void main(String args[])

int p1=Integer.parseInt(args[0]);

int p2=Integer.parseInt(args[1]);

int sum=p1+p2;

System.out.println("the sum of two no.s is" +sum);

OUTPUT
QUES 3> Write a programme to calculate the factorial of a given number
using command line argument.

class fact

public static void main(String args[])

int n=Integer.parseInt(args[0]);

int fac=1;

for(int i=n;i>1;i--)

fac=fac*n;

n=n-1;

System.out.println("the factorial is "+fac);

}}

OUTPUT
QUES 4> Write a programme to calculate the bitwise or operator , left
shift and right shift of a number.
class bit1

public static void main(String args[])

int a=1;

int b=2;

int c=3;

a|=4;

b>>=1;

c<<=1;

a^=c;

System.out.println("a="+a);

System.out.println("b="+b);

System.out.println("c="+c);

OUTPUT

QUES 5> Write a program to calculate the unsigned right shift -2>>>24
and -4>>>24.
class unsin

public static void main(String args[])

int a=-2;

int b=-4;

a>>>=24;

b>>>=24;

System.out.println("a="+a);

System.out.println("b="+b);

OUTPUT

QUES 6> Write a program to illustrate an example which includes


default and parameterized constructors.
class box
{

double width;

double height;

double depth;

box()

System.out.println("default constructor");

box(double w,double h,double d)

System.out.println("parametarizd constructor");

width = w;

height = h;

depth = d;

double volume()

return width*height*depth;

class cons

public static void main(String args[])

box b1=new box();


double vol;

b1.width=10;

b1.height=14;

b1.depth=15;

vol=b1.width*b1.height*b1.depth;

System.out.println("volume is="+vol);

box b2=new box(10.0,11.0,12.0);

vol=b2.volume();

System.out.println("volume is="+vol);

OUTPUT

QUES 7> Write a program to illustrate an example which includes


example of method overloading and method overriding.
class A

{
int i,j;

A(int a,int b)

i=a;

j=b;

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;

//displays k-- this overrides show() in A

void show()

System.out.println("after overriding k:"+k);


}

//overload show

void show(String msg)

System.out.println(msg +k);

class method

public static void main(String args[])

B subob = new B(1,2,3);

subob.show(); //this calls show() in B

subob.show("k aftr overloadin is k:"); // this calls overloaded show() in B

QUES 8> Write a program to illustrate an example which includes


example of inheritance using super for calling the super class keyword
and this keyword.
class one

int i;
one(int i)

this.i=i;

class two extends one

int i;

two(int a,int b)

super(a);

i=b;

void show()

System.out.println(" subclass i:"+i);

System.out.println(" superclass i:"+super.i);

class inheritance

public static void main(String args[])

two t = new two(11,22);

t.show();

}
}

OUTPUT

QUES 12> Calculate area of different geometrical figures(circle, rectangle,square,


triangle) using function overloading.
class area1

void areaa(double r)

double rad = r;
double arr= 3.14*r*r;

System.out.println("the are of circle is "+arr);

void areaa(int l,int b)

int len=l;

int brt= b;

int arr=len*brt;

System.out.println("the area of rectangle is"+arr);

void areaa(int s)

int sid=s;

int arr=s*s;

System.out.println("the area of square is"+arr);

void areaa(double b,double h)

double base=b;

double hgt=h;

double arr=0.5*base*hgt;

System.out.println("the area of triangle is"+arr);

class area

{
public static void main(String args[])

area1 ob = new area1();

ob.areaa(0.25);

ob.areaa(4,5);

ob.areaa(10);

ob.areaa(10.0,20.0);

QUES 11> Design three classes: Student, Exam and Result. The student class has
data members such as roll no, name etc. Create a class Exam by inheriting the
Student class. The Exam class adds data members representing the marks scored
in six subjects. Derive the Result from class Exam and it has its own members such
as total marks and average. Calculate the total marks and average.
class student

public int r_no;

public String name;

public String coursce;


void student1(int r,String n,String c)

r_no=r;

name=n;

coursce=c;

System.out.println("the roll no. of student is"+r_no);

System.out.println("the name of student is"+name);

System.out.println("the coursce of student is"+coursce);

class exam extends student

public String subjs[]={ "java" , "os" , "c" , "net" , "vhdl" , "ca" } ;

public int mrks[]=new int[6];

void exam1(int m1,int m2,int m3,int m4,int m5,int m6)

mrks[0]=m1;

mrks[1]=m2;

mrks[2]=m3;

mrks[3]=m4;

mrks[4]=m5;

mrks[5]=m6;

System.out.println("the marks in "+subjs[0]+"is "+mrks[0]);

System.out.println("the marks in "+subjs[1]+"is "+mrks[1]);

System.out.println("the marks in "+subjs[2]+"is "+mrks[2]);

System.out.println("the marks in "+subjs[3]+"is "+mrks[3]);


System.out.println("the marks in "+subjs[4]+"is "+mrks[4]);

System.out.println("the marks in "+subjs[5]+"is "+mrks[5]);

class result extends exam

public int total;

public double avg;

class resultstud

public static void main(String args[])

student ob1 = new student();

ob1.student1(1,"khush","bba");

exam ob2 = new exam();

ob2.exam1(10,20,30,40,50,60);

result ob3 = new result();

ob3.total=0;

for(int i=0;i<6;i++)

ob3.total=ob3.total+ob2.mrks[i];

ob3.avg=ob3.total/6 ;

System.out.println("the total of marks is"+ob3.total);

System.out.println("the average of marks is"+ob3.avg);


}

QUES 9> WAP to increment the employee salaries on the basis of there
designation(Manager-5000, General Manager-10000, CEO-20000, worker-2000).
Use employee name, id, designation , salary as data member and inc_sal as
member function.
class e

public String ename;

public int id;

public String des;

public float sal;


void e1(String ena,int i,String de,float sa)

ename=ena;

id=i;

des=de;

sal=sa;

System.out.println("employee name is "+ename);

System.out.println("employee id is "+id);

System.out.println("employee designation is "+des);

System.out.println("initial employee salary is "+sal);

public void inc_sal()

if(des=="manager")

sal=sal+5000;

else if(des=="general manager")

sal=sal+10000;

else if(des=="CEO")

sal=sal+20000;

else if(des=="worker")

sal=sal+2000;

else

System.out.println("wrong input");
System.out.println("new salary is "+ sal);

}}

class classem

public static void main(String args[])

e ob = new e();

ob.e1("khush",7687,"manager",40000);

ob.inc_sal();

QUES 10> Write a class bank, containing data member: Name of Depositor, A/c
type, Type of A/c, Balance amount. Member function: To assign initial value, To
deposit an amount , to withdraw an amount after checking the balance (which
should be greater than Rs. 500) , To display name & balance.

class bank

public String name;

public int no;

public String type;

public int bal;

public void set(String n,int i,String t,int b)


{

name=n;

no=i;

type=t;

bal=b;

System.out.println("name of customer is "+name);

System.out.println("A/C no. of customer is "+no);

System.out.println("type of A/C of customer is "+type);

System.out.println("initial balance of customer is "+bal);

public void deposit(int d)

bal=bal+d;

System.out.println("balance after deposition is "+bal);

public void withdraw(int w)

if(bal>=500)

bal=bal-w;

System.out.println("balance after withdraw"+bal);

else

System.out.println("insuffesent balance");

}
class bb

public static void main(String args[])

bank ob = new bank();

ob.set("khushboo" , 504 , "saving" ,40000);

ob.deposit(10000);

ob.withdraw(500);

Ques 13. Wap to Implement dynamic dispatch method?

class mypar

public void show()

System.out.println("par");

public void display()

System.out.println("display");

}
}

class dispatch extends mypar

public void show()

{ super.show();

System.out.println("dispatch");

public void show1()

System.out.println("show1");

public static void main(String nm[])

dispatch d=new dispatch();

mypar m=d;

m.show();

m.display();

d.show1();

}
Ques . 14 wap to extends interfaces and implements interface.

interface x

void a();

void b();

interface y

{
void b();

void c();

interface z extends x,y

void d();

class inter implements z

public void a()

System.out.println(" a");

public void b()

System.out.println(" b");

public void c()

System.out.println(" c");

public void d()

System.out.println(" d");

public static void main(String nm[])


{

inter i=new inter();

i.a();

i.b();

i.c();

i.d();

You might also like