Menu

Wednesday 14 October 2015

Java Concept (Matrix Operation using java array 1)

package matrix;
import java.util.Scanner;

/**
    *
 * @author Adeniran Adetunji
 */
public class MatrixC {
 
    double [][]matrix;
 
    //Create Matrix of Row = x and Column=y
     double [][] createMatrix()
    {
        int x=0;
        int y=0;
        Scanner setMatrix = new Scanner (System.in);
        System.out.print("Enter Number of row(s)");
        x = setMatrix.nextInt();
        System.out.print("Enter Number of column(s)");
         y = setMatrix.nextInt();
        matrix = new double[x][y];
        initializeMatrix(matrix);
     
        return matrix;
     
      }
 
//create Column vector i.e matrix of 1 row
     double[][] createColumnVector()
     {
        final int x= 1;
        int y;
      Scanner columnNo = new Scanner (System.in);
      System.out.print("Enter Number of Column(s): ");
      y = columnNo.nextInt();
   
      matrix=new double [x][y];
      initializeMatrix(matrix);
   
      return matrix;
     }
   
//create Row vector i.e. matrix of 1 column
     double [][] createRowVector()
     {
         final int y= 1;
        int x;
      Scanner columnNo = new Scanner (System.in);
      System.out.print("Enter Number of Row(s): ");
      x = columnNo.nextInt();
   
      matrix=new double [x][y];
      initializeMatrix(matrix);
   
      return matrix;
     }
   
     void displayMatrix(double[][]matrix1)
     {
         for(int i=0; i<matrix1.length; i++)
         {
             for(int k=0; k<matrix1[i].length; k++)
             {
                 System.out.printf("%-10.1f", matrix1[i][k]);
             }
             System.out.println("");
         }
       
     }
   
   
    //Initialize matrix of row i and column K with each element initialy equal to zero

    void initializeMatrix(double [][]matrix)
    {
        for (int i=0; i<matrix.length; i++)
        {
            for(int k=0; k<matrix[i].length; k++)
            {
                matrix[i][k]=0.0;
            }
        }
    }
 
//Update matrix or enter matrix elements

    void enterMatrixElement(double[][]matrix)
    {
        for(int i=0; i<matrix.length; i++)
        {
            for(int k = 0; k<matrix[i].length; k++)
            {
                Scanner input_matrix_element = new Scanner (System.in);
                System.out.print("Enter element in "+ "row:"+" "+ (i+1)+" "+ "column :"+(k+1));
                System.out.print("==>");
                matrix[i][k]= input_matrix_element.nextInt();
            }
        }
    }

=======================================================================
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package matrix;

/**
 *
 * @author DELL
 */
public class Matrix {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        MatrixC myMatrix = new MatrixC();

        System.out.println("Creating and Intializing Matrix");
        System.out.println("================================");
       double[][] matrixA = myMatrix.createMatrix();
       
       System.out.println("Entering elements in matrix indexes");
       System.out.println("===================================");
        myMatrix.enterMatrixElement(matrixA);
      
       System.out.println("Displaying Matrix Elements");
       System.out.println("==========================");
      
        myMatrix.displayMatrix(matrixA);
       
        System.out.println("Creating and initializing Column Vector");
        System.out.println("=======================================");
                           
       double[][]matrixB=myMatrix.createColumnVector();
       
       System.out.println("Entering elements in the Column Vector");
       System.out.println("======================================");
                           
       myMatrix.enterMatrixElement(matrixB);
       
       System.out.println("Displaying the elements in the column vector");
       System.out.println("============================================");
        myMatrix.displayMatrix(matrixB);
         
        
       System.out.println("Creating and Initializing row vector");
       System.out.println("======================================");
       double[][]matrixC =myMatrix.createRowVector();
        
        System.out.println("");
        
       System.out.println("Entering elements in the row Vector");
       System.out.println("===================================");
        myMatrix.enterMatrixElement(matrixC);
        
         System.out.println("Displaying elements of the Column Vector");
         System.out.println("=========================================");
        myMatrix.displayMatrix(matrixC);
        
        
    }
        
        
        
    }

OUTUPUT
run:

Creating and Initializing Matrix
================================
Enter Number of row(s)3
Enter Number of column(s)4

Entering elements in matrix indexes
===================================
Enter element in row: 1 column :1==>3
Enter element in row: 1 column :2==>12
Enter element in row: 1 column :3==>4
Enter element in row: 1 column :4==>12
Enter element in row: 2 column :1==>4
Enter element in row: 2 column :2==>5
Enter element in row: 2 column :3==>12
Enter element in row: 2 column :4==>2
Enter element in row: 3 column :1==>3
Enter element in row: 3 column :2==>4
Enter element in row: 3 column :3==>2
Enter element in row: 3 column :4==>2
Displaying Matrix Elements
==========================
3.0       12.0      4.0       12.0      
4.0       5.0       12.0      2.0       
3.0       4.0       2.0       2.0       

Creating and initializing Column Vector
=======================================
Enter Number of Column(s): 3
Entering elements in the Column Vector
======================================
Enter element in row: 1 column :1==>3
Enter element in row: 1 column :2==>3
Enter element in row: 1 column :3==>3

Displaying the elements in the column vector
============================================
3.0       3.0       3.0       

Creating and Initializing row vector
======================================
Enter Number of Row(s): 3

Entering elements in the row Vector
===================================
Enter element in row: 1 column :1==>33
Enter element in row: 2 column :1==>3
Enter element in row: 3 column :1==>33

Displaying elements of the Column Vector
=========================================
33.0      
3.0       
33.0      
BUILD SUCCESSFUL (total time: 41 seconds)

Sunday 4 October 2015

Java Concept (Queue Data structure)

package queuedatastructure;

/**
 *
 * @author Adeniran Adetunji
 */
public class ArrayStaticQueue {
    //Declaring the instance variables of a queue. Initalizing head and tail to zero
    int headRef=0;
    int tailRef=0;
    char [] arrayQueue;
     
    //Method to create queue array
   void initializeQueue(int Size)
    {
     
       arrayQueue = new char[Size];
     
       for( int i=0; i<arrayQueue.length; i++)
       {
           arrayQueue[i] = '\u0000';
       }
    }
 
   //This method checks if queue is empty
  boolean isEmpty()
  {
      boolean test= true;
      for (int i=0; i<arrayQueue.length; i++)
      {
          if(arrayQueue[i]!='\u0000')
           
              test=false;
      }
   
      return test;
  }

  //This method test if queue is full
  boolean isFull()
  {
      boolean test = true;
   
      for (int i=0; i<arrayQueue.length; i++)
      {
          if(arrayQueue[i]=='\u0000')
           
              test = false;
      }
   
      return test;
  }

  //This method adds item to queue
 void addItem( char c)
 {
     // The test determines if the queue is full
     boolean test=isFull();
     // If the queue is full
     if(test)
     {
     //The system should print QUEUE IS FULL
         System.out.println("Queue is Full");
     }
     else
     {
        arrayQueue[tailRef]=c;
     }
   
     setTail();
 }
   
//This method set new tail location after adding item to queue
  void setTail()
  {
//Test if queue is full
 boolean  test=isFull();
     //if is full
     if(test)
     {
         //Then tail index becomes -1
         tailRef=-1;
     }
     //If queue is not full
     else
     {
         //Get the current tail index in nTail variable
     int nTail = tailRef;
   
     //Check for free space to the right-side of the tail
         for(int i=tailRef; tailRef<arrayQueue.length; i++)
         {
             //if found...
             if(arrayQueue[i]=='\u0000')
             {
                 //set new tail at the index of the free space
                 tailRef=i;
                 //break out of the loop
                 break;
             }
         }
         //Test if tailRef is not updated by the loop above
         //If test is true: that is loop is not updated
      if (nTail==tailRef)
      {
          //start searching for the free space from the begining of the
          //queue i.e. from index 0
          for (int k=0; k<arrayQueue.length; k++)
          {
             //Get first free space
             if(arrayQueue[k]=='\u0000')
             {
                 //Update tailRef at the index of the first free space gotten
                 tailRef=k;
                 //break out of the loop
                 break;
             }
          }
      }
     }
  }
  //This method removes item from queue
 void removeItem()
 {
     boolean test = isEmpty();
   
     if(test)
     {
         System.out.print("Queue is empty");
     }
     else
     {
       
         arrayQueue[headRef]='\u0000';
       
         if(tailRef==-1)
         {
             tailRef=headRef;
         }
       
     }
   
     setHead();  
 }

 //This method set new head location after removing item from queue
 void setHead()
 {
    boolean test = isEmpty();
         if(test)
         {
           
         headRef=0;
       
         }
         else
         {
             if(headRef+1<arrayQueue.length)
             {
               
              headRef=headRef+1;
             }
             else
             {
                 for(int i=0; i<arrayQueue.length; i++)
                 {
                     if(arrayQueue[i]!='\u0000')
                       
                         headRef=i;
                     break;
                 }
             }
         }
 }
   
 }

....Class Instance...
package queuedatastructure;

/**
 *
 * @author Adeniran Adetunji
 */
public class QueueDataStructure {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
     
        ArrayStaticQueue myQueue = new ArrayStaticQueue();
     
        myQueue.initializeQueue(5); //Initializes queue that can hold five characters
     
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }    

/*2*/    boolean check = myQueue.isEmpty();

            System.out.println(check);

/*3*/ System.out.println("Head =" + myQueue.headRef);
         System.out.println("Tail =" + myQueue.tailRef);

/*4*/  myQueue.addItem('A');
/*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*2*/ check = myQueue.isEmpty();

       System.out.println(check);
     
 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);
  /*5*/ myQueue.addItem('B');
       myQueue.addItem('C');
       myQueue.addItem('D');
       myQueue.addItem('E');

 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*2*/ check = myQueue.isEmpty();
     
       System.out.println(check);
     
 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);

              check=myQueue.isFull();
 /*6*/     System.out.println(check);

 /*7*/   myQueue.addItem('F');
/*8*/  myQueue.removeItem();
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);
     
/*9*/  myQueue.removeItem();
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);

 /*10*/ myQueue.removeItem();
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);

 /*11*/ myQueue.addItem('F');
 /*12*/ myQueue.removeItem();
 /*1.*/ for (int i = 0; i<myQueue.arrayQueue.length; i++)
       {
           System.out.println(myQueue.arrayQueue[i]);
       }  

 /*3*/ System.out.println("Head =" + myQueue.headRef);
       System.out.println("Tail =" + myQueue.tailRef);



     
     
}
}


Output
run:
1.





2. true      
3. Head =0
   Tail =0

4.A




1. false
2. Head =0
3. Tail =1
//After adding item at /*5*/
1.A
   B
   C
   D
   E
2.false
3.Head =0
4.Tail =-1
6.True
7.Queue is Full
//After /*8*/
1.
   B
   C
   D
   E
3.Head =1
4.Tail =0
//After  /*9*/

C
D
E
Head =2
Tail =0
//After /*10*/
1.


   D
   E
3.Head =3
4.Tail =0
//After  /*11*/ & /*12*/
F


E
Head =4
Tail =1
BUILD SUCCESSFUL (total time: 9 seconds)