0

I tried to write a code that finds NashEquilibrium in given matrix.

I was keep getting errors that says I can't call non static method from static method so I turned every method and instance variable to static, is that a problem?

There are tons of logic errors in my code and it gives wrong answer, could it be because they are all static or its only logic error?

import java.util.ArrayList;
import java.util.Scanner;

public class Nash
{
    public static String nes;
    public static String str;
    
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Please enter the amount of strategies for each player");
        int stratA = scan.nextInt();
        int stratB = scan.nextInt();
        String[][] utilities = new String[stratA][stratB];
        
        System.out.println("Please enter the utilities");
        for(int row = 0; row<stratA; row++)
            for(int column = 0; column<stratB; column++)
                utilities[row][column] = scan.next();
                
// Creates a 2D array with given utilities
                
        if (nashExists(stratA, stratB, utilities) == true)
            System.out.println(nes);
        else 
            System.out.println("No NE found");
            
// Prints the results

    }
    
    public static boolean nashExists(int strA, int strB, String[][] util)
    {
        int[][] movesA = new int[strA][strB];
        for(int row = 0; row<strA; row++)
            for(int column = 0; column<strB; column++)
                movesA[row][column] = Integer.parseInt(util[row][column].substring(0,1));
        
        int[][] movesB = new int[strA][strB];
        for(int row = 0; row<strA; row++)
            for(int column = 0; column<strB; column++)
                movesA[row][column] = Integer.parseInt(util[row][column].substring(2,3));

// Creates a 2d integer array for utilites of every strategy of A and B

        ArrayList<String> aNE = new ArrayList<String>();
        ArrayList<String> bNE = new ArrayList<String>();
            
        for(int row = 0; row<strA; row++)
            for(int column = 0; column<strB; column++)
                if (nashExistsA(row, column, movesA) == true)
                    aNE.add((row+1) + "," + (column+1));
                
        for(int row = 0; row<strA; row++)
            for(int column = 0; column<strB; column++)
                if (nashExistsB(row, column, movesB) == true)
                    bNE.add((row+1) + "," + (column+1));

// Checks if there are NE for one of players

        if (compareArrayLists(aNE, bNE) == true)
            return true;
        else
            return false;
    }

// Checks if there are any matchs between both players NE's         
    
    public static boolean nashExistsA(int r, int c, int[][] a)
    {
        int max = a[r][c];
        
        for (int i = 0; i<a.length; i++)
            if (max < a[i][c])
                max = a[i][c];
        
        if (a[r][c] == max)
            return true;
        else
            return false;
    }
    
    public static boolean nashExistsB(int r, int c, int[][] b)
    {
        int max = b[r][c];
        
        for (int i = 0; i<b[0].length; i++)
            if (max < b[r][i])
                max = b[r][i];
        
        if (b[r][c] == max)
            return true;
        else
            return false;
    }
    
    public static boolean compareArrayLists(ArrayList<String> aN, ArrayList<String> bN)
    {
        for (int i=0; i<aN.size(); i++)
        {
            String potNE = aN.get(i);
            if (bN.indexOf(potNE) >= 0)
                str += "(" + potNE + ") ";
        }
        
        nes = str;
        
        if (str.length()>0)
            return true;
        else 
            return false;
    }
}
Arson1st
  • 27
  • 5
  • In a "real" program using only static methods would (likely) be a problem. For short assignments and testing it's fine. (And no, static methods don't cause logic errors. You'll have to examine your code.) – markspace Dec 03 '22 at 11:08
  • It doesn't effects to the result right? – Arson1st Dec 03 '22 at 11:09
  • No it doesn't. Errors in code will affect the result, not declaring a method static. – markspace Dec 03 '22 at 11:11
  • There is nothing particularly wrong with it for small stuff, but the right approach here would be to learn Object Oriented Programming properly, which would teach you that you should create an instance of your class instead and use it to call your (now no longer necessarily declared as static) methods. – JustAnotherDeveloper Dec 03 '22 at 11:14
  • 3
    The important thing is understanding [What does the 'static' keyword do in a class](https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class). If you understand what the static keyword actually does the question whether something should be static or not should answer itself. – OH GOD SPIDERS Dec 03 '22 at 11:15

1 Answers1

2

Turning members (methods and fields) into static is the classic mistake that novices tend to make when learning their first object-oriented language.

Don't do this.

I have seen this happening twice in workplaces where we hired fresh college graduates who had practically no programming experience. Predictably, after struggling with it for a while, the colleague would come to one of the older guys asking for help, and the help invariably was "lose static everywhere".

The more things you turn into static, the less object-oriented you are; if you turn everything static, then you are not object-oriented at all; you might as well be programming in BASIC or in COBOL.

When you become more familiar with the language and you start doing more advanced stuff, you will discover legitimate uses for static, which are very rare. When you come across such a situation, you will know it. Until then, stick with the rule that says:

Generally, avoid static like the plague.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Additional advice which is off-topic but nonetheless very pertinent: also avoid public instance variables like the plague. If it is public, it must be final. If it is not final, it must be private. This rule is so fundamental that the language should actually be enforcing it; alas, it does not; nonetheless, adhere to it as if your life depends on it. – Mike Nakis Dec 03 '22 at 14:24