Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java program to do Matrix operation

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java program to do Matrix operation

    I have an assignment that states:
    2. When you are adding two matrices together, you add similar elements together and place them in a new matrix. Because of this, you need to have matrices with identical sizes. Create a Matrix class, with methods such as add(Matrix a), subtract(Matrix a) and multiplyScalar(double n).

    I have no idea where to start, would anyone be able to help:?


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Adding matrices

    Hello saladfingers73 and welcome to the Java Programming Forums

    Do you know anything about Matrix? I suggest you take a look at:

    Getting and Setting Arrays and Their Components (The Java™ Tutorials > The Reflection API > Arrays and Enumerated Types)

    Here is an example of a Matrix:

    class MatrixExample{
     
        public static void main(String[] args)  {
     
        int array[][]= {{1,3,5},{2,4,6}};
        System.out.println("Row size= " + array.length);
        System.out.println("Column size = " + array[1].length);
        outputArray(array);
      }
     
       public static void outputArray(int[][] array) {
     
         int rowSize = array.length;
         int columnSize = array[0].length;
         for(int i = 0; i <= 1; i++) {
           System.out.print("[");
           for(int j = 0; j <= 2; j++) {
             System.out.print(" " + array[i][j]);
           }
           System.out.println(" ]");
         }
         System.out.println();
       }
    }
    See if this helps you and post back if you need help..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    santoshbharat (March 5th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Adding matrices the easy way

    /*
    A Program to add the two matrices
    */
    package com.bharat;
    import java.util.*;
    public class MyNewArrayConcepts {
    public static void main(String[] ar){
    //creating two dimensional arrays
    int[][] arr=new int[3][3];
    int[][] arr1=new int[3][3];
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter elements of first array");
    //accepting the elements for first array by taking the help of scanner object present in java.util
    for(int i=0;i<arr.length;i++){
    for(int j=0;j<arr[i].length;j++){
    arr[i][j]=sc.nextInt();
    }
    }
    //accepting the elements for second array by taking the help of scanner object present in java.util
    System.out.println("Enter elements of Second array");
    for(int i=0;i<arr1.length;i++){
    for(int j=0;j<arr1[i].length;j++){
    arr1[i][j]=sc.nextInt();
    }
    }
    //Displaying the elements of first array in the matrix form(3*3)
    for(int i=0;i<arr.length;i++){
    for(int j=0;j<arr[i].length;j++){
    System.out.print(arr[i][j]+" \t");
    }
    System.out.println();
    }
    System.out.println();
    //Displaying the elements of Second array in the matrix form(3*3)
    for(int i=0;i<arr1.length;i++){
    for(int j=0;j<arr1[i].length;j++){
    System.out.print(arr1[i][j]+" \t");

    }
    System.out.println();
    }
    //Adding the two matrices and Displaying the result of two matrices
    System.out.println("Adding of the matrices");
    for(int i=0;i<arr.length;i++){
    for(int j=0;j<arr[i].length;j++){
    System.out.print(arr[i][j]+arr1[i][j]+" \t");
    }
    System.out.println();
    }
    }
    }

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Adding matrices

    @santoshbharat, why are you resurrecting a post that is over 3 years old. Do you have a question? Is the code you posted meant to spoonfeed a solution?

  6. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Adding matrices

    Quote Originally Posted by copeg View Post
    @santoshbharat, why are you resurrecting a post that is over 3 years old. Do you have a question? Is the code you posted meant to spoonfeed a solution?
    Hey copeg i am sorry man,i am just learning java and figured like posting what i get to write about programming.i know its amateur way of writing.Could you please redesign the code to look better.Thanks in advance

  7. #6
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Adding matrices

    Quote Originally Posted by santoshbharat View Post
    Hey copeg i am sorry man,i am just learning java and figured like posting what i get to write about programming.i know its amateur way of writing.Could you please redesign the code to look better.Thanks in advance
    please read second post of this thread till end.

Similar Threads

  1. adding rows to coloumns. Netbeans
    By haygaurav in forum Java IDEs
    Replies: 1
    Last Post: April 1st, 2009, 03:16 PM