NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Thursday, August 16, 2012

C++ program to perform array operations


//Program to create array,display array,search for an element and delete
#include<iostream.h>
#include<conio.h>
#include<process.h>
class array
{
int a[100];
int terms;
public:
array();
void create();
void display();
void search();
void delete();
};
array::array()
{terms=0;}
//create an array
void array::create()
{
int i;
cout<<"How many elements in the array:";
cin>>terms;
if((terms>100)||(terms<0))
{
cout<<"Sorry! the size does not support";
getch();
return;
}
{cout<<"enter the element";
for(i=0;i<terms;++i)
cin>>a[i];
}}
//Display the content of the array
void array::display()
{int i;
for(i=0;i<terms;i++)
cout<<a[i]<<" ";
}
//search
void array::search()
{int m;
cout<<"enter the element to search:";
cin>>m;
for(int i=0;i<terms;i++)
if(a[i]==m)
{cout<<m<<" is found in "<<i+1<<" position";break;}
else
if(i==terms)
cout<<"not found";
}
//Delete
void array::delete()
{int p;
cout<<"enter the position to delete:";
cin>>p;
if((p>terms)||(p<1))
cout<<"Position invalid!";
else
{terms--;
for(int i=0;i<terms;i++)
a[i]=a[i+1];
}}

void main()
{ clrscr();
array ar;
abcd:
cout<<"\n\nARRAY\n";
cout<<".........\n";
cout<<" 1.create\n 2.Display \n 3.Search\n 4.Delete \n 5.Exit\n\n";
cout<<"Enter your option...";

int inp;
 cin>>inp;
switch(inp)
{ case 1: ar.create();goto abcd;
 case 2: ar.display();goto abcd;
 case 3: ar.search();goto abcd;
 case 4: ar.delete();goto abcd;
 case 5: exit(0);
}
}

Sunday, August 12, 2012

Java program to read and print student information


import java.io.*;
class student
{
int slno;
String name;
student()
{
name=new String();
slno=0;
}
void get()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the name of the student: ");
name=br.readLine();
System.out.print("\nEnter the Serial Number: ");
slno=Integer.parseInt(br.readLine());
}
void disp()
{
System.out.println("Serial Number : "+slno+"\nName : "+name);
}
}
class studmark extends student
{
int m1,m2;
studmark()
{
m1=m2=0;
}
void get()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the mark1: ");
m1=Integer.parseInt(br.readLine());
System.out.println("Enter the mark2: ");
m2=Integer.parseInt(br.readLine());
}
void disp()
{
System.out.println("Mark1 = "+m1+"\nMark2 = "+m2);
}
}
class personal extends studmark
{
int w,age;
personal()
{
w=age=0;
}
void get()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Weight : ");
w=Integer.parseInt(br.readLine());
System.out.println("Enter the age : ");
age=Integer.parseInt(br.readLine());
}
void disp()
{
System.out.println("Weight = "+w+"\nAge = "+age);
}
}
class studinheritance
{
public static void main(String[] args)throws IOException
{
student x;
student y= new student();
x=y;
x.get();
x.disp();
studmark i;
x=i;
x.get();
x.disp();
personal p;
x=p;
x.get();
x.disp();
}
}

Java program to perform String sorting


import java.io.*;
class stringsort
{
String s[];
int n;
stringsort(int q)
{
s=new String[q];
n=q;
}
void sortstring()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter The names: ");
for(int i=0;i<s.length;i++){
System.out.print("name: "+(i+1));
s[i]=br.readLine();}
}
void sort()
{
for(int i=0;i<s.length-1;i++)
for(int j=i+1;j<s.length;j++)
{
if(s[i].compareTo(s[j])>0)
{
String temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
System.out.println("\nThe sorted array is: ");
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
}
public static void main(String[] args)throws IOException
{
BufferedReader b= new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter no of names : " );
int x=Integer.parseInt(b.readLine());
stringsort s=new stringsort(x);
s.sortstring();
s.sort();
}
}

Java program to read and print n student names


import java.io.*;
class stringop1
{
String s[];
int n;
stringop1(int x)
{
s= new String[x];
n=x;
}
void get()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<n;i++)
{
System.out.print("\nName of student "+(i+1)+" : ");
s[i]=br.readLine();
}
}
void disp()
{
System.out.print("\nThe Entered names are : ");
for(int i=0;i<n;i++)
System.out.print("\nName "+(i+1)+" : " +s[i]);
}
public static void main(String[] args)throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter the no.of students : ");
int x=Integer.parseInt(b.readLine());
stringop1 s=new stringop1(x);
s.get();
s.disp();
}
}

Java program to find reverse of a number


import java.io.*;
class reversenum
{
int num,dup,rev,rem;
reversenum()
{
rev=rem=0;
}
void get()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number : ");
num=Integer.parseInt(br.readLine());
dup=num;
while(dup>0)
{
rem=dup%10;
rev=rev*10+rem;
dup=dup/10;
}
}
void disp()
{
System.out.println("\nThe reverse of " +num+" is "+rev);
}
public static void main(String[] args)throws IOException
{
reversenum r=new reversenum();
r.get();
r.disp();
}
}

Java program to create user defined thread


class childthread implements Runnable
{
Thread t;
childthread()
{
t=new Thread(this,"hello");
System.out.println("child created");
t.start();
}
public void run()
{
try
{
for(int i=0;i<10;i++)
 {
 System.out.println("child "+i);
 Thread.sleep(500);
 }
}
catch(InterruptedException e)
{}
}
}
class parentthread
{
public static void main(String[] s)
{
 childthread c= new childthread();
try
{
 for(int i=0;i<10;i++)
  {
   System.out.println("main "+i);
   Thread.sleep(1000);
  }
}
catch(InterruptedException e)
{}
}
}

Java program to Implement Runnable class


class childthread implements Runnable
{
Thread t;
childthread()
{
t=new Thread(this,"odd");
System.out.println("child created "+t);
t.start();
}
public void run()
{
try
{
for(int i=1;i<10;i++)
 {
 System.out.println("child "+i);
 i++;
 Thread.sleep(500);
 }
}
catch(InterruptedException e)
{}
}
}
class oddeventhread
{
public static void main(String[] s)
{
 childthread c= new childthread();
try
{
 for(int i=2;i<=10;i++)
  {
   System.out.println("even "+i);
   i++;
   Thread.sleep(1000);
  }
}
catch(InterruptedException e)
{}
}
}

Java program to read and print n student information


import java.io.*;
class nstudio
{
String s[];
int num;
nstudio(int m)
{
s= new String[10];
num=m;
}
void getdata()throws IOException
{
BufferedReader br= new BufferedReader( new InputStreamReader(System.in));
System.out.println("Enter "+num+" Names : ");
for(int i=0;i<num;i++)
{
System.out.print(i+1+ " : ");
s[i]=br.readLine();
}
}
void dispdata()
{
System.out.println("\n The Entered Names are :");
for(int i=0;i<num;i++)
{
System.out.println(i+1+" : "+s[i]);
}
}
public static void main(String[] args)throws IOException
{
nstudio n= new nstudio(10);
n.getdata();
n.dispdata();
}
}

Java Program to perform Matrix addition


class matrixaddition
{
int a[][]={{1,2,5},{1,0,2},{0,0,1}};
int b[][]={{2,2,4},{6,6,4},{3,7,8}};
int sum[][];
matrixaddition()
{
sum=new int[3][3];
}
void display1()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
        System.out.print(a[i][j]+" ");
     System.out.println("\t");
  }
}
void display2()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
        System.out.print(b[i][j]+" ");
     System.out.println("\t");
  }
}
void addmatrix()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
       sum[i][j]=a[i][j]+b[i][j];
  }
}
void summatrix()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
        System.out.print(sum[i][j]+" ");
     System.out.println("\t");
  }
}
public static void main(String s[])
{
matrixaddition ma=new matrixaddition();
ma.display1();
System.out.println("\n");
ma.display2();
System.out.println("\n");
ma.addmatrix();
System.out.println("\n");
ma.summatrix();
}
}

Java program to perform matrix multiplication


public class matrixmultiplication
{
int a[][]={{1,2,3},{2,1,0},{0,1,2}};
int b[][]={{2,2,2},{1,0,1},{1,1,1}};
int prdt[][];
matrixmultiplication()
{
prdt=new int[3][3];
}
void findproduct()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
{
prdt[i][j]+=a[i][k]*b[k][j];
}
}
void product()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
System.out.print(prdt[i][j]+" ");
System.out.println("\n");
}
}
void display1()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
        System.out.print(a[i][j]+" ");
     System.out.println("\n");
  }
}
void display2()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
        System.out.print(b[i][j]+" ");
     System.out.println("\n");
  }
}
public static void main(String[] args)
{
matrixmultiplication m= new matrixmultiplication();
m.display1();
System.out.println("\n");
m.display2();
m.findproduct();
System.out.println("\n");
m.product();
}
}

Java program to find largest element of an programmer defined array


class largest
{
int a[]={12,10,8,9,44,23,44,66,84,1};
int large;
void findlarge()
{
large=a[0];
for(int i=0;i<10;i++)
    {
if(large<a[i])
{
large=a[i];
}
   }
System.out.println("Largest is "+large);
}

public static void main(String s[])
{
largest l=new largest();

l.findlarge();
}
}

Using of interface in java program


interface x
{
public void area();
}

class y implements x
{
int a,b;
           public void area()
{
a=5;b=7;
System.out.println("Area is "+a*b);c
}
}

 class z implements x
{
int a;
public void area()
{
a=5;
System.out.println("Area of box is "+a*a);
}
}

class inter
{
public static void main(String[] args)
{
y a=new y();
z s=new z();
a.area();
s.area();
}
}

Java Program to implement multilevel inheritance


import java.io.*;
class xyz
{
int x,y;
void get() throws IOException
{
BufferedReader br= new BufferedReader( new InputStreamReader (System.in));
System.out.print("Enter a num : ");
x=Integer.parseInt(br.readLine());
System.out.print("Enter a num : ");
y=Integer.parseInt(br.readLine());
}
}
class abc extends xyz
{
int a;
void calc()
{
a=x+y;
System.out.println("The sum of "+x+" & "+y+" is "+ a);
}
}
class mno extends abc
{
int s;
void disp()
{
s=x-y;
System.out.println("Difference : "+ s);
}
}
class inheritance
{
public static void main(String[] args) throws IOException
{
mno d= new mno();
d.get();
d.calc();
d.disp();
}
}

Java program to print Factorial of a number, as Programmers wish


class factorial
{
int f;
factorial()
           {
f=1;
           }
void disp()
{
for(int i=1;i<5;i++)
     {
f=f*i;
System.out.println(f);
      }
}
public static void main(String s[])
             {
factorial f= new factorial();
f.disp();
             }
}

Program to do Exception Handling in java


import java.lang.Throwable;
class exception
{
public static void main(String[] args)
{
int c;
int a=5;
int b=0;
try
{
System.out.println("Hellow....");
c=a/b;
System.out.println("Haiii");
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("yippeeeee");
}
}

Java program to Compare elements of an array


import java.io.*;
class compelarray
{
int a[],big,small,n;
compelarray(int x)
{
n=x;
a=new int[n];
}
void readarray()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter elements of array: ");
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
}
void compare()
{
big=small=a[0];
for(int i=0;i<n;i++)
{if(big<a[i])
big=a[i];
if(small>a[i])
small=a[i];
}
System.out.println("The biggest element is : "+big);
System.out.println("The smallest element is : "+small);
}
public static void main(String[] args)throws IOException
{
int size;
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter size of the array : ");
size=Integer.parseInt(br2.readLine());
compelarray c= new compelarray(size);
c.readarray();
c.compare();
}
}

Java program to find sum of the elements of an array


class arraysum
       {
int a[],sum;
arraysum()
    {
a= new int[12];
sum=0;
for(int i=0;i<12;i++)
    {
a[i]=i+1;
    }
  }
void add()
 {
for(int i=0;i<12;i++)
    {
sum=sum+a[i];
     }
System.out.println("\t"+"sum of elements of array is "+sum);
 }

void disp()
{
for(int i=0;i<12;i++)
{
System.out.println(i+" ");
}
}

public static void main(String s[])
 {
arraysum as = new arraysum();
as.disp();
as.add();
 }

       }