Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to make data available to two different classes - theory and code question

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.



















    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


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: How to make data available to two different classes - theory and code question

    Getter/Setter methods.

    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:
    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);
      }
    }

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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.

Similar Threads

  1. [SOLVED] Ok, I'm trying to make my own Map classes.
    By javapenguin in forum Collections and Generics
    Replies: 3
    Last Post: August 4th, 2011, 09:13 PM
  2. Theory/Question,
    By Time in forum Java Theory & Questions
    Replies: 7
    Last Post: November 9th, 2010, 05:26 PM
  3. i/o classes question
    By p0oint in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 28th, 2010, 04:04 PM
  4. noob question on abstract classes
    By joshred in forum Object Oriented Programming
    Replies: 1
    Last Post: September 15th, 2010, 06:27 PM
  5. A Question Regarding Abstract Classes & Packages
    By Kerubu in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 01:27 PM