aspose file tools*
The moose likes Beginning Java and the fly likes New to Java setting up and implementing Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New to Java setting up and implementing Interface" Watch "New to Java setting up and implementing Interface" New topic
Author

New to Java setting up and implementing Interface

b fish
Greenhorn

Joined: Feb 19, 2013
Posts: 7
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

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
b fish
Greenhorn

Joined: Feb 19, 2013
Posts: 7
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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 13, 2005
Posts: 38851
    
  23
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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Feb 19, 2013
Posts: 7
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Feb 19, 2013
Posts: 7
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Feb 19, 2013
Posts: 7
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

Joined: Jul 19, 2010
Posts: 145

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.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New to Java setting up and implementing Interface