How to make data available to two different classes - theory and code question
I am trying to make my first Java program using classes and objects and so I picked a simple game problem to model. (1) Spin wheel (2) Move player 6 spaces (for example) (3) Display all the "spaces" with the player shown on the correct space. Done! (This is somewhat like some homework I have seen on this forum).
I want to learn to use objects so I set up a class called Space, which can be occupied or vacant. I made an array of 20 spaces. I set up another class called Player, which can have a "position" , for example player[1].position =6. So the player has landed on space 6. The main method will spin the wheel to generate a number.
My theory question is: how do I let the space object "know" that the player object contains the value "6". Or perhaps some other information found only in the player object, such as last_position (where the player was earlier, or the color of the piece, etc.).
Below is code for the Space class. Below that is an error. I'm a newbie so I hope this make sense to you all. The main() class spins the wheel and then I want "the action" to occur in my objects, which I believe is the whole point of OOP.
One other thing. I use Eclipse and so I think I am required to create three files. The main file for the Game class and then two more files for Space and Player. Does that seem correct? To have a separate file for each class? Just checking. Thanks.
Code :
public class Space
{
public int player_on; // occupied by which player
int bonus_value;
int zz;
public void update_space (int id)
{
this.player_on =id; // so maybe put player "1" on space 6
zz= player[1].last_position; // this is causing the error
}
public void zero_space ()
{
this.player_on =0 ;
}
}
-----------------------------
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
player cannot be resolved to a variable
at Space.update_space(Space.java:17)
at gameopoly.main(gameopoly.java:81)
0
Re: How to make data available to two different classes - theory and code question
I'm not entirely sure I understand, but I think your best bet would be to draw a little diagram of everything involved and see what needs what.
e.g a Game has many Players and one Board. the Board has many Spaces.
You can use the Game class as your controller. Have a think about what you want each class to do and what attributes they will need. Your Game class can be in charge of things like the spin.
I would also reccomend getters and setters rather than accessing the attributes of the class directly.
Re: How to make data available to two different classes - theory and code question
Hello - thanks for reply. This is just a learning exercise so the game is not really important. I just chose Player and Space so they could be used to instantiate objects that have attributes. My big question is: how do I set up two distinct classes and have class A access the data in class B. By way of example, suppose I had a Bank_Account and a Customer class. Anytime the bank account balance for a customer exceeds one million dollars I want the corresponding person instance to have an attribute changed to "gold level customer". So the Classes are distinct yet a method in one can change data in the other.
Re: How to make data available to two different classes - theory and code question
Getter/Setter methods.
Code Java:
public class ClassA
{
//The variable that class be wants to access
private int importantVariable;
//Basic Constructor
public ClassA(int startingValue)
{
importantVariable = startingValue;
}
//Get the variable
public int getVar()
{
return importantVariable;
}
//Set the variable
public void setVar(int newValue)
{
importantVariable = newValue;
}
}
ClassB:
Code Java:
public class ClassB
{
public static void main(String[] args)
{
new ClassB();
}
public ClassB()
{
ClassA classA = new ClassA(5);
int classAsVariable = classA.getVar();
System.out.println(classAsVariable);
classA.setVar(7);
classAsVariable = classA.getVar();
System.out.println(classAsVariable);
}
}
Re: How to make data available to two different classes - theory and code question
Thanks Tjstrech, I have used your code to make that work in my own example. One thing I don't get though.
I think of ClassA classA = new ClassA(5); as being a way to create a new instance (an object) called classA. I get that. But I am not sure what the following really does.
Quote:
public static void main(String[] args)
{
new ClassB();
}
Since I can see that the class called ClassA was created without a "new" command, I am not sure why ClassB must have one. Thanks again.
Lastly, suppose I want ClassC to have access to data in ClassA ? So three classes.
One other thing: I can't seem to find the "Code Tag" button - only Quote Tag.