Results 1 to 10 of 10

Thread: Matrix

  1. #1
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Matrix

    Hi,

    Im new to java and i want to make a 2x2 matrix, but nothing on the internet could me make clear enough to understand it.
    So basically i want a class "Matrix" who represents a 2x2 matrix.
    Any help would be great!;)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default Re: Matrix

    Quote Originally Posted by awesom View Post
    Hi,

    Im new to java and i want to make a 2x2 matrix, but nothing on the internet could me make clear enough to understand it.
    So basically i want a class "Matrix" who represents a 2x2 matrix.
    Any help would be great!;)
    You could use four separate numbers, as in:

    Java Code:
    double a00, a01, a10, a11;
    ... but that doesn't scale very well (you most certainly don't want to take that route with a 100x100 matrix); or you could simply define a two dimensional array:

    Java Code:
    double[][] a= new double[2][2];
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Matrix

    Hey thanks for you help, but now i got this:
    Java Code:
    public class Matrix
    {
       private int[][] x1,y1;
    
        /**
         * Constructor for objects of class Matrix
         */
        public Matrix()
        {
           double[][] a= new double[2][2];
        }
    Do i need to change the field too? Sorry this is like my second time java:$
    And yeah im so dumb, i want to print the result of the 2x2 matrix.
    Can i do that with a System.out.println?
    Last edited by sunde887; 10-05-2011 at 04:29 PM. Reason: Added code tags, [code]...[/code]

  4. #4
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Matrix

    no one can help me?
    I just cant fix it...

    What's wrong in my code?
    I can make a object, but when i excute "public void print()" it gives "double" back and nothing more:(

    Java Code:
    public class Matrix
    {
        // instance variables - replace the example below with your own
       private int[][] x1,y1;
    
        /**
         * Constructor for objects of class Matrix
         */
        public Matrix(double matr)
        {
           double[][] a= new double[2][2];
        }
    
       
        
        public void print()
        {
            System.out.println(double.class);
        }
    }
    Last edited by sunde887; 10-05-2011 at 04:29 PM. Reason: Added code tags, [code]...[/code]

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Matrix

    There are a lot of problems. Including, why are you printing double.class? Why do you expect it to print anything other than double? Why do you give the constructor an argument and not use it? Why is the argument a double, and not a matrix? Why do you create a local 2d array in the matrix constructor? Do you ever initialize the instance variables? Why is the instance variable of type int? Why do you have two 2d arrays as instance variables?

    Also, please use code tags when posting code: [code] YOUR CODE HERE [/code]
    Last edited by sunde887; 10-05-2011 at 04:31 PM.

  6. #6
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Matrix

    Quote Originally Posted by sunde887 View Post
    There are a lot of problems. Including, why are you printing double.class? Why do you expect it to print anything other than double? Why do you give the constructor an argument and not use it? Why is the argument a double, and not a matrix? Why do you create a local 2d array in the matrix constructor? Do you ever initialize the instance variables? Why is the instance variable of type int? Why do you have two 2d arrays as instance variables?

    Also, please use code tags when posting code: [code] YOUR CODE HERE [/code]
    I changed the double.class but the problem is that i dont know what to use to make it print.
    I did make a setter but i dont know if it will works because i cant see the result of it.
    How can i make the argument a matrix?
    And how can i make a matrix than?

    Java Code:
     public class Matrix
    {
        /**
         * Constructor for objects of class Matrix
         */
        public Matrix()
        {
            int [ ] [ ] scores = new int [ 2 ] [ 2 ] ;
        }
        
        public void setTable()
        {
            int [ ] [ ] scores = {   { 20, 18},
                                     { 18, 20}
                                 };        
        };
        
        public void print()
        {
            System.out.println();
        }
    }
    Greets,

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Matrix

    Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language) may be of help to you. Look into how to use the constructor properly. The setter, and the constructor, should initialize, or change, the instance variable of the class. Obviously the instance variable should be the two dimensional array(could also be 2 1D arrays if you don't want to use a 2D array. The print method should print each item in the instance variable via loop techniques.

  8. #8
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Matrix

    Cant you tell me what to do? I read those manuals and i still dont get it.
    I just want to know where i need to set "int [ ] [ ] scores = new int [ 2 ] [ 2 ] ;" And does this make a matrix?
    I need only a matrix and it needs to get a print option more not, some more explaination is welcome;)

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Matrix

    Re read those tutorials until you do understand. They are written quite well. I won't give you the answer (but the tutorials will).

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default Re: Matrix

    Quote Originally Posted by awesom View Post
    Cant you tell me what to do? I read those manuals and i still dont get it.
    I just want to know where i need to set "int [ ] [ ] scores = new int [ 2 ] [ 2 ] ;" And does this make a matrix?
    I need only a matrix and it needs to get a print option more not, some more explaination is welcome;)
    It's just a matter of local variables versus member variables; it has nothing to do with matrices per se. I agree with Sunde and also tell you to reread the manuals. If we'd give you the answer you'd just copy and paste it without understanding what you did which can be quite dangerous.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Determinant of matrix
    By osenna66 in forum New To Java
    Replies: 2
    Last Post: 07-03-2011, 04:57 AM
  2. Matrix Inverse
    By TryingMybestInJava in forum New To Java
    Replies: 4
    Last Post: 11-30-2010, 11:59 PM
  3. Matrix class
    By maz22 in forum New To Java
    Replies: 5
    Last Post: 10-15-2010, 06:53 AM
  4. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 01:26 PM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •