Welcome To Uday Satya Blog

21-30 PROGRAMS



21  Program to find the product of two matrices

class Mulm
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,1,1},{0,1,0},{0,0,1}};
int c[][]= new int[3][3];
int i,j,k;
for(i=0;i<3;i++)
            for(j=0;j<3;j++)
                        for(k=0;k<3;k++)
                                    c[i][j]=c[i][j]+a[i][k]*b[k][j];
for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                        System.out.print(+c[i][j]+ "  ");
            System.out.println();
            }
}
}

OUTPUT :

1  3  4
4  9  10
7  15  16

                                   
                                   


22  Program to demonstrate a CLASS

class Democlass //Defining a class
{
int a=34;//Instance Variable of the class
void showa() //Method of the class
{
System.out.println("a = " +a);
}
}//End of the class

class A
{
public static void main(String args[]) {
Democlass obj = new Democlass();
obj.showa();
}
}

OUTPUT :

a=34


23  Program to demonstrate a Constructor

class Box
{
double width;
double height;
double depth;
Box()
{
System.out.println("Constructing Box");
width=10;
height=10;
depth=10;
}
double volume()
{
return width*height*depth;
}
}
class BoxDemo6
{
public static void main(String args[]) {
Box mybox1=new Box();
Box mybox2=new Box();
double vol;
vol=mybox1.volume();
System.out.println("volume of is " +vol);
vol=mybox2.volume();
System.out.println("volume of is " +vol);
}}

OUTPUT :

Constructing Box
Constructing Box
volume of is 1000.0
volume of is 1000.0


24  Program to demonstrate a Parameterized Constructor

class Box
{
double width;
double height;
double depth;
Box(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
double volume()
{
return width*height*depth;
}
}
class BoxDemo7
{
public static void main(String args[]) {
Box mybox1=new Box(10,20,15);
Box mybox2=new Box(3,6,9);
double vol;
vol=mybox1.volume();
System.out.println("volume of is " +vol);
vol=mybox2.volume();
System.out.println("volume of is " +vol);
}
}

OUTPUT :

volume of is 3000.0
volume of is 162.0


25  Program to Demonstrate THIS keyword

class Box
{
double width;
double height;
double depth;
Box(double width,double height,double  depth)
{
this.width=width;
this.height=height;
this.depth=depth;
}
double volume()
{
return width* height* depth;
}
}
class BoxThis
{
public static void main(String args[])
{
double vol;
Box mybox1=new Box(10,20,15);
Box mybox2=new Box(3,6,9);
vol=mybox1.volume();
System.out.println("Volume is " +vol);
vol=mybox2.volume();
System.out.println("Volume is " +vol);
}
}

OUTPUT :

Volume is 3000.0
Volume is 162.0




26   Program to Demonstrate Constructor Overloading


class Box
{
double width;
double height;
double depth;
Box(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
Box()
{
width=-1;
height=-1;
depth=-1;
}
Box(double len)
{
width=height=depth=len;
}
double volume()
{
return width*height*depth;
}
}
class OverloadCons
{
public static void main(String args[]) {
Box mybox1=new Box(10,20,15);
Box mybox2=new Box();
Box mycube=new Box(7);
double vol;
vol=mybox1.volume();
System.out.println("volume of mybox1 is " +vol);
vol=mybox2.volume();
System.out.println("volume of mybox2 is " +vol);
vol=mycube.volume();
System.out.println("volume of mycube is " +vol);
}
}

OUTPUT :

volume of mybox1 is 3000.0
volume of mybox2 is -1.0
volume of mycube is 343.0


27  Program to pass objects as parameters


class Test
{
int a,b;
Test(int i,int j)
{
a=i;
b=j;
}
boolean equals(Test o)
{
if(o.a==a && o.b==b) return true;
else return false;
}
}
class PassOb
{
public static void main(String args[])
{
Test ob1=new Test(100,22);
Test ob2=new Test(100,22);
Test ob3=new Test(-1,-1);
System.out.println("ob1 == ob2 : " +ob1.equals(ob2));
System.out.println("ob1 == ob3 : " +ob1.equals(ob3));
}
}

OUTPUT :

ob1 == ob2 : true
ob1 == ob3 : false


28  Program to Demonstrate Queue using Class

class A
{
int q[] = new int[10];
int f,s;
A()
{
f =-1;
s=-1;
}
void push(int item)
{
if(f==9)
            System.out.println("Queue is Full");
else
            q[++f]=item;
}
int pop()
{
if(f==s)
            {
            System.out.println("Queue is Underflow");
            return 0;
            }
else
            return q[++s];
}
}
class que{
public static void main(String args[]){
A obj1 = new A();
for(int i=0;i<10;i++)
            obj1.push(i);
System.out.println("Queue elements are : ");
for(int i = 0 ;i<10;i++)
            System.out.println(obj1.pop());
}
}

OUTPUT :

Queue elements are :
0
1
2
3
4
5
6
7
8
9



29 Program to Demonstrate Stack using Class

class A
{
int s[] = new int[10];
int tos;
A()
{
tos=-1;
}
void push(int item)
{
if(tos==9)
            System.out.println("Stack is Full");
else
            s[++tos]=item;
}
int pop()
{
if(tos<0)
            {
            System.out.println("Stack is Underflow");
            return 0;
            }
else
            return s[tos--];
}
}
class stack{
public static void main(String args[]){
A obj1 = new A();
for(int i=0;i<10;i++)
            obj1.push(i);
System.out.println("Stack elements are : ");
for(int i = 0 ;i<10;i++)
            System.out.println(obj1.pop());
}
}

OUTPUT :

Stack elements are :
9
8
7
6
5
4
3
2
1
0


30  Program to Demonstrate Access Specifiers

class Test
{
int a;
public int b;
private int c;
void setc(int i)
{
c=i;
}
int getc() {
return c;
}
}
class AcessTest
{
public static void main(String args[])
{
Test ob=new Test();
//These are OK,a and b may be accessed directly
ob.a=10;
ob.b=20;
//This is not OK and will cause an error
//ob.c=100; //error!
//you must access c through its methods
ob.setc(100); //Ok
System.out.println("a,b,and c: " +ob.a+" " +ob.b +" "+ob.getc());
}
}

OUTPUT :

a,b,and c: 10 20 100

0 comments:

Post a Comment