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)

No comments:

Post a Comment

Please drop your comment here, thanks.