• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New to Java setting up and implementing Interface

 
b fish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a program to perform simple linear algebra operations such as addition, multiplication, transpose, etc. I understand the linear algebra concepts well enough, but I am new to programming and so I wanted to ask for some help getting this going. My goal is to use a basic interface for the matrix, and then implement that interface with a matrix object. Once I have the matrix object I want to use the consul to query the user for the size of the matrix and the matrix values and the plug those values into a new matrixentity object.

Here is my first shot at the interface:



And here is my attempt to implement the interface with a MatrixEntity




Using interfaces are totally new to me, so I am not quite sure the best way to set one up. Also I am struggling to figure out how to then populate a MatrixEntity object with information that the user puts in from the consul, i.e. the best way to plug in each integer value to the multi dimensional array.

Thanks in advance for any and all help!
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b fish wrote: Using interfaces are totally new to me, so I am not quite sure the best way to set one up. Also I am struggling to figure out how to then populate a MatrixEntity object with information that the user puts in from the consul, i.e. the best way to plug in each integer value to the multi dimensional array.


Welcome to the ranch Bobby. Could you please explain the use case in a bit more detail? What is it exactly that you are attempting to do here? Simply taking inputs from the user and storing, performing basic operations and storing them in a matrix is it? Or is there more to it?
 
b fish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

The end goal is to have a program that can perform matrix operations for the user. Meaning the program should take values in from the user, populate them into the matrix, allow the user to choose which operation to perform (addition, multiplication, transpose, etc..), and then perform that operation. I think I have a good grip on the operations, but I am struggling with setting up the following beforehand:

1. Creating a appropriate interface
2. Creating a Matrix object that implements that interface
3. Determining the right way to take matrix values from the user (most likely using a scanner) and populating that data into the Matrix.

Once I figure out how to properly create the Matrix object and populate the user data, I think I can figure out how to manipulate via methods for the Matrix operations.



 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to have a separate interface at all? You could simply create the 2D array using a constructor in your MatrixEntity class like you are currently doing, read the input from the console using a Scanner and populate it in the array. Then provide the user with options- 1, 2, 3, etc.. and use a switch-case construct to call the appropriate method as per the users choice. Does that answer your question? Or do you still have some doubt? Do one thing- start by writing the code for taking the input and populating the 2D array. Get that working correctly first.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Why do you need to have a separate interface at all?
It would probably be better design to have a Matrix interface. You can then create all sorts of classes which implement that interface and use them all under the generic title “Matrix”.
You could simply create the 2D array . . .
There is no such thing as a 2D array.

Beware: MatrixInterface is a bad name for the interface. Simply call it Matrix. It would require other methods, eg add() subtract() mutiply(), etc. I can’t remember much matrix arithmetic. Remember that you cannot add methods to the interface once you have implemented it, so make sure it has all the requisite methods before you let it loose on an unsuspecting world.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mansukhdeep Thind wrote: You could simply create the 2D array . . .
There is no such thing as a 2D array.


An array or arrays.



 
b fish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mansukhdeep Thind wrote:Why do you need to have a separate interface at all?
It would probably be better design to have a Matrix interface. You can then create all sorts of classes which implement that interface and use them all under the generic title “Matrix”.
You could simply create the 2D array . . .
There is no such thing as a 2D array.

Beware: MatrixInterface is a bad name for the interface. Simply call it Matrix. It would require other methods, eg add() subtract() mutiply(), etc. I can’t remember much matrix arithmetic. Remember that you cannot add methods to the interface once you have implemented it, so make sure it has all the requisite methods before you let it loose on an unsuspecting world.


Thanks for the input. I have revamped this a bit and I am now trying to figure out how to properly create my matrixEntity objects in the main class. Here is my code so far:

My interface is below.


Here is the implementation of that interface without method definitions.



Here is the main class where I am attempting to create MatrixEntity objects with dimensions as specified by the user.



I keep getting an error telling me that I cannot call "createSingleMethod(m1,n1)" staticly becuase it is a non static method. Should I simply make the method static?

Also any comments on the general design? Thanks!
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like that interface at all. Here's the simplest explanation of why I think it's wrong:



A method which returns the determinant of a matrix doesn't need another matrix as a parameter. It should just return the determinant of the matrix object on which it is called. So it should be



Likewise the add method should return the result of adding another matrix to the matrix object on which it is called, so it should be



(You have all of your Matrix parameters declared as double for some reason which I don't understand.)

As for the error message you're asking about -- yes, your createSingleMatrix() method should be a static method. It shouldn't be a method of the Matrix type because it isn't the job of a Matrix to produce another Matrix. I also don't understand why you have two methods called createSingleMatrix and createSecondMatrix when they do exactly the same thing. Why not just a createMatrix(int, int) method?


 
b fish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

As for the error message you're asking about -- yes, your createSingleMatrix() method should be a static method. It shouldn't be a method of the Matrix type because it isn't the job of a Matrix to produce another Matrix. I also don't understand why you have two methods called createSingleMatrix and createSecondMatrix when they do exactly the same thing. Why not just a createMatrix(int, int) method?


ahhhh I see what you mean. I'll change my interface.

As for the method creating matrices, if I create a single method, how will I make the second matrix for the addition, subtraction and multiplication operations?

I guess I am just looking for a good way to create two different MatrixEntity objects so I can them use them my operations on them.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b fish wrote:As for the method creating matrices, if I create a single method, how will I make the second matrix for the addition, subtraction and multiplication operations?

I guess I am just looking for a good way to create two different MatrixEntity objects so I can them use them my operations on them.


You call the single method twice.
 
b fish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
b fish wrote:As for the method creating matrices, if I create a single method, how will I make the second matrix for the addition, subtraction and multiplication operations?

I guess I am just looking for a good way to create two different MatrixEntity objects so I can them use them my operations on them.


You call the single method twice.


won't that create two matrices with the name "Matrix 1" ?
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b fish wrote:
Paul Clapham wrote:
b fish wrote:As for the method creating matrices, if I create a single method, how will I make the second matrix for the addition, subtraction and multiplication operations?

I guess I am just looking for a good way to create two different MatrixEntity objects so I can them use them my operations on them.


You call the single method twice.


won't that create two matrices with the name "Matrix 1" ?


Since the method returns a matrix object, you could do this:



Then you would have matrix1 and matrix2 from the same method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic