using constructor

Ads
 

using constructor

all constructor for matrix class?

View Answers

May 26, 2012 at 12:17 PM

The given code implements the Matrix class and show the addition of two matrices.

public class Matrix{
        int M;             
        int N;            
        int[][] data;

        public Matrix(int M, int N) {
            this.M = M;
            this.N = N;
            data = new int[M][N];
        }

        public Matrix(int[][] data) {
            M = data.length;
            N = data[0].length;
            this.data = new int[M][N];
            for (int i = 0; i < M; i++)
                for (int j = 0; j < N; j++)
                        this.data[i][j] = data[i][j];
        }
        Matrix(Matrix A) {
            this(A.data); 
        }

        public Matrix plus(Matrix B) {
            Matrix A = this;
            if (B.M != A.M || B.N != A.N)  throw new RuntimeException("Illegal matrix dimensions.");
            Matrix C = new Matrix(M, N);
            for (int i = 0; i < M; i++)
                for (int j = 0; j < N; j++)
                    C.data[i][j] = A.data[i][j] + B.data[i][j];
            return C;
        }

        public void show() {
            for (int i = 0; i < M; i++) {
                for (int j = 0; j < N; j++) {
                    System.out.print(data[i][j]+" ");
                }
                System.out.println();
            }
        }

        public static void main(String[] args) {
            int[][] arr1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 9, 1, 3} };
            Matrix A = new Matrix(arr1);
            System.out.println("Matrix A: ");
            A.show();        
            System.out.println();

            int[][] arr2 = { { 2, 3, 4 }, { 1, 2, 3 }, { 4, 1, 2} };
            Matrix B = new Matrix(arr2);
            System.out.println("Matrix B: ");
            B.show(); 
            System.out.println();

            System.out.println("Sum of two matrices: ");
            A.plus(B).show();
            System.out.println();
           }
    }

Ads









Related Tutorials/Questions & Answers:
using constructor
using constructor  all constructor for matrix class?   The given code implements the Matrix class and show the addition of two matrices. public class Matrix{ int M; int N
constructor
: a call to a constructor method. Constructor methods are special methods... of that type. The new operator creates the object, the constructor initializes
Advertisements
Constructor
Constructor  what is the advantage of a constructor ? not the purpose of the constuctor? give me some in-depth analysis?   Please visit the following links: http://www.roseindia.net/java/java-tips/oop/constructors
can we use scanner class,class , object and methods to get output without using constructor ????
and methods without using constructor??? //Program to illustrate the classes...can we use scanner class,class , object and methods to get output without using constructor ????  im getting error here..i hav used scanner class
constructor program
constructor program  write a program to calculate the gross salary and net salary of an employee based on the following attributes: empno,empname,emp.......using constructor.pls send program
call a constructor
call a constructor  How do you call a constructor for a parent class
Constructor in Servlet.
Constructor in Servlet.  how to write a constructor in Java Servlet?   Servlet is like a POJO .You can create constructor in servlet. You can also use constructor for initialising purpose but it is not a useful approach
constructor or object
constructor or object  object or construct which create first?   A constructor is always created first. Here is an example: class Example { Example(String st){ System.out.println(st); } public
constructor inheritance
constructor inheritance  can we inherit constructor ?if not then why yhis example is given class A { public A() { System.out.println("In A ctor"); } } class B extends
Getting information about Constructor
about the constructor by using the getConstructors() method. Here is an example... constructor. Now retrieve the name of the constructor by using the getConstructors... Getting information about Constructor     
constructor in java
constructor in java  Ex: public class A { public A(){ System.out.println("A"); } public A(int i){ this(); System.out.println(i); } } public class B extends A{ public B (){ System.out.println("B"); } public B (int i
Java Constructor
Every Class has at least one constructor, which assign initial values to instance variables of the class. Name of the constructor is same as class name. A default constructor with no arguments will be called automatically by the Java
Calling Constructor in Spring
and retrieving the values defined in the constructor using java file.    ... Calling Constructor in Spring       In the given example you will be learning about a constructor
constructor - Java Interview Questions
constructor  We cann't override a constructor, i.e., it is almost like a final method, then why cann't we write the constructor as final?  Hi friend, Overriding constructor : * you cannot override
ModuleNotFoundError: No module named 'constructor'
ModuleNotFoundError: No module named 'constructor'  Hi, My Python... 'constructor' How to remove the ModuleNotFoundError: No module named 'constructor' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'constructor'
ModuleNotFoundError: No module named 'constructor'  Hi, My Python... 'constructor' How to remove the ModuleNotFoundError: No module named 'constructor' error? Thanks   Hi, In your python environment
java serializable default constructor
java serializable default constructor  java serializable default constructor
Constructor overloading in java
to remember, they are as follows: You can call overloaded constructor using...Constructor overloading in java In this section we will discuss about constructor overloading in java. Constructor overloading is not much different from
Calling Constructor in Spring
and retrieving the values defined in the constructor using java file.    ... Calling Constructor in Spring       In the given example you will be learning about a constructor
Calling Constructor in Spring
the values defined in the constructor using java file.       <... Calling Constructor in Spring       In the given example you will be learning about a constructor and how
java default constructor
be the default constructor: 1) public Student(){ private int rollNo = 0; private... the space in memory and initializes the fields. So, in the default constructor you can initializ your fields with a value or you can simply create a constructor
Constructor - Java Beginners
Constructor  why can use constructor in java .without constructor Can we run program in java..?  to create an object constructor needed.   Hi friend, i am sending code class Constract{ int x,y
Java Constructor Overloading Example
Java Constructor Overloading Example In this section we will read about the constructor overloading in Java. We will see how the constructor overloading... of methods that have no any return type. Constructor's name is same as of its
program for default constructor in java
program for default constructor in java   class Box { double l; double b; double h; Box
Java - Constructor in java
the constructor feature in a class. This program is using two classes. First class... Java - Constructor in java     .... Constructor creates a instance for the class. Constructor initiates (initialize
What is the difference between a constructor and a method?
What is the difference between a constructor and a method?  Hi, What is the difference between a constructor and a method?   Hi, I have found a good link of Java program related to difference between constructor
What are constructors ? explain different types of constructor with example
What are constructors ? explain different types of constructor with example  Hi, What are constructors ? explain different types of constructor...;Hi, For creating a new instance or a new object of class we using
ModuleNotFoundError: No module named 'constructor-io'
ModuleNotFoundError: No module named 'constructor-io'  Hi, My... named 'constructor-io' How to remove the ModuleNotFoundError: No module named 'constructor-io' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'constructor-io'
ModuleNotFoundError: No module named 'constructor-io'  Hi, My... named 'constructor-io' How to remove the ModuleNotFoundError: No module named 'constructor-io' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'model-constructor'
ModuleNotFoundError: No module named 'model-constructor'  Hi, My... named 'model-constructor' How to remove the ModuleNotFoundError: No module named 'model-constructor' error? Thanks   Hi, In your
Constructor Inheritance
Constructor Inheritance      ... the class. Constructor declaration are just like method declaration, except... provides us with a default constructor to the class having no arguments

Ads