Thursday, 17 March 2016

Sorting numbers in an array using java coding

The following program coding helps you to sort the numbers in  ascending and descending order.



class SortString{
 static String arr[]={
"13com50","13com48","13com64","13com41","13com52",
     "13com38","13com55","13com64","13com36","13com57"
                 
                      };
     public static void main(String args[]) {
               for(int j=0;j<arr.length;j++){
                for (int i=j+1;i<arr.length;i++) {
                 if(arr[i].compareTo(arr[j])<0) {
       String t=arr[j];
       arr[j]=arr[i];
       arr[i]=t;
      }
    }
  }
System.out.println("NAMES IN ASCENDING ORDER");
        for(int k =0; k<arr.length; k++)
      System.out.println(arr[k]);

System.out.println("NAMES IN DECENDING ORDER");
for(int k = arr.length-1;k>=0;k--)
            System.out.println(arr[k]);
   }
}
 

OUTPUT:                                                                                                                   
H:\>javac ascendingorder.java
H:\>java SortString
NAMES IN ASCENDING ORDER
13com36
13com38
13com41
13com48
13com50
13com52
13com55
13com57
13com64
13com64
NAMES IN DECENDING ORDER
13com64
13com64
13com57
13com55
13com52
13com50
13com48
13com41
13com38
13com36



Tuesday, 15 March 2016

Matrix multiplication using java coding

Similar to addition we can also write multiplication coding in java.
The program explains you.



import java.io.*;
class matrmul
{
 public static void main(String args[])throws IOException
{
int i,n,m,j,x,y,a[][],b[][],c[][];

           
DataInputStream br =new DataInputStream(System.in);

System.out.println("Enter the number of rows for A");
m=Integer.parseInt (br.readLine());

System.out.println("Enter the number of columns for A");
n=Integer.parseInt (br.readLine());
a=new int[m][n];
System.out.println("Enter the A array Elements");
  for (i=0; i<m; i++)
  {
    for (j=0;j<n;j++)
    {
      a[i][j]=Integer.parseInt(br.readLine());
    }
  }

System.out.println("Enter the number of rows for B");
x=Integer.parseInt (br.readLine());

System.out.println("Enter the number of columns for B");
y=Integer.parseInt (br.readLine());

b=new int[x][y];
System.out.println("Enter the B array Elements");
  for (i=0; i<m; i++)
  {
   for (j=0;j<n;j++)
   {
     b[i][j]=Integer.parseInt(br.readLine());
   }
  }
 
if(n==x)                                                                                                           
{
c=new int[m][y];

for(int i =0; i<m;i++)
            {
   for(j=0;j<y;j++)
             {
             for(int k =0; k<y;k++)
              {                                                      c[i][j]= c[i][j]+a[i][k]*b[k][j];
              }
             }
 }
 for(i=0;i<m;i++)
 {
    for(j=0;j<y;j++)
     {
      System.out.println(c[i][j]+"/t");
      }                                                        System.out.println("/n");
 }
}
else
{System.out.println("Matrix Multiplication is not possible");
}
}
}


OUTPUT
C:\Documents and Settings\dcom358>H:

H:\>cd java

H:\Java>path=C:\jdk1.3\bin;

H:\Java>javac matrmul.java
Note: matrmul.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

H:\Java>java matrmul                                      
Enter the number of rows for A
2
Enter the number of columns for A
2



Enter the A array Elements
1
2
3
4
Enter the number of rows for B
2
Enter the number of columns for B
2
Enter the B array Elements
1
2
3
4
Product is
7
10


15
22


Working with Objects in Java

Its simple and easy to work with objects in Java. Here is a sample program explaining about the working with the objects in java.

Here the class box is the Object. The object - get - has w,h,b which is declared in double. The print will print the w,b and h.


class box
{
double w,h,b;
void get()
{
w= 12;
h=6;
b=4;
}
void print()
{
System.out.println("Width ="+w+"\n");
System.out.println("height="+h+"\n");
System.out.println("breadth="+b+"\n");
}
}
/*Then carry on with the main program. The below given is our main program*/
 
class boxdemo
{
 public static void main(String args[])
 {
box b = new box();
b.get();
b.print();
}
}
/*The program calls get and print. Thus the coding in the get and print get exectued*/
OUTPUT
H:\>cd java

H:\Java>path=C:\jdk1.3\bin;

H:\Java>javac boxdemo.java

H:\Java>java boxdemo
Width =12.0

height=6.0

breadth=4.0