Object Oriented Programming

The general declaration of a class is
                        class classname
                        {
                                    type instance var1;
                                    type instance var2;
                                    - - - - - - - - -  - --  - - -                                                 
                                    - - - - - - - - -  - --  - - -                                                 
                                    - - - - - - - - -  - --  - - -                                                 
                                    type instance method();
                        }

WRITE  A PROGRAM TO CREATE CLASS.

class Test
{
int a,b,c;
}
class Sample
{
public static void main(String args[])
{
Test t=new Test();
t.a=10;
t.b=20;
t.c=30;
System.out.println(t.a+t.b+t.c);
}
}

WRITE A PROGRAM TO FIND THE SUM OF TWO NUMBERS USING CLASSES AND OBJECTS.

class Sum
{
int a,b,c;
}
class Test
{
public static void main(String args[])
{
Sum s=new Sum();
s.a=10;
s.b=30;
s.c=s.a+s.b;
System.out.println(s.c);
}
}

WRITE A PROGRAM TO SWAP THE VALUES BETWEEN TWO OBJECTS.

class Test
{
int a,b;
}
class Swap
{
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
Test t3=new Test();
t1.a=10;
t2.a=40;
t1.b=20;
t2.b=60;
t3=t1;
t1=t2;
t2=t3;
System.out.println(t1.a+t1.b);
System.out.println(t2.a+t2.b);
}
}

WRITE A PROGRAM TO FIND THE AREA OF TRIANGLE USING CLASSES.

class Test
{
int b,h,a;
}
class area
{
public static void main(String args[])
{
Test t=new Test ();
t.b=20;
t.h=10;
t.a=(t.b*t.h)/2;
System.out.println(t.a);
}

WRITE A PROGRAM TO FIND THE AREA OF RECTANGLE USING METHODS.

class Area
{
int l,b,a;
public void disp()
{
l=a*b;
System.out.println("Area:"+l);
}
}
class A5
{
public static void main(String args[])
{
Area t=new Area();
t.a=10;
t.b=20;
t.disp();
}
}

WRITE A PROGRAM TO FIND THE AREA OF CIRCLE USING METHODS BY RETURN VALUE.

class Test
{
int r,a;
public int disp()
{
a=(r*r)*22/7;
return(a);
}
}
class A6
{
public static void main(String args[])
{
Test t=new Test();
t.r=10;
int a=t.disp();
System.out.println(a);
}
}

WRITE A PROGRAM TO PRINT THE TOTAL AND AVERAGE MARKS OF A STUDENT BY PASSING ARGS.

class Test
{
int tot,avg;
public void get(int a,int b,int c)
{
tot=a+b+c;
avg=tot/3;
}
public void put()
{
System.out.println("Total: "+tot);
System.out.println("Average: "+avg);
}
}

class Sample
{
public static void main(String args[])
{
Test t=new Test();
t.get(100,100,100);
t.put();
}
}

 

For
Online Classes

Contact Us: 9885348743