Search code examples
javaclassmethodserror-handling

Why I Can't Use My Method in Another Class? (The Method X is Undefined for the Type Y)


I wrote a code in Java but I'm keep getting an error named "The method getMinor(Matrix) is undefined for the type Main." for the last line of Main class. I can use my other methods in Main class but can't use getMinor and printMinor methods in it. What am I doing wrong?

This is Matrix class:

import java.util.ArrayList;

public class Matrix {

    private int n;
    private int m;
    private ArrayList<Double> Matrices = new ArrayList<Double>();
    
    public Matrix(int n, int m) {
        
        this.n = n;
        this.m = m;
        
        Matrices = new ArrayList<Double>(n*m);
        
    }
    
    public void printElement(int a, int b) {
        
        int index = (a-1)*m + b - 1;
        double element = Matrices.get(index);
        System.out.println(element);
        
    }
    
    public double getElement(int a, int b) {
        
        int index = (a-1)*m + b - 1;
        double element = Matrices.get(index);
        return element;
        
    }
    
    public void printMatrix() {
        
        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() {
        
        return n;
        
    }
    
    public int getColumnNumber() {
        
        return m;
        
    }
    
    public Matrix getMinor(Matrix Matrices) {
        
        Matrix minor = new Matrix(Matrices.getRowNumber() - 1, Matrices.getColumnNumber() - 1);
        
        for(int r = 1; r <= Matrices.getRowNumber(); r++) {
            for(int i = 1; i < Matrices.getColumnNumber(); i++) {
                minor.addTo(r, i, Matrices.getElement(r+1, i+1));
            }
        }
        
        return minor;
        
    }
    
    public void printMinor(Matrix Matrices) {
        
        Matrix minor = getMinor(Matrices);
        minor.printMatrix();    
        
    }

        public void addTo(int a, int b, double value) {
        
        int index = (a-1)*m + b - 1;
        Matrices.add(index, value);
        
    }
    
}

This is Main class:

public class Main{

    public static void main(String[] args) {
        
        Matrix myNew2Matrix = new Matrix(3, 3);
        myNew2Matrix.addTo(1,1,3);
        myNew2Matrix.addTo(1,2,2);
        myNew2Matrix.addTo(1,3,2);
        myNew2Matrix.addTo(2,1,1);
        myNew2Matrix.addTo(2,2,4);
        myNew2Matrix.addTo(2,3,4);
        myNew2Matrix.addTo(3,1,8);
        myNew2Matrix.addTo(3,2,2);
        myNew2Matrix.addTo(3,3,5);
        getMinor(myNew2Matrix); //getting error in this line
    }

}

Solution

  • In the code you've posted, getMinor(...) is defined as a method of the Matrix class. This means you would need to call myNew2Matrix.getMinor(myNew2Matrix) with your current code. If you want to avoid specifying myNew2Matrix twice, you could do one of the following.

    1. Make getMinor a static method (not bound to an instance of Matrix). If you implement it this way, you would call Matrix.getMinor(myNew2Matrix).
    2. Don't pass an argument to getMinor, instead use this as the matrix you're performing calculations on. If you choose to do it this way, you would call myNew2Matrix.getMinor()
       public Matrix getMinor() {
            
            Matrix minor = new Matrix(this.getRowNumber() - 1, this.getColumnNumber() - 1);
            
            for(int r = 1; r <= this.getRowNumber(); r++) {
                for(int i = 1; i < this.getColumnNumber(); i++) {
                    minor.addTo(r, i, this.getElement(r+1, i+1));
                }
            }
            
            return minor;
        }
    

    Because you control the Matrix class, and the only thing you pass to getMinor is a Matrix, the second option would probably be the better option of the two, but both options will work.