i have a class Matrix and I use the this pointer in the application class to access the methods of the Matrix class. For some reason it doesn’t identify the methods. This is a code snippet and it gives me an error starting with the AddMatrix method when I use
import java.io.*;
import java.util.*;
import java.util.Scanner;
class Matrix {
double [][] element;
int rows, cols ;
Matrix(int rows, int cols){
this.rows = rows;
this.cols = cols;
element = new double [rows][cols];
}
public double getValue (int row, int col){
return element[row][col];
}
public void setValue (int row, int col, double value){
element[row][col] = value;
}
public int getNoRows(){ // returns the total number of rows
return rows;
}
public int getNoCols(){ // returns the total number of cols
return cols;
}
// The methods for the main calculations
public Matrix AddMatrix(Matrix m2){
int row1 = getNoRows();
int col1 = getNoCols();
Matrix result = new Matrix(row1, col1);
for (int i=0; i<row1; i++){
for (int j=0; j<col1; j++) {
result.setValue(i,j, (getValue(i,j) + m2.getValue(i,j)));
}
}
return result;
}
public Matrix MultiplyMatrix(Matrix m2){
if (this.getNoCols != m2.getNoRows)
throw new IllegalArgumentException ("matrices can't be multiplied");
int row2 = this.getNoRows();
int col2 = m2.getNoCols();
Matrix result = new Matrix(row2, col2);
for (int i=0; i<row2; i++){
for (int j=0; j<col2; j++){
result.setValue(i,j,(this.getValue(i,j)*m2.getValue(i,j)));
}
}
return result;
}
this.getNoCols != m2.getNoRowsshould be
this.getNoCols() != m2.getNoRows()You are using the syntax to access a variable instead of the syntax for calling a method.