NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Showing posts with label Array in java. Show all posts
Showing posts with label Array in java. Show all posts

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 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();
}
}

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();
 }

       }