I have three classes named Matrix, BasicOperations and Determinant in the same package.
I want to define separate methods in separate classes (for example, methods related to finding determinants under the Determinant class), but I get errors like "The method getValue(int, int) is undefined for the type Determinant" in lines 7 and 9 in the Determinant class. I couldn't figure out where I made a mistake, so I had to ask, how can I make these methods work?
Matrix.java (for creating objects)
package Matrix;
import java.util.ArrayList;
public class Matrix {
public int n;
public int m;
public ArrayList<Double> Matrices = new ArrayList<Double>();
public Matrix(int n, int m) {
this.n = n;
this.m = m;
Matrices = new ArrayList<Double>(n*m);
}
}
BasicOperations.java (for containing basic operation methods)
package Matrix;
public class BasicOperations extends Matrix{
public BasicOperations(int n, int m){
super(n, m);
}
public void putTo(int a, int b, double value) {
//allows user to put a value at coordinate ab in the matrix.
int index = (a-1)*m + b - 1;
Matrices.add(index, value);
}
public void changeValue(int a, int b, double newValue) {
//allows user to replace the value at coordinate ab in the matrix with a new value.
int index = (a-1)*m + b - 1;
Matrices.remove(index);
Matrices.add(index, newValue);
}
public void printValue(int a, int b) {
//allows user to print the value at coordinate ab in the matrix.
int index = (a-1)*m + b - 1;
double element = Matrices.get(index);
System.out.println("Bu matrisin "+a+". satır "+b+". sütunda bulunan elemanı "+element+" sayısıdır.");
System.out.print("\n");
}
public double getValue(int a, int b) {
//allows user to get the value at coordinate ab in the matrix.
int index = (a-1)*m + b - 1;
double value = Matrices.get(index);
return value;
}
public void printMatrix() {
//allows user to print the matrix.
for(int a = 1; a <= n; a++) {
System.out.print("[");
for(int b = 1; b <= m; b++) {
if(b == 1) {
System.out.print(Matrices.get((a-1)*m + b - 1).toString());
} else {
System.out.print(" " +Matrices.get((a-1)*m + b - 1).toString());
}
}
System.out.println("]");
}
System.out.print("\n");
}
public int getRowNumber() {
//allows user to get number of rows.
return n;
}
public int getColumnNumber() {
//allows user to get number of columns.
return m;
}
}
Determinant.java (for containing determinant operation methods)
package Matrix;
class Determinant{
public double get2x2Determinant() {
if(this.getColumnNumber() * this.getRowNumber() == 4) {
double det = (this.getValue(1, 1) * this.getValue(2, 2)) - (this.getValue(1, 2) * this.getValue(2, 1));
return det;
} else {
return 0;
}
}
Your direct problem stems from trying to call a method that doesn't exist for the class, getValue(...)
. Yes, the method exits in the Matrix class and in all classes that extend from it, and is declared public
, and so it can be called on any object that is a Matrix or a child of Matrix, but Determinant isn't the such a class as does not derive from the Matrix class, it has no parent class other than object. And so when you call this.getValue(...)
the compiler looks inside of the Determinant class and its parent, Object, sees that the method does not exist, and calls "foul".
One solution that is quick, easy and wrong would be to have Determinant extend from the Matrix or BasicOperations class, and then yes, it would have this method, but then you are stuck with a bad code design, an overly fragmented class. The Matrix class should truly hold all the states and behaviors of a mathematical matrix, and you are splitting it apart for no reason. This will only increase confusion, errors, and will give you no benefit. Instead, create a complete and full Matrix class. Divide out things that are not part of matrices, such as this: public ArrayList<Double> Matrices = new ArrayList<Double>();
. This doesn't really belong inside of Matrix.
Other suggestions:
e.g.,
public class Matrix {
private double[][] elements;
// most of this code is similar to your current code
public Matrix(int rows, int columns) {
this.elements = new double[rows][columns];
}
public Matrix(double[][] elements) {
this.elements = elements;
}
public Matrix(Matrix matrix) {
this.elements = matrix.elements;
}
public double getElement(int row, int column) {
return this.elements[row][column];
}
public void setElement(int row, int column, double value) {
this.elements[row][column] = value;
}
public int getRows() {
return this.elements.length;
}
public int getColumns() {
return this.elements[0].length;
}
public Matrix add(Matrix matrix) {
// test first that row and column counts are the same. If not, throw exception
// then do calculation
}
public Matrix subtract(Matrix matrix) {
// test first that row and column counts are the same. If not, throw exception
// then do calculation
}
public Matrix multiply(Matrix matrix) {
// test first that row count of one matrix equals the column counts of the other
// then do calculation
}
// getSubMatric
public Matrix getSubMatrix(int row, int column) {
// ...
}
public double getDeterminant() {
// check that row and column counts are not zero or one and that they are the same, that it is a square matrix
// note that even square matrices sometimes have no valid determinant
}
public Matrix getTranspose() {
//....
}
// print
public void print() {
for (double[] row : this.elements) {
for (double element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}