• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting values from a 2D array in one class to another class (to then "convert" to another array)  RSS feed

 
Gijs Walravens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, dear people,

Pretty new programmer here (4th week into a Java course now) and I have run into a bit of a problem.
The main focus of the current tasks we are being given is handling different methods throughout classes.
While I have been succesful in declaring objects etc. , I am now a tad bit confused of what to do now.

Basically, we have been given three classes, of which two are really of the importance here; the class Cell and the class Field (none of these are the main class)

First we have to class Field, in which there is supposed to be a void readInitial, which gets the initial input (which I have decided would be handy to put in a 2D Array)

It looks (with some bits cut out ofc.) like this:



I haven't been able to test if anything over there is correct since the whole code is far from finished, but InteliJ doesn't indicate anything is wonky so I'm gonna go with it for now.

Here is the problematic part: I now have a different class Cells. Inside that class Cells I need the data from the array of the class Field. To be more specific, I need to use it in a boolean getAlive() which would return whether an inpute would be alive or dead (for those experienced, you probably have noticed already that this is a Game of Life task )

The initial input would consist of a field of "." and "*", representing dead and alive cells resp.
Now I have an array filled with all of these, and have to connect these characters to false or true resp. (at least that is what I understood from it)

I have tried something like this:


I have tried many things I found on the internet, but these generaly don't seem to fit my problem, so any other advice here would be apreciated

Thanks in advance,

Gijs
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Gijs,

in your 'Field' class, the readInitial() method is a void method, i.e. returns nothing.
Then, the 2D array 'startingGen' is local to this method, so once this method
finishes, that's the end of 'startingGen' as well.

What you can do is, for instance, make 'startingGen' an instance variable,
so that it is known after 'readInitial()' finishes.

You could then supply a getter for this array, for instance

so that you can invoke this method from within your Cell class.

Greetz,
Piet
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, don't write long lines.
I broke them up for you this time.

And welcome to the Ranch!
 
Gijs Walravens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Gijs,

in your 'Field' class, the readInitial() method is a void method, i.e. returns nothing.
Then, the 2D array 'startingGen' is local to this method, so once this method
finishes, that's the end of 'startingGen' as well.

What you can do is, for instance, make 'startingGen' an instance variable,
so that it is known after 'readInitial()' finishes.

You could then supply a getter for this array, for instance

so that you can invoke this method from within your Cell class.

Greetz,
Piet


Thanks for the response (and sorry to moderator for the long lines, I did not see that rule, il try to watch out for it).

So from what I understand from your advice, I should Field up like this:



And then in the Cell class:
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Gijs,

yes, that's what I meant.

The phrase about creating a copy of the 2D array is related to making that 2D array
a private member, and the fact that only the Field can then change that array.
But it is not essential to get your program going.

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!