Question

I am writing a Game of Life java code for a school project and need to declare a method that calls a constructor from a separate class.

I am not sure if I'm writing this correctly, specifically the constructor parameters.

public class GameOfLife {   
    public static void main(String[] args){
        LifeWindow game = new LifeWindow([100][100], 8);
    }
} 

LifeWindow is a separate class with the constructor that was provided to me.

My IDE gives me an error on the LifeWindow line, saying:

"syntax error on token '(' expression expected after this token"

Also, I'm not sure how to call the method in main. "game." doesn't allow me to use any of the instance variables in the constructor.

EDIT: The constructor is:

public LifeWindow(int [][] world, int scale) {
    this.world = world;
    this.scale = scale;
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(world.length * scale, world[0].length * scale);
    this.setUndecorated(true);
    this.setVisible(true);
    this.createBufferStrategy(2);
}
Was it helpful?

Solution 2

You have to create a new array:

LifeWindow game = new LifeWindow(new int[100][100], 8);

Just for future reference, this is a pretty terrible API, using raw arrays like that.

A better choice would have been either a List<List<Integer>> or even better aMap<Integer,List<Integer>>.

An even better choice would have been a specific configuration class with named properties to let you know what they were actually being used for.

OTHER TIPS

You mean

new LifeWindow(new int[100][100], 8);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top